Doctor Drafts in Your Scripts

You may have heard of Doctor Drafts. It is my Alfred workflow for working with the app Drafts on the Mac. It has proven hugely popular, and I continue to expand and enhance the workflow as time permits. But, Doctor Drafts is already quite expansive, and more capable than people realise. While I’ve expounded in the past on how it is designed so that others can build their own custom workflows on top of Doctor Drafts, most people don’t realise that this extends beyond Alfred too. In this post, I am going to walk through how you can leverage Doctor Drafts functionality in your own scripts.

The Basic Premise

Within Doctor Drafts, I made efforts to make as much as I could accessible beyond simple calls within Alfred from the flows that I created. I did this by trying to encapsulate as much functionality as possible into callable units.

In Alfred terms, these are flows that can be triggered by calling an external trigger. Up to version 4 of Alfred, this has been possible via AppleScript, but with version 5, the option to trigger with a URL also became available. I have enabled support for this wherever it made sense.

In addition, I recently updated the flow documentation on the Doctor Drafts website to provide example code and URLs to call the various Doctor Drafts external triggers.

A URL Call Example

In the vast majority of cases, you will find that the standard Drafts URL scheme will cover the use of URLs and you should not need to go via the Alfred workflow. However, just for the purposes of illustration, here is a shell script that URL encodes any text on the clipboard and opens Drafts (via Alfred and Doctor Drafts) to search for the text.

#!/bin/zsh

urlencode()
{
	setopt localoptions extendedglob
	input=( ${(s::)1} )
	print ${(j::)input/(#b)([^A-Za-z0-9_.\!~*\'\(\)-])/%${(l:2::0:)$(([##16]#match))}}
}

alfredurl="alfred://runtrigger/com.thoughtasylum.doctordrafts/drsearch/?argument=$(urlencode "$(pbpaste)")"
open $alfredurl

An AppleScript Call Example

The real benefit comes when you want to do something more complex and instead of scripting something more complex yourself, you can conveniently let Doctor Drafts and Alfred so the heavy lifting for you.

This AppleScript is set to log an entry to a draft titled “Movie Log”, adding the date and time followed by the content of the clipboard, followed by an empty line.

set strLog to ((current date) as text) & "
" & (the clipboard as text) & "
"
tell application id "com.runningwithcrayons.Alfred"
	set configuration "title" to value "Movie Log" in workflow "com.thoughtasylum.doctordrafts"
	run trigger "drlog" in workflow "com.thoughtasylum.doctordrafts" with argument strLog
end tell

This is just an example to show you how you can incorporate the Doctor Drafts flows into your own scripts with minimal effort - particularly if you copy, paste, and modify the example code provided with each flow in the documentation.

“All we want are the Scripts Ma’am”

If you want to go all Joe Friday and don’t have Alfred, then I do have a few command line scripts for working with Drafts. Not as extensive as the operations I have built into Doctor Drafts, but useful nonetheless if you want to work with Drafts at the command line.

Summary

As well as the Drafts URL schemes and AppleScript, I hope you will find the additional automation capabilities that Doctor Drafts and my Drafts shell scripts provide allow you to take control of Drafts in ways that you had not previously considered. Drafts is a powerhouse of utility and being able to automate it ‘externally’ provides a whole host of ways you can utilise it to do work on your behalf.

Author: Stephen Millard
Tags: | alfred | drafts | scripting |

Buy me a coffeeBuy me a coffee



Related posts that you may also like to read