AppleScript Daily Note Markdown error: Can't get every text of content

Hi, I am modifying the template AppleScript for Daily Notes from Template Extras. The goal is to create/edit markdown file with format:

filename: 2020-10-13.md

# Tuesday, 13 October 2020

## 14:56
Note content

## 15:02
...

My script below manage to create new markdown file, but it always show error:

An error occured when adding the document. DEVONthink 3 got an error: Can’t get every text of content id 90087 of database id 2.

my modified script is:

tell application id "DNtp"
	try
		activate
		set myGroup to create location "/Journals"
		set recordName to theYear & "-" & numMonth & "-" & theDay
		if not (exists child recordName of myGroup) then -- Create the document from scratch
			set myRecord to create record with {name:recordName, type:markdown, content:"", tags:theYear & "," & theMonth} in myGroup
			
			tell text of myRecord
				set theHeadline to ("#" & space & longWeekday & "," & space & shortDay & space & theMonth & space & theYear)
				make paragraph at beginning with data (theHeadline & return)
			end tell
			
		else -- Record already exists, just add new weather/time header
			set myRecord to child recordName in myGroup
		end if
		
		tell text of myRecord
			display alert (localized string "Debug1")
			set the last character to the last character & return & return
			display alert (localized string "Debug2")
			make new paragraph at end with data (("##" & space & theTime & return) as string)
			display alert (localized string "Debug3")
			set last character to last character & ""
			display alert (localized string "Debug4")
		end tell
		
		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

It seems the syntax for rtf and markdown is different?
What seems to be the error, and also, where can I find the syntax reference for the AppleScript on DEVONthink?
Thanks in advance :slight_smile:

That’s correct, only rich texts support scripting of its contents (see tell text of myRecord blocks). In case of Markdown or plain text document you could simplify specify the desired content (see content property of create record command)

Thank you for the response.

by that, the creation line should be something like:

set theHeadline to ("#" & space & longWeekday & "," & space & shortDay & space & theMonth & space & theYear & return & return & "## " & theTime)
set myRecord to create record with {name:recordName, type:markdown, content:theHeadline, tags:theYear & "," & theMonth} in myGroup

what about if the file already exist? will the flow be like:

  • get content of existing file, set to var
  • set new content with var & return & return & "## " & theTime & return
  • “replace”? or “create” new file and delete the old one?

In this case get the plain text of the Markdown document, update it, then set the plain text again.

1 Like

I finally get the script working, with the help of (also your post!) this comment:

for those with the same goal as me, my working script is:

(*
	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 and styles updated by Eric Böhnisch-Volkmann June 28, 2019
	Modified for Markdown by ybbond October 13, 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

-- Retrieve the user's locale so that we can e.g. get localized quotes and headlines
set theLocale to user locale of (get system info)
if the (length of theLocale > 2) then
	set theLocale to (characters 1 through 2 of theLocale) as string
end if

-- 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
set daySuffix to ""
if theLocale is not "de" then
	set suffixList to {"st", "nd", "rd"}
	set theIndex to last character of theDay as integer
	if (theIndex > 0) and (theIndex < 4) and the first character of theDay is not "1" then
		set daySuffix to item theIndex of suffixList
	else
		set daySuffix to "th"
	end if
end if

-- 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 shortWeekday to characters 1 thru 3 of longWeekday

tell application id "DNtp"
	try
		activate
		set myGroup to create location "/Journals"
		set recordName to theYear & "-" & numMonth & "-" & theDay
		if not (exists child recordName of myGroup) then -- Create the document from scratch
			set theHeadline to ("#" & space & longWeekday & "," & space & shortDay & space & theMonth & space & theYear & return & return & "## " & theTime)
			set myRecord to create record with {name:recordName, type:markdown, content:theHeadline, tags:theYear & "," & theMonth} in myGroup
			
		else -- Record already exists, just add new weather/time header
			set myRecord to child recordName in myGroup
			set plain text of myRecord to (plain text of myRecord) & return & return & "## " & theTime & return
		end if
		
		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

For the next goal, including the day’s weather forecast and current weather from Weather report: Ashburn, Virginia, United States to the file.

Thank you for your help @cgrunenberg !

2 Likes

You’re welcome.

I’m Very interested in doing the same kind of thing, especially with the weather addition. Would you be willing to share your work?

this far, I only manually add the weather from http://wttr.in/?FT1q (the F, T 1 and q url params can be read on http://wttr.in/:help). I then give 5 spaces before the region name and 2 spaces before the weather ASCII image to make them quote


I use the CSS for markdown from [[A CSS to embellish *MultiMarkdown* rendering]]

I might improve the above AppleScript sometime later, but currently quite busy myself. Do let me know if you’ve found the solution yourself! I will do the same :bowing_man:t2: