Script: Open DEVONthink record for PDF Expert tab

Hi there, this script opens the DEVONthink record for the PDF currently viewed in PDF Expert.

It’s meant to add a comment, an annotation, custom metadata or whatever you need DEVONthink for at this moment.

Enjoy :slightly_smiling_face:

-- Open DEVONthink record for current PDF Expert tab

-- solution for umlauts ("iconv -t UTF8-MAC") found here: https://stackoverflow.com/questions/23219482/bash-ps-grep-for-process-with-umlaut-os-x/23226449#23226449

tell application "System Events"
	try
		tell application process "PDF Expert"
			tell window 1
				set theFileName to value of attribute "AXTitle" & ".pdf"
				if theFileName starts with "* " then set theFileName to characters 3 thru -1 in theFileName as string
			end tell
			set PID to unix id
		end tell
	on error error_message number error_number
		if the error_number is not -128 then
			display alert "System Events" & space & error_number message error_message as warning
			return
		end if
	end try
end tell

set theOpenFiles to (do shell script "lsof -p " & PID)

set convertedOutput to (do shell script "iconv -t UTF8-MAC <<<$" & quoted form of theOpenFiles)

set theLines to paragraphs of convertedOutput

repeat with thisLine in theLines
	if thisLine contains theFileName then
		set thePath to characters (offset of "/" in thisLine) thru -1 in thisLine as string
		exit repeat
	end if
end repeat

tell application id "DNtp"
	try
		set theDatabases to databases
		set theResults to {}
		repeat with thisDatabase in theDatabases
			set thisDatabasesResults to lookup records with path thePath in thisDatabase
			set theResults to theResults & thisDatabasesResults
		end repeat
		if theResults = {} then
			display notification "Pfad nicht in offenen Datenbanken enthalten!" with title "Lookup DEVONthink"
			return
		else
			open window for record (item 1 of theResults)
			activate
		end if
		
	on error error_message number error_number
		if the error_number is not -128 then
			display alert "DEVONthink Pro" & space & error_number message error_message as warning
			return
		end if
	end try
end tell

It’s supposed to do what now? It opens a PDF that’s open in PDF Expert in DEVONthink?

Why are you using lsof for this?

I’ve found a solution for the umlauts problem :smiley:

(found here https://stackoverflow.com/questions/23219482/bash-ps-grep-for-process-with-umlaut-os-x/23226449#23226449)

Now it’s possible to go back from reading a PDF in PDF Expert to the corresponding record in DEVONthink with a shortcut. No need to search the PDF in DEVONthink anymore.

It’s even possible to set up e.g. Keyboard Maestro in a way that hitting the shortcut not only opens the DEVONthink record but also opens e.g. DEVONthinks info popup window to instantly add a comment, an annotation or custom metadata :sunglasses:

-- Open DEVONthink record for current PDF Expert tab

-- solution for umlauts ("iconv -t UTF8-MAC") found here: https://stackoverflow.com/questions/23219482/bash-ps-grep-for-process-with-umlaut-os-x/23226449#23226449

tell application "System Events"
	try
		tell application process "PDF Expert"
			tell window 1
				set theFileName to value of attribute "AXTitle" & ".pdf"
				if theFileName starts with "* " then set theFileName to characters 3 thru -1 in theFileName as string
			end tell
			set PID to unix id
		end tell
	on error error_message number error_number
		if the error_number is not -128 then
			display alert "System Events" & space & error_number message error_message as warning
			return
		end if
	end try
end tell

set theOpenFiles to (do shell script "lsof -p " & PID)

set convertedOutput to (do shell script "iconv -t UTF8-MAC <<<$" & quoted form of theOpenFiles)

set theLines to paragraphs of convertedOutput

repeat with thisLine in theLines
	if thisLine contains theFileName then
		set thePath to characters (offset of "/" in thisLine) thru -1 in thisLine as string
		exit repeat
	end if
end repeat

tell application id "DNtp"
	try
		set theDatabases to databases
		set theResults to {}
		repeat with thisDatabase in theDatabases
			set thisDatabasesResults to lookup records with path thePath in thisDatabase
			set theResults to theResults & thisDatabasesResults
		end repeat
		if theResults = {} then
			display notification "Pfad nicht in offenen Datenbanken enthalten!" with title "Lookup DEVONthink"
			return
		else
			open window for record (item 1 of theResults)
			activate
		end if
		
	on error error_message number error_number
		if the error_number is not -128 then
			display alert "DEVONthink Pro" & space & error_number message error_message as warning
			return
		end if
	end try
end tell

Yes, it takes you right back to the start, so you can act on the PDF in DEVONthink.

PDF Expert has no AppleScript support and the only thing one can get from the app via AppleScript is the window name. I don’t want to rely just on the filename and read about “lsof” last week or so. This was a happy accident :grin:

@pete31 By chance, does your script sometimes mistakenly open apps when it’s checking to see if they’re running?

The reason I ask is that I made a script that I run through Alfred and feed to other apps, like DEVONthink (which I think I might have mentioned to you previously) that has this problem. After restarting or shutting down my computer, the first time I run the script, it always does this - opening some of the apps that are not running, though it ultimately still grabs the correct file path, etc. After it’s run once, however, it works perfectly fine. It’s super annoying!

In any case, my code’s a little different from yours, so I was considering checking if the app is running by converting over to your approach, assuming it doesn’t have this problem. FWIW - While I’m no programmer, I think it’s related to the way that applescripts are compiled (whatever that means), which I’ve seen others complain about elsewhere.

If you’re curious, here’s how my script starts:

global frontApp, frontAppName, windowTitle
set windowTitle to ""
tell application "System Events"
	set frontApp to first application process whose frontmost is true
	set frontAppName to name of frontApp
end tell

if frontAppName is equal to "Adobe Illustrator" then ... [TONS of apps, etc.]

Thanks for your help!

Hi @pete31,

I’m trying to set up a way to work on the same PDF in Acrobat Pro 2020 and DevonThink, seemlessly opening the currently visible page in the other application via a keyboard maestro hotkey.

While modifying your script from this thread works to open the PDF page from DevonThink in Acrobat, I was not able to get the script above to work.

After replacing PDF Expert with Acrobat Pro 2020 and running the script via a keyboard maestro hotkey in Acrobat, nothing happens at all.

In case there is an easy solution here, your advice would be highly appreciated.

I don’t use Adobe but downloaded a demo. After spending far more time than I thought it would take I’m quite sure Adobe got a serious problem with AppleScript.

This is a vaild statement, I think:

tell application id "com.adobe.Acrobat.Pro"
	try
		set theWindows to windows
		if theWindows = {} then error "Please open a window"
		set theDocuments to documents
		
	on error error_message number error_number
		if the error_number is not -128 then display alert "Acrobat Pro" message error_message as warning
		return
	end try
end tell


This is what Adobe Acrobat retuns:

It’s of course possible I overlooked something but it seems it’s just not possible to get the path of Adobe Pros’s current tab. (I did try a lot more than what’s shown in the script - to no avail)

Thank you for your efforts! I’ve gained tremendous value from the scripts you’ve posted on these forums.

It’s a shame that Acrobat doesn’t support AppleScript.

I will try to hack together a workaround via keyboard maestro. Something like this:

  1. Get file name of open document in Acrobat and save as %filename% variable
  2. Get page number and save as %pagenumber% variable
  3. If a DT window with the file name is open, then switch to it and go to the page %pagenumber%
  4. Else search for the exact filename in the database, reveal it and go to the page %pagenumber%
1 Like

It’s possible, see Script: Open DEVONthink record for Adobe Acrobat Pro tab.

Thank you very much for letting me know that posting scripts is of value (I’m often not sure whether a script might be useful for others).

Your scripts are always helpful—at least for me—even if I don’t have an immediate use for them. I read them and squeeze out the relevant knowledge like juice from an orange. :grin: Thanks so much for all your contributions.

Stephen

1 Like