Creating new items in Devonthink from Hookmarks

This is a quick cross post of mine from the Hookmarks forum that some people may also find useful.

Edit - apologies for not being clear, this script is to be used within Hookmarks

I love the Hook to New feature in Hookmarks, as I can create linked support materials straight from a project in Omnifocus, avoiding a lot of manual steps. However, with Devonthink, if you create a new item and then have it imported into DT, it loses the hook (since Devonthink essentially makes a copy then destroys the original instead of just moving it).

There’s also the downside that you can only have one set of Hook to New scripts per app in Hookmarks (afaik) so it would only support one type of file anyway. I like to use mindmaps, markdowns, kanbans, spreadsheets and so on, according to my need.

A workaround I’ve come up with is to use Applescript to create a new item based on the templates folder in Devonthink. I’ve then populated my templates folder with all the various document types that I might want to link to Omnifocus.

When you run the Applescript it offers you a pop-up showing you your Templates. You then choose the relevant template and it will create the file, rename it and Hook it.

set thePath to "/Users/[Your User]/Library/Application Support/DEVONthink 3/Templates.noindex"
tell application "System Events"
	set templateList to {name, POSIX path} of every disk item of folder thePath
end tell

tell application id "DNtp"
	set theTitle to "$title"
	set theTemplate to choose from list templateList's item 2
	set newItem to import template (theTemplate as string) to incoming group of current database
	set name of newItem to theTitle
	set refURL to reference URL of newItem
	set itemPath to path of newItem
end tell
open location refURL
get refURL

Limitations

  • I have no idea what I’m doing with Applescript and just fudged my way through the above. If you, dear reader, can retype this script more succinctly, that would be very welcome.
  • Just shows you the path and filename of each template. A nicer list of templates would be great.
  • This doesn’t permit you to put any content within the file itself, as currently happens with the rtf and md files that Hook creates. No idea if that’s possible.
  • The list of templates won’t work recursively down into any sub-folders. This is possible, I just haven’t bothered to do it yet since I don’t use enough templates to make it worth it.

With thanks to this post on the DT forum Creating new file from template folder by AppleScript - Automation - DEVONtechnologies Community

1 Like

I don’t understand what the get refURL at the end should do. Nor why you set an itemPath that you never use. Or are these variables used by Hook?

As to the set thePath: There are ways to set the path in a more flexible way using Standard Additions. Something like
set thePath to POSIX path of (path to application support from user domain) & "DEVONthink 3/Templates.noindex"
Looks ugly because of the stupid POSIX path thingy, but this method avoids changes in the script.

As to the templates: If I understand it correctly, the template folder contains not only template files but also subfolders. Your list displays both side by side (and .DSStore, which is neither). I have no idea what happens when a user selects a subfolder, though. To make all that look “nicer”, I’d omit the leading path (i.e. the thePath part of the template names) and drill down into the folders. But that would require more code, more trouble with displaying the results, and more work to find the correct template from the user’s selection. Probably not worth it.

Apologies my bad, I edited the original post for not being clear. This script is to be used within Hookmarks. The refURL is to tell Hookmarks the relevant location to create the hook I believe.

The itemPath was just a hangover from a previous attempt at making it work, it can be deleted freely. As I say I’ve never done any scripting, just pieced it together from other stuff I saw on the forum.