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.