Create link to line?

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