Help with smart template, new group, new md file, reminders

I would like to be able to use a smart template to:

  • in a specific group and always this same group in the same dB
  • ask for a new itemName
  • create a new group with that itemName
  • create a new markdown file with itemName
  • populate it with some basics (TOC, date, itemName, etc)
  • add some tags
  • run the existing script “Add as to do to reminders” on the new markdown file.

I started with the Daily Journal smart template, stripped out the date localization sections and have tried to modify it to my own ends but got a little into the weeds.
The ask-for-input text is working and I can create a new markdown doc with the right stuff but I can’t get it to create the group it should go into and to put it into that group.
Also haven’t got around to figuring out the reminder section.
Anyone with a machete care to help me hack my way back to the beaten path?
Thanks.

(*
	Based on script by Chuck Lane October 2, 2013
	https://discourse.devontechnologies.com/t/daily-journal-script/16509
	Updated and optimized for DEVONthink 3 by Christian Grunenberg April 30, 2019
	Localized by Eric Böhnisch-Volkmann June 28, 2019
	Revised for Markdown by Christian Grunenberg Oct 19, 2020
*)
-- Import helper library
tell application "Finder" to set pathToAdditions to ((path to application id "DNtp" as string) & "Contents:Resources:Template Script Additions.scpt") as alias
set helperLibrary to load script pathToAdditions

-- Format the time, strip out the seconds but keep the AM/PM indicator
set theDate to current date
set theTime to time string of theDate
if (theTime contains "AM" or theTime contains "PM") then
	if character 5 of theTime is ":" then
		set theTime to (characters 1 through 4 of theTime) & (characters 8 through 10 of theTime) as string
	else
		set theTime to (characters 1 through 5 of theTime) & (characters 9 through 11 of theTime) as string
	end if
else if character 5 of theTime is ":" then
	set theTime to (characters 1 through 4 of theTime)
else
	set theTime to (characters 1 through 5 of theTime)
end if

-- Format the month number
set numMonth to (month of theDate as integer) as string
if the (length of numMonth) < 2 then set numMonth to "0" & numMonth

-- Format the day, calculate suffix for English if needed
set theDay to day of theDate as string
set shortDay to theDay -- shortDay won't have a leading zero
if the (length of theDay) < 2 then set theDay to "0" & theDay

-- Format the year
set theYear to year of theDate as string

-- Format month and weekday names (localized)
set theMonth to month of theDate as string
set longWeekday to weekday of theDate as string

set theResponse to display dialog "What's the name of the new To-Do Item?" default answer "" with icon note buttons {"Cancel", "Continue"} default button "Continue"
-- {button returned:"Continue", text returned:" "}
set newToDoName to text returned of theResponse

tell application id "DNtp"
	try
		activate
		set groupToDo to get record with uuid "265FFD7A-B6F0-4ADA-B9BF-51C0269E53D1"
		set myGroup to create location groupToDo & "/" & newToDoName
		set theContent to "{{TOC}} " & return & "# " & newToDoName & return
		set myRecord to create record with {name:newToDoName, content:theContent, type:markdown, tags:"TO-DO List" & "," & "CGT2"} in myGroup
		set theContent to plain text of myRecord
		set plain text of myRecord to theContent & return & return & "## " & theTime & return & "- "
		
		open tab for record myRecord
	on error errMsg number errNum
		display alert (localized string "An error occured when adding the document.") & space & errMsg
	end try
end tell

Just change the UUID to the desired one, then this should work as desired:

set theDate to current date
set theTime to time string of theDate
if (theTime contains "AM" or theTime contains "PM") then
	if character 5 of theTime is ":" then
		set theTime to (characters 1 through 4 of theTime) & (characters 8 through 10 of theTime) as string
	else
		set theTime to (characters 1 through 5 of theTime) & (characters 9 through 11 of theTime) as string
	end if
else if character 5 of theTime is ":" then
	set theTime to (characters 1 through 4 of theTime)
else
	set theTime to (characters 1 through 5 of theTime)
end if

tell application id "DNtp"
	try
		activate
		set newToDoName to display name editor "New To-Do Item" info "Name:" default answer ""
		set groupToDo to get record with uuid "x-devonthink-item://EBBDB071-00F8-4631-9FFB-98ABEC0A9734"
		set myGroupLocation to (location of groupToDo) & "/" & (name of groupToDo) & "/" & newToDoName
		set myGroup to create location myGroupLocation in (database of groupToDo)
		set theContent to "{{TOC}} " & return & "# " & newToDoName & return & return & return & "## " & theTime & return & "- "
		set myRecord to create record with {name:newToDoName, content:theContent, type:markdown, tags:"TO-DO List" & "," & "CGT2"} in myGroup
		open tab for record myRecord
	on error errMsg number errNum
		if errNum is not -128 then display alert errMsg
	end try
end tell
1 Like

Oh my! worked right out of the gate.
I will have to make time to look more closely at what is going on there to see how I got lost. Probably a little too much looking at generic AppleScript help sites and not enough DevonThink specifics.
Thanks so much @cgrunenberg

1 Like

Now I get to see if I can reproduce this in an iOS shortcut! Wheeeeeeeee!

Edit

This is my first try.
Create ToDo Item and Reminder
I can’t figure out a way to programmatically get the URL from the new file that’s created so I manually copy the link while in DevonThink and then return to shortcuts and enter it on the second prompt.

https://www.icloud.com/shortcuts/2786a21da2724c6693acc55013797c0c

Now I’m going back to bed :sleeping:

1 Like

Perfect!
Sweet dreams @BLUEFROG

This is how it ended up:
Create DT ToDo Item and Reminder

1 Like