New free software for writing DEVONthink item links into Finder comments

If you often open files in external programs (for example, to edit Markdown or OmniOutliner documents in other programs), you may sometimes want to figure out the corresponding document in DEVONthink. This may not be easy if you have a lot of documents open in an external editor and the file names aren’t enough to tell you the databases and groups in which they’re filed.

My approach to solving this has been to write the DEVONthink item links into the Finder comments of certain kinds of file, under control of a DEVONthink smart rule. The idea is simple: outside of DEVONthink, read the Finder comment to get the x-devonthink-item URI of the document and then use that to jump to the document in DEVONthink.

Implementing this turned out to be not quite as easy as I hoped, because of an issue discussed in another thread and mentioned in past postings by DEVONthink’s staff: DEVONthink does not sync the metadata to the file unless it’s an indexed file or you export the document. Thus, it doesn’t work to have a smart rule that simply writes the item link into the comment in DEVONthink; to make it visible outside of DEVONthink, you need to use an external program to write the Finder comment of the file itself.

One thing led to another, and I ended up writing a more general tool that offers not just the ability to write URIs into Finder comments, but does it intelligently, offers different options for handling existing Finder comment text, and offers a way to print the URIs in Finder comments too. The tool is Urial (URI Annotation tooL) and is available from PyPI and GitHub:

It’s free and released under a BSD-type open-source license.

10 Likes

I use an applescript for this part
The following is a handler in my Process-Inbox script

on setComment(theNote)
	tell application id "DNtp"
		set comment of theNote to comment of theNote & reference URL of theNote
		repeat with theChild in children of theNote
			my setComment(theChild)
		end repeat
	end tell
end setComment

Sadly, setting the comment that way only works for indexed files. If it’s a file in DEVONthink’s database, then as explained by @cgrunenberg in the linked-to discussion, the comment is not written to the file, and thus not visible outside DEVONthink.

Acknowledged, and thank you for your application to address this

1 Like

I’m curious about the repeat-with-children-of-the-note part of your script. What’s the situation this covers? Is it if you drag or import a folder of files?

Correct; recursive processing to handle groups

1 Like

Thanks for sharing, @mhucka.

I usually run a little script using Alfred File Actions to do the same. For instance, I have a simple workflow that filters PDF files (or OO files, and so on), then I hit a shortcut (⌘⌥\) and choose Open File in DEVONthink. Alfred will pass on the path to the script, then it opens in DT3.

Applescript
on run
	tell application id "DNtp"
		set PassedPath to "{query}"
		
		-- Method 1: search using the path
		set PathToSearch to my fixPath(PassedPath)
		set theRecords to search "path:" & PathToSearch
		
		
		-- Fallback method: search using the name
		if theRecords is {} then
			tell application "Finder" to set theName to name of (POSIX file PassedPath as alias)
			set theRecords to search "name:" & theName
		end if
		
		if theRecords is {} then error "Nothing found. Go figure..."
		
		set theRecord to item 1 of theRecords
		set theURL to reference URL of theRecord
		tell me to do shell script "open " & theURL
		
	end tell
end run



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


on fixPath(thePath)
	set HomePath to (POSIX path of (path to home folder))
	set thePath to replaceText(thePath, HomePath, "~/")
	return thePath
end fixPath

Here are the file actions, in case someone feels inclined to give this a try.

DEVONthink File Actions.alfredworkflow.zip (50,1,KB)

Thanks for that code and description, @Bernardo_V

Searching in DEVONthink by the full path to get the corresponding DEVONthink document is a great idea. This approach should be adaptable to invoking from Keyboard Maestro, too.

I don’t know how Alfred gets the file name from the front application, but I would guess it uses system events or AppleScript actions, correct? One of the applications I use most does not support events or AS, so it wouldn’t work for that case, but it’s a fringe case and for most applications it ought to work.

The approaches also could be combined. A script could first check if a file’s comments has a x-devonthink-item URI and open it, else try to get the full path of the file and search in DEVONthink using that, else try another fallback method.

You’re welcome, @mhucka.

What you described sounds like a good combination of the two methods.

In the context I am using the script, it is actually not getting the path from the front application. The Alfred workflow is a file filter; that is, a filter targeted at specific folders and/or specific file types. When I run it, Alfred shows me the files matching the query and, when I choose a file, it already has the path to pass along to the file action.

But that is not to say we can’t get it done using Applescript. @pete31 was kind enough to build a library for this very purpose (see below).

Could you share the name of this app you mentioned? Perhaps there is something that could be done.

Actually, this is not an idea we advocate as the file path can change.

I can see the reason for this, but, specifically in this context, I think it shouldn’t be a problem: it is unlikely the path will change in the split second it takes for the script to run. Also, it is merely being used to locate a record with greater precision, but the filename could just as well be used instead.

For any readers that may use Urial, this is just a quick note that I’ve released a new version (1.1.0) with a minor feature enhancement:

1 Like