Link Search Menu Expand Document

AppleScript

iOS macOS

Mac Only

Apple’s AppleScript language and automation features are only available on the Mac version of Drafts.

Table of Contents

  1. Example Action Group
  2. Using AppleScript from Drafts
    1. The “Run AppleScript” Action Step
    2. The AppleScript Script Object
  3. Using Drafts from AppleScript

Example Action Group

To get started with some example actions, install the Examples (Mac): AppleScript & Shell Script action group.

Using AppleScript from Drafts

The “Run AppleScript” Action Step

The “Run AppleScript” action steps allows you to paste AppleScript (as text) into Drafts, and run that AppleScript, passing in information about the current draft as an AppleScript record.

Each AppleScript used should contain an execute subroutine, which takes a single parameters, which will contain a record of values about the current draft, including properties the following properties:

  • uuid (text)
  • content (text)
  • title (text, readonly)
  • tags (list of text)
  • flagged (boolean)
  • folderName (text)
  • languageGrammar (text)
  • createdAt (date)
  • modifiedAt (date)
  • createdLatitute (number)
  • createdLongitude (number)
  • modifiedLatitute (number)
  • modifiedLongitude (number)
  • permalink (string)

This example AppleScript is useful for debugging the contents of the draft record:

on execute(draft)
	set theText to ""
	
	set theText to theText & "UUID: " & uuid of the draft & return
	set theText to theText & "TITLE: " & title of the draft & return
	set theText to theText & "CR: " & createdAt of the draft & return
	set theText to theText & "MOD: " & modifiedAt of the draft & return
	set theText to theText & "F: " & folderName of the draft & return
	set theText to theText & "Flagged: " & flagged of the draft & return
	set theText to theText & "Syntax: " & languageGrammar of the draft & return
	set theText to theText & "Permalink: " & permalink of the draft & return
	display dialog theText
	
end execute

-- mock for testing in Script Editor
execute({title: "Title", content: "Content", ...})

It is recommended AppleScripts be developed and tested in Apple’s Script Editor application, and the final scripts copy and pasted in Drafts action editor.

If the AppleScript defined in the action step returns a result, it will be available to subsequent Script action steps via the context.appleScriptResponses array. Most basic data types, including lists and records, can be returned and will be converted to usuable values in JavaScript.

The AppleScript Script Object

For more advanced and customizable use of AppleScript, the AppleScript script object is available to JavaScript executing in a script step. More detailed documentation is coming - but here’s an example script:

let method = "execute";
let script = `on execute(bodyHTML)
	tell application "Safari"
		activate
	end tell
	return "Yeah!"
end execute`;

let html = draft.processTemplate("%%[[draft]]%%");

let runner = AppleScript.create(script);
if (runner.execute(method, [html])) {
	// the AppleScript ran without error
	// if the script returned a result, it's available...
	alert(runner.lastResult);
}
else {
	alert(runner.lastError);
}

Using Drafts from AppleScript

We are working on exposing more of Drafts features to AppleScript, but to get started implemented the ability to create drafts. This should be useful for incorporating capture with other automation tools. Example:

tell application "Drafts"
    make new draft with properties {content: "My Draft Content", flagged: false, tags: {"blue", "green"}}
end tell

Download on App Store Download on Mac App Store

© 2012-2020 by Agile Tortoise, Inc.
Drafts is a registered Trademark of Agile Tortoise, Inc.
Privacy | Terms