Using 'import template' for single template

Hi,

I want to adapt the following script used in Omnifocus to create a folder in DT3 from a template chosen in a dialogue. The folder has the name of the OF project and the tag ‘Hookmark’.

set thePath to "/Users/david/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 theFilingGroup to get record at "/1 Projects" in database "Project Support"
	set newItem to import template (theTemplate as string) to theFilingGroup
	set name of newItem to theTitle
	set theTag to "Hookmark"
	set tags of newItem to theTag
	set refURL to reference URL of newItem
	set itemPath to path of newItem
end tell
open location refURL
get refURL

This seems to work well, but as I only want to use the same template every time, I’d like to remove the block listing all the available templates and simply code the required template directly in. The template’s path is ~/Library/Application Support/DEVONthink 3/Templates.noindex/New project.dtTemplate.

What’s the simplest method of doing this, please?

I thought that as the original code seems to be using a string (theTemplate) I could remove the preliminary code and just hard code the path of the template in.

set newItem to import template "/Users/<me>/Library/Application Support/DEVONthink 3/Templates.noindex/New Project.dtTemplate" to theFilingGroup

It doesn’t work, though, and OF’s script window doesn’t give any feedback.

What am I doing wrong, please?

Many thanks

The complete source of your script would be useful.

and OF’s script window

? OF’s script window ?

Sorry, Hookmark’s script window :wink:

Hi Christian,

That is the complete script :grinning:.

You enter it into the Hookmark Preferences > Script ‘New Item’ panel for Devonthink as a replacement for the default script, so presumably Hookmark adds the handlers behind the scenes.

The first screenshot script works:

My attempt to simplify the code to a single template doesn’t:

Cheers!

Do you really have <me> in your path in the script or did you put it there for “privacy”? And did you try running the script in Script Editor? That’s usually the first place to go if something is not working as desired – there, you can follow script execution and see what goes wrong.

Aside: It would be more helpful to post the script code as code, instead of a screenshot.

You removed also this line…

set theFilingGroup to get record at "/1 Projects" in database "Project Support"

…but theFilingGroup is used afterwards.

Sorry, that’s an editing error from trying to recreate the screenshot for you in too much haste…

The actual code which doesn’t work is:

tell application id "DNtp"
	set theTitle to "$title"
	set theFilingGroup to get record at "/1 Projects" in database "Project Support"
	
set newItem to import template "/Users/<me>/Library/Application Support/DEVONthink 3/Templates.noindex/New Project.dtTemplate" to theFilingGroup
	set name of newItem to theTitle
	set theTag to "Hookmark"
	set tags of newItem to theTag
	set refURL to reference URL of newItem
	set itemPath to path of newItem
end tell
open location refURL
get refURL

Thanks.

This should be your username of course.

Yes, I changed the name in the screenshot!

I changed it for the screenshot: the actual version has my username… Is the syntax correct other than that, or am I misunderstanding how import template should work?

Have you actually tried this in Script Editor?

And yes, it works with or without using tilde expansion.

But why is the path of the newly created record empty?

Trying it in ScriptEditor would mean I’d have to work out what to do with $title and get $RefURL. Much simpler to take the version that works in Hookmark and adapt it…

But thanks for the confirmation of the exact syntax: I appreciate your help!

1 Like

Nope. If everything runs fine up to this point, i.e. up to and including the end tell, then you know that the part concerning DT is working ok. And Script Editor is the only tool pre-installed that you can use to debug AppleScript.

You could, if you’d like to, print out refURL, just to make sure that it’s fine.

After end tell, it’s Hookmarks job.

That’s the sort of thing that’s obvious to the skilled and experienced, and not at all clear to anyone else, I’m afraid. I shall know better next time, if there is one.

Thanks!

Because my template was a group. Groups don’t exist so there’s no path. Here’s one with a document-based template…