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. Using AppleScript from Drafts
    1. The “Run AppleScript” Action Step
    2. The AppleScript Script Object
    3. Example Action Group
  2. Using Drafts from AppleScript
    1. AppleScript Syntax Examples
      1. Create New Draft
      2. Query Drafts
      3. Getting Draft Properties
      4. Updating Drafts
      5. Workspaces
      6. Running Actions

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);
}

Example Action Group

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

Using Drafts from AppleScript

Drafts supports creating, updating, and querying your Drafts library from AppleScript. Some of the capabilities available include:

  • Drafts
    • Create new drafts
    • Update content and properties (flagged status, tags, folder, etc.) in existing drafts
    • Query drafts
    • Get current draft from the editor
  • Workspaces
    • Query workspaces
    • Load drafts from any workspace
    • Get current workspace
  • Actions
    • Query actions
    • Run actions on drafts or text

AppleScript Syntax Examples

Below are some syntax examples to help get you started. Refer to the app’s scripting dictionary in ScriptEditor for more details.

Create New Draft

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

Query Drafts

tell application "Drafts"
	set myDrafts to every draft whose tags contains "personal"
	set myDrafts to every draft whose folder is equal to inbox and flagged is equal to true
	set myDrafts to every draft whose content contains "Hello"
	set myDraft to draft id "4A376C15-73B4-48DE-9C7B-1BD5FF9C65D9"
end tell

Getting Draft Properties

tell application "Drafts"
	set myDraft to current draft
	set myContent to content of myDraft
	set myTags to tags of myDraft
end tell

Updating Drafts

tell application "Drafts"
	set myDraft to draft id "4A376C15-73B4-48DE-9C7B-1BD5FF9C65D9"
	set content of myDraft to "New Content"
	set folder of myDraft to archive
	set tags of myDraft to {"red", "purple"}
end tell

Workspaces

tell application "Drafts"
	get workspaces
	set myWorkspace to workspace "Work"
	every draft of workspace "Work" whose folder is inbox
	set myWorkspace to current workspace
	set myDrafts to every draft of current workspace whose folder is inbox
end tell

Running Actions

tell application "Drafts"
	set myAction to action "Copy"
	set myDraft to draft id "4A376C15-73B4-48DE-9C7B-1BD5FF9C65D9"
	perform action myAction on draft myDraft
end tell

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

Mastodon