Using AppleScript to append to Draft

I’m currently trying to write an AppleScript to append things to the end of a draft.

What I can’t figure out is if its possible to actual append to a draft. Every attempt I try in bringing data into drafts it creates a new one. Can someone please help point me in the right direction in this?

That is not possible at this time. AppleScript only supports creation of new drafts (docs).

Extended AppleScript support for other functions is something on the list, but can’t promise a timeframe for implementation. For now, the /append URL scheme is the only way to accomplish that on Mac (Shortcuts can be used on iOS).

1 Like

Thank you.

I think I devised a way to do what I want with the URL scheme. Two questions.

  1. Does anyone know how to not have to answer this every time I use an URL scheme?

  2. Is there a way to close the Drafts window without actually quitting the application. Right now I’ve been trying to add it to my AppleScript and I can’t close the window, only quit the application.
    tell application "Drafts" to close every window returns with the following error message: error "Drafts got an error: every window doesn’t understand the “close” message." number -1708 from every window whenever I try and run it.
    I’ve tested this with other apps without AppleScript support, in this case Slack, and it does close their windows.

1 Like

If anyone is curious here is the script I developed to do this with AppleScript.

AppendToDrafts

-- Create a markdown style link from a Safari tab and append it to a Drafts.app list

tell application "Safari"
	set siteURL to URL of front document
	set siteName to name of front document
	set markdownLink to "- [" & siteName & "](" & siteURL & ")" as string
	-- Replace UUID-TO-VALID-DRAFT with drafts UUID
	set draftsAppend to "drafts://x-callback-url/append?uuid=UUID-TO-VALID-DRAFT&text=" & markdownLink as string
	tell window 1
		set current tab to (make new tab with properties {URL:draftsAppend})
		-- At this point you will click `Allow` when asked `Do you want to allow this page to open "Drafts" before the delay expires and closes tab?
		delay 3
		close current tab
	end tell
end tell

-- Quits Drafts.app so it is not in the forefront
tell application "Drafts" to quit

Initially, I tnought that you might be able to invert the logic: start from a Drafts document and retrieve the URL and name from the current topmost Safari window with a JavaScript action. However, it seems that JXA’s global Application object is not available in Drafts. Maybe it’s possible with an AppleScript from within Drafts?

There’s an example here: https://actions.getdrafts.com/a/16h

Interesting, I’ll check that out. My thought process with this was I was in Safari and wanted to quickly record things while in it. The current use case is creating a list of things for Christmas.