I’d like to use Smart Rules to write the URL field into the body of a Markdown note, appended to whatever text is already in the note. Is this possible?
It should be. Get the plaintext property of the record, append the URL to it and set the plaintext to the new value
I gather you’re speaking in Applescript. I don’t talk Applescript, but I’ll dig around and see if I can cobble something together. Thanks!
Something like this? I am assuming the content in URL field is a DT link. The paste link name is the name of the DT link in the URL field. Else you will have to replace theName to anything that is appropriate.
Please use test files for confirming the results. I’ve tested the script but not thoroughly.
on performSmartRule(theRecords) tell application id "DNtp" repeat with theRecord in theRecords set theURL to theRecord's URL set theName to name of (get record with uuid (texts 21 thru -1 of theURL)) set theLink to "[" & theName & "](" & theURL & ")" set plain text of theRecord to plain text of theRecord & return & return & theLink end repeat end tell end performSmartRule
on performSmartRule(theRecords) tell application id "DNtp" set theRecords to item 1 of (selection as list) as list repeat with theRecord in theRecords set theURL to theRecord's URL if theURL ≠ "" then set theName to name of (get record with uuid (texts 21 thru -1 of theURL)) set theLink to "[" & theName & "](" & theURL & ")" set plain text of theRecord to plain text of theRecord & return & return & theLink end if end repeat end tell end performSmartRule
@ngan’s script amended to error trap if there is no URL on the file.
You have all been magnificently helpful. However, setting up a test smart rule (as per ngan’s screenshot, with Bluefrog’s script) doesn’t produce any change in the document when I run “Apply Rules”, targeting the specific smart rule.
It may be that I have been unclear, or that I’m not implementing things properly. Here are the pictures that say 1000 words:
This is typical of the MD document I want to amend:
And this is how I’d like to amend it, chiefly as a future-proofing strategy to ensure URLs are always available in the note body:
Any thoughts on where the problem lies?
on performSmartRule(theRecords) tell application id "DNtp" repeat with theRecord in theRecords set theURL to theRecord's URL as string set plain text of theRecord to plain text of theRecord & return & return & theURL end repeat end tell end performSmartRule
No result with that script either. Because I didn’t receive any feedback on whether the embedded script had run, I made an external script and put it in the “Smart Rules” script folder. On running it I got a fail dialogue:
2:52:53 pm: ~/Library/Application Scripts/com.devon-technologies.think3/Smart Rules/URL writes to note body.scpt on performSmartRule (Error -1708)
Does that provide any useful information?
I appreciate your willingness to try and help out someone wandering in the Applescript wilderness.
You might want to provide a screenshot of your smart rule
You set the script to apply to RTF, RTFD and TXT but not Markdown. So it won’t do anything with Markdown files. Technically they are plain text too. But they have a different file extension and are treated differently than txt by apps that can handle them (they get rendered for example).
And regarding RTF(D): Be very careful and test it with dummy notes. I’m not an AppleScript expert at all and might have done something wrong. But manipulating text of a RTF(D) note in a Smart Rule (it’s a whole different thing to actively be in a note) in my experience resulted in converting the note into plain text.
Ah - that screenshot shows the wrong script. Something else I tried.
This is the setup with Bluefrog’s script:
- I’m not sure if a smart rule works without any condition.
- More importantly: this is NOT the script that ngan posted previously. NOR is it the one that Bluefrog poste here. You inserted a weird line of code which modifies the input parameter (theRecords). It is working on “selection”, and I’m not sure that this does anything useful in a smart rule.
What I have
does what it is supposed to. I suggest that you use two conditions: kind is markdown and URL is not empty.
@chrillek gives good advice here.
A smart rule should have criteria to match specific files, ideally in specific locations (though this is dependent on the situation).
8:57:44 am: URL to note body on performSmartRule (Can’t get name of missing value.)
Something to do with your error trapping code? I think @chrillek’s solution of having the criteria “URL is not empty” probably sorts this out in another way.
I’m deeply appreciative of the investment in time and knowledge you’ve all made. I’ve been wrestling with how to incorporate certain information up-front in my note-taking, and then I realised that DevonThink can take care of most things automatically. I just need a little hand-holding to work it out. Thank you, all.
My script doesn’t have any reference to using the name of anything.
True. Tried again with a bit more care and it runs perfectly.
@Bluefrog’s script gives me a Markdown-formatted URL but with the [page title] simply replicating the DT URL field, like so:
@chrillek’s script takes the same information but inserts the page title:
[Why poetry matters](https://is.gd/0Txak9)