Create link to line?

I’m not seeing any issue with Script Editor here.

Well oddly enough - I have two Macs side by side.

On my new Mac I get the response above.

If I do the same exact thing on my older Mac (still running current version of Catalina) it works fine.

What could be configured differently about one of the Macs to cause this?

No idea on this, but I’d start by rebooting the machine.

OK that fixes it - for the other script causing problems too.

I try not to reboot the machine given that there are several user accounts active with others logging in - but clearly that is necessary periodically.

For anyone who uses Hook, you can generate unique tokens through Hook that you then add as annotations within records in DT (e.g., PDFs, RTF, plain-text). You can then invoke Hook with the token on your clipboard and Hook will bring you not only to the document with the associated annotation, but to the annotation within the document itself.

Some more info in this thread over on the Hook forum:

EDIT: For clarity, this only works with Skim and won’t bring you to the location in the PDF within DT.

1 Like

Thank you @pete31 and @Bernardo_V - very helpful script

Maybe @BLUEFROG can lend some insight here, but I think the expected functionality is that a search within DT as follows should at least give you a list of the records that have annotations applied to them:

Trouble is not only that this doesn’t appear to work (at least, for me, the PDFs I’ve annotated within DT aren’t showing up and other PDFs that seemingly contain no annotations do show up). Moreover, unless I’m missing something, it doesn’t appear to be possible to search the content of a note annotation from within DT. Again, hoping @BLUEFROG can clarify…

Once a week is not a bad idea

Nope. That is detecting files with associated Annotation files. Look for PDF Annotations.

Thanks for clarifying, @BLUEFROG! Is this the intended way to identify records with PDF Annotations?

The textbox next to the drop-down operators never expands larger than this, so I am thinking this is the only way to search for documents with annotations and that it’s not possible to distinguish between different kinds of PDF annotations… Is that accurate?

Also, this begs the question whether this returns only PDFs that have been annotated within DT, or whether PDF annotations applied in, e.g., Skim or Preview will also be found in this search.

The textbox next to the drop-down operators never expands larger than this,

That is a macOS control and only handles numerical values.

so I am thinking this is the only way to search for documents with annotations and that it’s not possible to distinguish between different kinds of PDF annotations… Is that accurate?

That is correct. It is a count of all annotations, regardless of type.

Also, this begs the question whether this returns only PDFs that have been annotated within DT, or whether PDF annotations applied in, e.g., Skim or Preview will also be found in this search.

Skim and Preview annotations will be detected and counted.

1 Like

What a great script - it really helps me a lot as I have to reference very often in large documents. I made a few additions, including for umlauts.
For the final touch, it would be great if I could enclose the search term with wildcards so that you don’t have to be so careful to select exactly full words. Does anyone have any ideas on how to make this work with the url scheme?

This won’t work, I’m afraid. Tested with asterisks and percent-encoded asterisks, both don’t work.

I tried that to without success but I hoped that there might be different wildcards or you have to use a like operator instead of an equals sign. Otherwise it would be a feature request that might be much more easy to implement than the original request for “links to line”. Hope we can get news on this

I would like to support this feature request (wildcards for the DevonThink URL scheme).

With the script from this post, it’s possible to backlink to specific passages in PDF documents - great!

However, the text highlighted upon clicking the link only covers characters before the first semicolon (or other non-supported character). It would be simple to replace the semicolon with another character, but after some experimentation I can confirm that none seems to be accepted as a wildcard i.e. the link will no longer function at all.

Actually, I just found a solution for my specific issue with semicolons. Perhaps this approach can work in other use cases as well.

On this site, you’ll find the html reference for different non-indexabled characters:

Similar to how spaces in the selected text are being replaced with %20 in @Bernardo_V’s original script, I modified the script to also replace semicolons with the respective html syntax. Now the entire passage that is selected when generating the link also gets highlighted upon following that link.

1 Like

Based on my regular use of @Bernardo_V’s really useful script, I found a couple additional special characters that cause trouble. Most special characters don’t have an impact but some do, specifically apostrophe, ampersand and forward slash. If the linked paragraph contains these characters, the link doesn’t work.

I have since adapted the script to take this into account by replacing these characters with the respective HTML codes in the generated link, such that it now does find and highlight the selected paragraph even if these special characters are present.

Here’s the slightly modified version - all credit to Bernardo for coming up with the script.

tell application id "DNtp"
	set theRecord to (content record of think window 1)
	set theSelection to the selected text of think window 1 as string
	set theSelection to my replaceText(theSelection, " ", "%20")
	set theSelection to my replaceText(theSelection, ";", "%3B")
	set theSelection to my replaceText(theSelection, "&", "%26")
	set theSelection to my replaceText(theSelection, "/", "%2F")
	
	set theURL to get the reference URL of theRecord & "?search=" & theSelection
	set the clipboard to theURL
	
end tell

on replaceText(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 replaceText
2 Likes

Though potentially less efficient than a pure vanilla AppleScript solution, this may be a more secure means of URL encoding in the long run, using ASObjC:

use framework "Foundation"

on encodeForURL(theText)
	set nsInput to current application's NSString's stringWithString:theText
	set characterSet to current application's NSCharacterSet's URLQueryAllowedCharacterSet()
	return (nsInput's stringByAddingPercentEncodingWithAllowedCharacters:characterSet) as text
end encodeForURL

I believe this comes originally from Shane Stanley, though I don’t remember where I copied it from.

1 Like

Here’s a simpler take on it…

tell application id "DNtp"
	set r to "https://www.devontechnologies.com"
	do JavaScript "encodeURIComponent('" & r & "');" in think window 1
end tell
--> "https%3A%2F%2Fwww.devontechnologies.com"
2 Likes

I wish I had read Jim (@BLUEFROG)'s post a couple of days ago before launching into a wild goose chase after a way to encode absolutely anything that could come up in a pdf file. My first impulse was to use Ruby, but calling it from an Applescript using do shell script is a nightmare.

The best I could do was to check for invalid characters and encode everything else using the Foundations framework:

Version using Foundation framework
use framework "Foundation"
use scripting additions
tell application id "DNtp"
	if (selected text of think window 1 as string) is "" or (selected text of think window 1 as string) contains "􏰙" then
		return display alert "DEVONthink" message "Error: empty or invalid selection" as warning
	else
		set the clipboard to (get the reference URL of (content record of think window 1) & "?search=" & stringByAddingPercentEscapesUsingEncoding_(NSUTF8StringEncoding of current application) of stringWithString_((selected text of think window 1 as string)) of NSString of current application) as string
	end if
end tell

Jim’s suggestion, however, fixes the problem I was having in the script above with invalid characters in the pdf. Javascript manages to encode them without any issues. But It doesn’t work to encode linebreak as the foundation framework, so I created a version to circumvent this and will post it to a new thread to make it easier for others to find and make suggestions.

2 Likes