AppleScript: smart rule to grab item link of imported item


This smart rule, including a short script, effectively grabs the item link of an imported record and places it on the clipboard so that you can paste it in an existing markdown document in the group containing that document.

My need for the rule

I have written before of the delights of diarying (US: joy of journalling) in DEVONthink. I have separate groups for each year of diary entries and each of those groups contains a sub-group called Photos. When I wish to add a photo to a diary entry (initially imported into a Temp group) I import it into the relevant Photos group and then, of course, paste the item link in the diary entry. This rule simply saves me:

  1. right-clicking on the photo when imported and choosing Copy Item Link;
  2. then returning to the Temp group containing the relevant diary entry.

It’s not sophisticated but it does save me time (and saves me forgetting to do 1 above when I import the photo!).

Posted deferentially in the hope it might sometime help someone else. (Of course, you’d want to change the name of the watched group and the UUID of the group to wish you return at the end of the script.)

Smart rule

on performSmartRule(theRecords)
	tell application id "DNtp"
			repeat with theRecord in theRecords
				set theName to name of theRecord
				set theRefURL to reference URL of theRecord
				set theLink to "[" & theName & "]" & "(" & theRefURL & ")"
				set the clipboard to theLink
				display notification "Item link of image added to clipboard" with title "Get item link of imported image"
			end repeat
			-- Go back to the Temp group
			set myGroup to get record with uuid "F3E59383-23EF-4CAB-9912-369CF9CF45CC"
			set root of viewer window 1 to myGroup
		end try
	end tell
end performSmartRule


Do you actually insert a link in the Markdown document or do you use it to insert the image? Images can be also inserted by simply selecting them (yes, even multiple ones) in the item list, copying them and then pasting into the Markdown document.

I insert the image—which has reminded me to improve the script by including ! at the start of the assembled link.

Oh dear, one lives and learns (in the case of this “one”, rather slowly!). However, on the basis I’m almost invariably importing and including only one image then, with the modification mentioned above, I think the smart rule and script probably does save a few seconds.