Applescript to open "path/file" in DEVONthink?

Hello experts. I have found in the forum an applescript that can be used to open the record with a particular UUID in DEVONthink. I would like to do the same thing, but starting with the full path to the file instead of the UUID.

My use case: I usually edit my markdown files in BBEdit, but have the rendered file open in DEVONthink at the same time. It works pretty well for me, but I will often change the document in one program or the other and eventually want to have them both showing the same file again.

When I’m in DEVONthink and showing an MD render, it’s simple enough to ⇧⌘-O and the file opens in BBEdit. But when I’m in BBEdit all I have to work from is the path to the file I’m editing. For some reason I am missing something simple and obvious because I can’t make this work in applescript. Help? (and thanks!)

??

It’s unclear what you’re asking.
Did you open a file in BBEdit from within DEVONthink, navigate elsewhere in DEVONthink, and now want the BBEdit file to be the active one in DEVONthink?

Hey Blue, simpler explanation: I have a document open in BBEdit. It is also a note from DEVONthink, but not open in DEVONthink at the moment. Looking at the raw MD in BBEdit, I want to fire a script to take the full pathname of that file and cause DEVONthink to open it in a window.

I’ve seen it’s possible with UUID, but I don’t have that in BBEdit. Thanks.

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.

Hey, that seems to work perfectly! (And the same trick for PDF Expert is bonus! I can use that too.)

Thanks very much Pete!

Even in one database. The file will remain the same except most of metadata. It’s a nice feature if you want to have one file with different metadata sets.

set myFile to get file of front document of window 1 may not be the case, it may be Window 2 or 3. So, set theFile to file of (get container of (get selection)) may be more reliable. And it may be not saved to disk and you can get an error, so it’s better to check it with on disk document property.