Applescript to open "path/file" in DEVONthink?

What you have in mind is a very good idea not only because I had the same some time ago.

I’ve written a handler which returns the path of the front document of every app it knows. With this it’s super simple to open a file’s DEVONthink record from everywhere.

  • Get path of front document
  • Lookup path in DEVONthink
  • Open DEVONthink record

If you save the handlers as scripts and put them in /Users/username/Library/Script Libraries/ then you can call them from any script like so

set thePath to script "Path to front document"'s path_to_front_document()

set theResults to script "Lookup DEVONthink"'s lookup_path(thePath)

tell application id "DNtp"
	try
		open window for record (item 1 of theResults)
		activate
		
	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

Here are the handlers (click)

Path to front document

Also linked above A script library to get the path of the frontmost document. You can see in this handler that I started to write it for BBEdit.

Lookup path in DEVONthink
on lookup_path(thePath)
	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 Handler"
				return
			end if
			
			return theResults
			
		on error error_message number error_number
			if the error_number is not -128 then display alert "DEVONthink" message error_message as warning
		end try
	end tell
end lookup_path

There’s also a script to open records from within PDF Expert which is not included in the linked handler.

If you just want to open records from within BBEdit you can use this

-- Open DEVONthink record of BBEdit document 1

tell application "BBEdit"
	set myFile to get file of front document of window 1
end tell

set thePath to POSIX path of myFile

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 "DEVONthink"
			return
		end if
		
		open window for record (item 1 of theResults)
		activate
		
	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

Note that there might be cases where a lookup path returns more than one path (I think I’ve read this happens if you’ve indexed a file in different databases). If this happens item 1 of theResults might not return the record you want to open.