A weird behaviour when a markdown file is saved by AppleScript (Solved)

I am working on a script that requires saving a markdown/rich text file first before doing other kinds of stuff. There is a “save” command in the DT dictionary but it seems the command doesn’t work as expected? Thank you.

e.g.

tell application id "DNtp"
	set a to content record of think window 1
	save record a  -- gives me an invalid object reference
end tell

The save command is part of the Standard suite, not something we implemented independently. Typically the file specifier would be something like document 1.

I’m not sure what you’re up to but this code snippet should do what you mentioned…

tell application id "DNtp"
	set theRecord to content record
	set recordPath to (path of theRecord as string)
	save document 1 of think window 1 in recordPath
end tell
1 Like

Thank you very much!

No problem.

Just want to report a weird behaviour but I think it is not going to affect 99.99% of the normal scripting.

This is a simple markdown file. I type “Test 1” and save the file.

Then I run this simple script. The script is supposed to save the content record of the frontmost window, and display the text in an alert box. The script is working as expected in script editor or in script debugger.

tell application id "DNtp"
	set theDoc to content record of think window 1
	save document 1 of think window 1
	display alert (plain text of theDoc as text)
end tell

But if the script is placed in DT’s script folder and invoked by a shortcut, the alert box doesn’t shows the lastest content of the save file in the 1st run, it only shows the latest content in the 2nd run.

For example, if I type “Test 2” after “Test 1” and run the script (by a shortcut in DT’s script folder) before saving the file:

The first time I run the script, I will see this:

If I run the script again, then I will see this:

Originally, I thought “get plain text” may has a bit of delay, so I add a delay. But the issue persists. There is no need to take any action coz there are work arounds. I am just curious.

tell application id "DNtp"
	set theDoc to content record of think window 1
	save document 1 of think window 1
    Delay 0.3
	display alert (plain text of theDoc as text)
end tell

Thanks.

DEVONthink is not a document-based application, therefore the save command does not work and the alert just triggers the autosave due to the deactivation of the window. This line works:

	save think window 1

Wonderful! Thanks.