New script to copy link to selected text

A while ago, I posted a script I was using for copying links to selected text in PDF files. This one here is an improved version that will encode empty spaces and everything else it needs to encode to create a valid link. It uses a tip offered by @BLUEFROG in this thread, and it circumvents a minor deficiency in it with regards to the linebreaks.

A couple of caveats:

  1. If I am not mistaken, links to selected text need to start at the word boundary. If you create a link and it does not work, double-check if the start of the selection coincides with the beginning of a word.
  2. If the selection spans across pages, the link won’t work.

So, to be clear, don’t select text on more than one PDF page, and always start the selection at the beginning of words.

--- bcdav 2022-05-15-14-52 ---

use scripting additions
tell application id "DNtp"
	if not (exists think window 1) or not (exists content record) then return display alert "DEVONthink" message "Error: no document open" as warning
	if not (exists selected text of think window 1) then return display alert "DEVONthink" message "Error: no text selected" as warning
	set the clipboard to my gsub((get the reference URL of (content record of think window 1)) & "?search=" & (do JavaScript "encodeURIComponent('" & my gsub((selected text of think window 1 as string), linefeed, "∫") & "');" in think window 1), "%E2%88%AB", "%0A")
end tell

on gsub(theString, old, new)
	set {TID, text item delimiters} to {text item delimiters, old}
	set theStringItems to text items of theString
	set text item delimiters to new
	set theString to theStringItems as text
	set text item delimiters to TID
	return theString
end gsub

Suggestions to improve upon it are more than welcome.

P.s.: @AW2307, perhaps this version could be useful to you.


I’ve been relying heavily on the script’s previous version for adding backlinks to specific sections in source documents. It’s the best available solution afaik. Assuming that this version further increases the stability of the links when there are special characters in the linked section, it will certainly be of value to me. Thanks for sharing :+1:

1 Like