Creating a linked Zettel/item in a specific database

I trying to create a linked Zettel/item from an existing item in a specific database (ZettelkastenDB) with a template script. I think I am close to the solution but I am missing something as I can’t create the new item in the right location.

The target location structure is ZettelkastenDB/Zettelkasten/YYYY/MM

The important stuff is at the bottom. Thanks for the help.

Here is the script:
property pTemplateName : “Document”

tell application id “DNtp”

activate

set sourceName to name of content record

set sourceURL to reference URL of content record

set titleDateString to do shell script “date +%y%m%d%H%M”

set theName to (display name editor default answer pTemplateName info “Please enter a name for this document:”)

set theName to titleDateString & " " & theName

– display dialog theName

set theTemplateFile to (path to me as string) & “Contents:Resources:%myDoc%.md”

– display dialog theTemplateFile

set thePlaceholders to {|%myDoc%|:theName}

set the clipboard to ("[[" & sourceName & “]](” & sourceURL & “)”)

– set theRecord to import theTemplateFile placeholders thePlaceholders to the current group

– new Zettel to be stored in ZettelkastenDB/Zettelkasten/YYYY/MM

– Format the month number

set theDate to current date

set numMonth to (month of theDate as integer) as string

if the (length of numMonth) < 2 then set numMonth to “0” & numMonth

– Format the year

set theYear to year of theDate as string

– set theRecord to import theTemplateFile placeholders thePlaceholders to “/” & theYear & “/” & numMonth

if (exists database “ZettelkastenDB”) then

set theDatabase to open “/Users/manfred/DEVONthink/ZettelkastenDB.dtBase2”

set theLocation to create location “/Zettelkasten” & “/” & theYear & “/” & numMonth in theDatabase

– repeat with thisItem in theRecord

– set theResult to move record thisItem to theLocation

– end repeat

end if

set theRecord to import theTemplateFile placeholders thePlaceholders to the theLocation

end tell

That won’t work. DEVONthink doesn’t know about a database if it isn’t already open.


Please edit your post and wrap the script in three backticks, like this

```
script

```

Note: You have to paste it again as plain text as the previous version without backticks is messed up.

Thanks.

Thanks for the help. Here is the script again:

property pTemplateName : "Document"


tell application id "DNtp"
	activate
	
	set sourceName to name of content record
	set sourceURL to reference URL of content record
	
	set titleDateString to do shell script "date +%y%m%d%H%M"
	
	set theName to (display name editor default answer pTemplateName info "Please enter a name for this document:")
	set theName to titleDateString & " " & theName
	
	-- display dialog theName
	
	set theTemplateFile to (path to me as string) & "Contents:Resources:%myDoc%.md"
	
	-- display dialog theTemplateFile
	
	set thePlaceholders to {|%myDoc%|:theName}
	
	set the clipboard to ("[[" & sourceName & "]](" & sourceURL & ")")
	
	
	-- new Zettel to be stored in ZettelkastenDB/Zettelkasten/YYYY/MM
	
	-- Format the month number
	set theDate to current date
	set numMonth to (month of theDate as integer) as string
	if the (length of numMonth) < 2 then set numMonth to "0" & numMonth
	
	-- Format the year
	set theYear to year of theDate as string
	
	if (exists database "ZettelkastenDB") then
		set theDatabase to "ZettelkastenDB"
		set theLocation to create location "/Zettelkasten" & "/" & theYear & "/" & numMonth in theDatabase

	end if
	set theRecord to import theTemplateFile placeholders thePlaceholders to the theLocation
	
end tell

The new Zettel/item is created inside the global inbox where the original/referenced item resides. What is missing is the creation in the new location ZettelkastenDB/Zettelkasten/YYYY/MM or second best option a move of the item to that location.

Thanks for the reply but it didn’t do the job. The document is created in the current group no matter which database. I assume the part about setting the database and the location is not working.

I tested it on global inbox and ZettelkastenDB items the result is still that the new item is created in the current group and not created in the target location. ZettelkastenDB exists and was open at both tests.

How do I create a new item in a specific database and a specific group no matter in which database I am in? That’s my problem here.

I have seen posts about selecting location and db. So I hope there is a way to do it the hard coded way as well.

After some playing around I got it to work under the condition that the database is open.

Below is the working code.

There is still one issue. It would be better to make sure that the database is open of course. If I replace “ZettelkastenDB” with theDatabase in the second line, the script won’t work. I have no idea why the replacement of the value with the variable makes a difference here.

	set theDatabase to open database "/Users/manfred/DEVONthink/ZettelkastenDB.dtBase2"
	set theLocation to create location "/Zettelkasten" & "/" & theYear & "/" & numMonth in database "ZettelkastenDB"
	set theRecord to import theTemplateFile placeholders thePlaceholders to the theLocation

Use name of theDatabase. A database (like any other record) got many different properties. You have to tell DEVONthink which property to use.

I see now why I didn’t spot the error earlier:

You used variable theDatabase for a literal string while I always use variable theDatabase for a database reference (and theDatabase_Name or theDatabaseName for its name). And you’ve used theLocation for a group reference while I always use this for a literal string. So your variable usage and mine are somehow upside down … which I obviously couldn’t handle. That’s of course ok, just mentioning it because I’ve found that (for me) it’s very important to

  • be as precise as possible in variable naming
    (i.e. a variable should tell you what it contains)
  • use variables that are widely used
    (e.g. theGroup for a group reference, theRecord, etc.)

Hi pete31 thank you so much for your patience and help. This solved the problem. As I am starting with AppleScript, I am keen to learn about how to use it with DTP.
I assume there isn’t a reference for widely used variables as this is something you learn on the job but do you have some links which might be helpful to learn scripting for DTP.

Your help is very much appreciated. Thanks.

Check out the Automation chapter of the Help and use File > Open Dictionary in Script Editor to browse an AppleScript-aware application’s classes, commands, and properties.

1 Like

Thanks. Will look into it. Much appreciated.