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.

I hope it’s okay to reanimate this post years after it went quiet.

I’m trying to automate at least a portion of a very repetitive series of actions.
I generally receive many email attachments I need to save and reference later. I typically do the following:

a) Share the attachment to Devonthink (from MailMate).
b) Go to DevonThink, press ⌃, ⌘, ⌥, and “C” to copy the item link.
c) Paste the link in Obsidian along with relevant notes.

I hope to reduce a step or two by having a smart rule copy the UUID to the clipboard upon import so I can remove step B and go straight to step C.

I tried both of pete31’s AppleScripts from Nov 2020 in the apply script / AppleScript / “Edit Script…” button. No success.

Any thoughts?
Am I misunderstanding either the script or how the smart rule functions?

I am generally open to entirely different ways of achieving the same goal. My knowledge of AppleScript is limited to adapting pieces of other scripts (like those generously shared by pete31), but I’m open to simplifying or trying other tools (KM, Alfred?).

Help is greatly appreciated.

Welcome @mclnelson

You need to clarify and define your needs.

  • I would not set Kind is Any Document if you are intending to process emails. Set it to Email.
  • You need to set some other criteria or the smart rule could easily match the same emails over and over again. For example, tagging or moving the item could mitigate this possibility.
  • Are you only importing one email at a time?
    • If not, then using the clipboard wouldn’t be a feasible option.
  • Also, are you wanting to do this with every email?
    • If not, then what criteria determines when an automation should occur?

c) Paste the link in Obsidian along with relevant notes.

  • Is it necessary to put it into Obsidian?
  • What is the form and content of such a note in Obsidian?

Thank you so much for responding.
Your follow-up questions make me think that I may be missing or misunderstanding an entire layer of functionality in Devonthink as it relates to email and perhaps project management, but let me start by trying to be sure I’m being clear.

You need to clarify and define your needs.

My overall goal is to save attachments I receive by email so I can find them again later, linking them to notes (in Obsidian) and calendar appointments (in Fantastical) in general for fast retrieval.

I receive many PDFs (drawings, letters, and reports) by email.
Currently, I right-click on those attachments in my mail client (MailMate) and press “Share”/“Add to DEVONthink.”

I then open Devonthink, go to the inbox, and “Copy Item Link.”

I then open Obsidian, where I’ll add the link to the project that it is for.

My hope with this question was to eliminate the need to open Devonthink for this specific workflow. To have a smart rule (or something else) copy the UUID to the clipboard upon import so that I can go directly from sharing to DT in the mail client to pasting the UUID in Obsidian.

  • I would not set Kind is Any Document if you are intending to process emails. Set it to Email.
  • You need to set some other criteria or the smart rule could easily match the same emails over and over again. For example, tagging or moving the item could mitigate this possibility.

While I have tinkered with at least setting up DT import for email, I don’ actively use it for email, and now I’m wondering if there is a better answer to my question in learning more about this.
For now, I changed Kind to PDF/PS, which I generally receive. Word docs would probably be the next most popular. Would that be a Formatted Note maybe?

  • Are you only importing one email at a time?
  • If not, then using the clipboard wouldn’t be a feasible option.
  • Also, are you wanting to do this with every email?
  • If not, then what criteria determines when an automation should occur?

Not needed for every email, just for attachments that I need to maintain a record in Obsidian.

c) Paste the link in Obsidian along with relevant notes.

  • Is it necessary to put it into Obsidian?

Your question is causing me to reflect on how I am currently organized to see if adjustments may help.
I use Obsidian for project management; all project information, meeting minutes, call notes, and daily notes are there. I regularly link to DT from many notes in Obsidian.
Always open to suggestions.

  • What is the form and content of such a note in Obsidian?
    Drawings may provide the best example. I manage the preparation of drawings with large teams. I save drawing milestones to DEVONThink. I have a TOC/Map-of-Content for each project in Obsidian for each project, from there, I link out to DT. Drawing sets can at times, get into the hundred + page-count range. I often link notes or meeting prep notes to specific drawings in a set or even specific details on drawings, again using Obsidian linked to DT.

I am happy to explain further, but I don’t want to overstep my request for help.
Please let me know if more information is helpful.

…and again, I’m open to alternate methods of managing the workflow that I described.
Just trying to reduce friction.

Thanks again!

You’re welcome :slight_smile:

Word docs would probably be the next most popular. Would that be a Formatted Note maybe?

You’d use an Any subcriteria with Kind is PDF/PS and Extension is Word Document. Here is an example…

Specificity in smart items is a good habit. Here I have filtered to certain documents, added This Hour, and also tagged with a specific tag. This allows processing incoming documents while avoiding reprocessing them.

And the script is a fairly simple thing, but does support copying the links of multiple attachments. It puts a full Markdown URL on the clipboard…

on performSmartRule(theRecords)
	tell application id "DNtp"
		set attachList to {}
		repeat with theRecord in theRecords
			copy ("[" & (name of theRecord) & "](" & (reference URL of theRecord) & ")  " & return as string) to end of attachList
		end repeat
		set the clipboard to attachList as string
	end tell
end performSmartRule

Many thanks. This is so greatly appreciated.

You’re very welcome :slight_smile: