Error with Drafts for macOS and AppleScript

Hey all (but particularly @cgrunenberg and @BLUEFROG),

Drafts is getting AppleScript support! That’s the good news. The bad news is that something weird is happening with DT3’s AppleScript support when called from Drafts.

Here’s the script:

-- AppleScript content
on execute(draft)
	set theTitle to title of the draft
	set theContent to content of the draft
	set theLink to "drafts5://" & uuid of the draft
	
	tell application “DEVONthink 3"
		set theMD to theContent
		create record with {name:theTitle, type:markdown, content:theMD, URL:theLink} in the current group
	end tell
end execute

And here’s the error:

{
    NSAppleScriptErrorAppName = Drafts;
    NSAppleScriptErrorBriefMessage = "compilation error: Expected expression, property or key form, etc. but found unknown token. (-2741)";
    NSAppleScriptErrorMessage = "compilation error: Expected expression, property or key form, etc. but found unknown token. (-2741)";
    NSAppleScriptErrorNumber = 1;
    NSAppleScriptErrorRange = "NSRange: {0, 0}";
}

Greg (Draft’s dev) believes the issue is specifically with the create record command. Note that Drafts’ AppleScript support seems to work fine with most other applications, so we think something weird is happening on the DT3 side.

Thanks!

I don’t use Drafts but this seems to work as expected:

set theTitle to "Title"
set theContent to "Text"
set theLink to "drafts5://1-2-3-4"

tell application id "DNtp"
	set theMD to theContent
	create record with {name:theTitle, type:markdown, content:theMD, URL:theLink} in (root of inbox)
end tell

I’m assuming that the variable ‘draft’ is a reference to a Draft document.
Try enclosing those 1st 3 lines in a ‘tell Draft’ block.

Unfortunately, the “execute” command is necessary for Drafts’s in-built AppleScript support to function. See beta documentation here: https://docs.getdrafts.com/applescript

There’s definitely something about DT3 not liking the execute subroutine that Drafts uses. I can’t even get something simple like display group selector to work, even though I can get all sorts of AppleScripts working just fine outside of Drafts (including the create record with command).

@alastor933 and @cgrunenberg, new beta of Drafts works around this issue. This code now works as you’d expect:

-- AppleScript content
on execute (draft)

	set theTitle to title of the draft
	set theContent to content of the draft
	set theLink to permalink of the draft
	set theTags to tags of the draft as list
	
	tell application "DEVONthink 3"
		set theMD to theContent
		create record with {name:theTitle, type:markdown, content:theMD, URL:theLink, tags:theTags} in the (root of inbox)
	end tell

end execute

The issue had something to do with DT3 using older AppleScript compilation techniques (don’t ask me, I just use this stuff!) But I’m a happy camper now!

Just a curious question and definitely not trying to challenge the choice of app:

From a glimpse of Drafts website, the app seems to be a better version of Apple Notes app that works on both iOS and macOS. I am just wondering what’s the main reason not to use DT for note taking as well if DT is already ur data management tools?.

Thanks in advance.

Drafts is “where text starts.” It’s not really a replacement for DT3 or Notes. It’s also a much better text manipulation tool than either DT3 or Notes, with the ability to build arbitrary JavaScript-based scripts to perform all manner of actions. It’s amazing. Most used app by far across my Apple ecosystem (including the Watch app).

1 Like

I think @apoc527 says it well. Myself I use the native notetaker in DEVONthink 3 now and find it wonderful and sufficient. I know it doesn’t have the power of Drafts. I use Drafts and Ulysses. I don’t write and compost text from with DEVONthink 3 at all. I access notes I have there all the time though.

A lot of my actual writing is done in Ulysses now though. I use the DEVONthink 3 notetaker as that, pure and simple: it already has some Markdown of course even then. In fact I have nothing now which gets saved as simple plain text. If that makes sense? In my workflows Markdown is plain text. I have to experiment more with Drafts as a writing app I think on the basis of what @apoc527 says.

Thanks for the explanation.