Getting last edited Draft via URL Scheme

Note that query() is a static method, and so it should be Draft.query, not draft.query.

I agree that this would be a good way to find the last modified draft, and that a URL scheme call is probably the best way to go at the moment given the constraints on AppleScript.

Here’s a quick function based on updating the last modified draft if one is available.

function appendToLastModifiedDraft(p_strTextToAppend)
{
	let adraftModified = Draft.query("", "all", [], [], "modified", true);
	if (adraftModified.length > 0)
	{
		adraftModified[0].append(p_strTextToAppend);
		adraftModified[0].update();
		return true;
	}
	else 
	{
		app.displayErrorMessage("No drafts found");
		return false;
	}
}

Here’s an action that utilises it to append the current draft content to the last modified content.

To make use of this from Alfred, it is just a two step workflow. Here I selected a hotkey trigger (purposefully an awkward one to ensure it didn’t clash with any of my existing ones :laughing: ).

2020-11-07-09.45.11

The hotkey passes in the selected text.

2020-11-07-09.45.55

And the URL executed looks like this:

drafts://x-callback-url/runAction?text={query}&action=Append%20Draft%20to%20Last%20Modified

2020-11-07-09.47.25

You can download the Alfred Workflow here, and do modify that shortcut key combination if you do :wink:

Hope that helps.

4 Likes