Copy document link to clipboard on import

Unless I’ve missed something there is currently no action in the Smart Rules to copy a Devonthink link to the clipboard.

My use case. Reading through emails I regularly copy attachments to DT by dragging them into the Sorter. I then make an entry in my planning (text document) to schedule the tasks and the work I want to do e.g. next steps before going back to my email inbox.

In my planning entry I usually at the link to the document within DT. This is a pain - go to DT, copy link within DT, go back to planning text document, go back to email inbox and worth automating. Ideally I would have a smart rule triggered by the new Input which copies the DT link to the clipboard for me to paste in my planning document (without actually going to the DT window).

I don’t think this is possible yet as lacking a Copy Link and Paste to Clipboard action ?

What type is your planning text document? Markdown? Do you use the same planning document every day?

If so a short script should be possible which appends a link to the currently selected record into your planning document.

Thanks @pete31. Note that the planning document is not necessarily within DT (typically in Tinderbox which uses RTF).
We could try an intermediate solution based on RTF within DT synced to TB.

In the medium to longer term actions within Smart Rules that support Copy Link and Paste to Clipboard would provide more flexibility for the user.

Ah, I see.

So all you need is a way to copy the item link via a Smart Rule.

If you put these two lines in Smart Rule script syntax (see help) it should work

set theMarkdownLink to "[" & (name of theRecord) & "](" & (reference URL of theRecord) & ")"
set the clipboard to theMarkdownLink

The idea is good but no joy yet. What I’ve done is

  1. Created the following smart rule. The script is a copy based of your apple script code.

image

  1. Leave DT and open finder window. Drag any text document into the DT Sorter.

The clipboard is unchanged. I’m not sure the rule was triggered or at least the copy process did not work.

Tested further - seems to be the code as I’ve applied the rule manually without success.

The script isn’t compatible to smart rules and needs an on performSmartRule handler. In addition, the event On Clipping is only triggered after clipping websites via the Sorter.

According to the online help within DT

On Clipping: Runs when documents are added via clipping, e.g., via menu commands, the Sorter, DEVONagent, bookmarklets, or AppleScript.

My interpretation of the Help entry is that On Clipping triggers for all documents entering DT via the list provided e.g. the Sorter… not only Web Clippings as you seem to suggest. Maybe I’ve misunderstood ?

Yes, to use the code in a Smart Rule you have to wrap it in Smart Rule script syntax, like so:

on performSmartRule(theRecords)
	tell application id "DNtp"
		try
			repeat with theRecord in theRecords
				set theRefURL to reference URL of theRecord
				set the clipboard to theRefURL
			end repeat
		end try
	end tell
end performSmartRule

But pasting in Tinderbox wouldn’t result in a clickable link.

Give me some minutes …

This Smart Rule script copies the item link for pasting in RTF.

The part that actually prepares the link is from here.

-- Copy Item Link for pasting in RTF

on performSmartRule(theRecords)
	tell application id "DNtp"
		try
			repeat with theRecord in theRecords
				set theName to name of theRecord
				set theRefURL to reference URL of theRecord
				
				-- https://discourse.omnigroup.com/t/selecting-text-and-adding-a-hyperlink/23673/7
				if (theRefURL is not "") and (theName is not "") then
					set strHTML to quoted form of ("<font face=\"helvetica\"><a href=\"" & theRefURL & "\">" & theName & "</a></font>")
					do shell script "echo " & strHTML & "  | textutil -format html -convert rtf -stdin -stdout | pbcopy -Prefer rtf"
				end if
			end repeat
		end try
	end tell
end performSmartRule

And this version copies item links separated by a new line

-- Copy Item Link(s) for pasting in RTF

on performSmartRule(theRecords)
	tell application id "DNtp"
		try
			set theHTMLLinks to {}
			
			repeat with theRecord in theRecords
				set theName to name of theRecord
				set theRefURL to reference URL of theRecord
				set end of theHTMLLinks to "<font face=\"helvetica\"><a href=\"" & theRefURL & "\">" & theName & "</a></font><br>"
			end repeat
			
			set theHTMLLinks_string to my tid(theHTMLLinks, linefeed)
			
			-- https://discourse.omnigroup.com/t/selecting-text-and-adding-a-hyperlink/23673/7
			do shell script "echo " & quoted form of theHTMLLinks_string & "  | textutil -format html -convert rtf -stdin -stdout | pbcopy -Prefer rtf"
			
		on error error_message number error_number
			if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
			return
		end try
	end tell
end performSmartRule

on tid(theList, theDelimiter)
	set d to AppleScript's text item delimiters
	set AppleScript's text item delimiters to theDelimiter
	set theString to theList as text
	set AppleScript's text item delimiters to d
	return theString
end tid

@pete31 you’re a scholar and a Gentleman ! I’ve tried out your scripts and they work great. Thanks so much.

I hope @cgrunenberg can clarify the trigger mechanisms. I’ve applied your rules manually for the moment.

1 Like

The documentation seems to be correct, there are many ways to clip a website and only this triggers On Clipping. Importing a file triggers the On Importing event.

Maybe just add “website” for clarity along the lines of

On Clipping: Runs when documents are added via website clipping, e.g., via menu commands, the Sorter, DEVONagent, bookmarklets, or AppleScript.