Writing URL into note body

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!

Yes. The only way to achieve what you want is by scripting. That can be AppleScript or JavaScript. There are lots of example scripts in the “Automation” subforum of the DT forum. I’m not sure if you need a Smart Rule at all in this case – only if you want to modify the data as a reaction to an event. If you just want to change all your MD files, a script alone should suffice.

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

SmartRule

1 Like
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.

1 Like

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?

Wait for @BLUEFROG or @cgrunenberg, they are the experts.

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

Here’s where I’m at:

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.

1 Like

Ah - that screenshot shows the wrong script. Something else I tried.

This is the setup with Bluefrog’s script:

  1. I’m not sure if a smart rule works without any condition.
  2. 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).

This works…

Done! @chrillek, your screenshot worked as advertised. @BLUEFROG, yours did not - I got an error message:

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.

Last question:

@Bluefrog’s script gives me a Markdown-formatted URL but with the [page title] simply replicating the DT URL field, like so:

[https://is.gd/0Txak9](https://is.gd/0Txak9)

@chrillek’s script takes the same information but inserts the page title:

[Why poetry matters](https://is.gd/0Txak9)

I prefer the latter. Is there any cost to @chrillek’s script over @BLUEFROGs? Should I just use @chrillek’s script, or is there an ideal mashup of the two?