Creating a document via AppleScript from a template that's using %recordName%

Hello everyone -

I’ve been using DevonThink for a little while and I’m trying to branch into more custom automation. I’m a novice with AppleScript so this could be something simple. I’ve got a MarkDown template that I want to create a new record from. The template is pretty basic but it uses the placeholder %recordName%.

I’m creating a record from the template using AppleScript and the import template verb.

tell application id "DNtp"
	set templatePath to ("/Users/jefferysmith/Dropbox/DevonThink/Templates/Zettlekasten Note.md")
	set thisGroup to incoming group
	set thisText to ("Testing Milk")
	set theDate to do shell script "date +%Y%m%d%H%M%S"
	set itemName to (thisText & " - " & theDate)
	set theRecord to import template templatePath to thisGroup
	set name of theRecord to itemName
end tell

How would I set the name of theRecord during the import template? It seems like what’s happening is the record is getting created first and then the name is being set. But because the template has already been imported, the placeholder doesn’t seem to trigger. (I’m assuming it’s only on creation?)

Is there an example in the built-in scripts? I looked through them but didn’t see anything with import template. All the scripts appear to be compiled so grep is being foiled.

1 Like

the placeholder doesn’t seem to trigger.

What placeholder?

I’m seeing no issue with your code producing a new file from a template named Testing Milk - 20230320100732.rtf. That includes the resolution of a %shortDate% placeholder in the document itself…

Specifically the %recordName% place holder. What I’m noticing is that any placeholder that doesn’t rely on information about the record seems to work fine. So I’m wondering if I have a chicken and egg problem.

So I’m wondering if I have a chicken and egg problem.

Yes you do.
The record doesn’t yet exist, so there’s no value to apply to the placeholder.

You’re right. One possibility is to use the command import templatePath name itemName placeholders {|%recordName%|:itemName} to thisGroup instead of import template.

WOOOOT! That worked! Thanks @cgrunenberg !

Purely as a source for additional ideas, I wanted to mention this reply in another thread: Automatically inserting the title of a Markdown file inside the file itself: any way? - #4 by mhucka

1 Like