AppleScript to erase and replace text file content

I’ve been plodding around trying to figure this out on my own, so thought I’d ask here, given my limited AppleScript know-how –

Is there an AppleScript way to select a specific markdown text file in DEVONthink (the markdown file’s item link/ID never changes), clear the contents, and add new content (the current date at the top, in the form 2024-02-10, followed by a blank line)?

(/TLDR background – I have a “daily notes” system with a daily note always named “TODAY-W”, and the item link/ID never changes, for easier automation. I use a Smart Rule, triggered by a reminder at the end of the day, to duplicate this daily note and move the duplicate to an archive folder, where another smart rule renames it with the proper date. The final piece of the puzzle for me would be clearing the contents of the TODAY-W note, and putting the date (e.g., 2024-02-10) at the top, followed by a blank line, so I have a clean slate waiting for me to start each day. (I was doing all this with indexed files previously, using other methods, but sync got too messy.)

Thank you for any help.

P.S. I’ve fairly recently found that DEVONthink makes a great writing environment, after years of mostly using it as a repository for my files/data.

1 Like

Perhaps a template would be a better tool for that? Just wondering, I have no experience with them.

Yes, the contents can be accessed and updated using Applescript
From Devonthink’s script dictionary; record properties include

The script would be something like this

tell application id "DNtp" --------------------------Retrieve the DT data
	set theDailyNote to (get record with uuid "0538FAFE-BCDE-448C-8BDE-13E875EF34B0")
	set theNewContents to "..."
	set plain text of theDailyNote to theNewContents
end tell
1 Like

Agreed, as I have been using it for writing lo these past 11.5 years :smiley:

It’s unclear why you feel the item link being preserved is important when you are using a strictly confirming filename that very simply could be matched with a smart rule and other criteria, e.g.,…

I agree with @chrillek’s suggestion, and here is a simple template that fills the bill IMHO…

…yielding, this document, empty (except for the date) and ready to edit… (1.6 KB)

Thanks so much! That worked perfectly. To insert the date at the start, here was the final AppleScript:

set date_ to do shell script "date +'%Y-%m-%d'"
tell application id "DNtp" --------------------------Retrieve the DT data
	set theDailyNote to (get record with uuid "8B2C1362-D4F5-4BD2-806D-7C7DD4F527BA")
	set theNewContents to date_
	set plain text of theDailyNote to theNewContents
end tell

Back when I first was doing all this with indexed files, I found that trying to name match with Shortcuts on iOS was not reliable for me. Perhaps (maybe probably) it was user error, but the Shortcut would wait and wait at that step, and at times never find the correct file. (I have a few Shortcuts tied to the item link, such as one that pops up a text input field, and appends the input to my daily note. I found that by having the daily note always have the same ID, it worked 100% of the time and was very fast. CORRECTION: I just checked my shortcuts, and they have the exact file selected, by name. I found that selecting the exact file worked, while name matching by part of the name was hit or miss (again, possibly by user error)).