An Alfred Workflow for Drafts

If as well as being a Mac user of Drafts, you are also an Alfred app user, then this post might be for you.

I’ve recently been updating and sharing some of my Alfred workflows, and I’m now in a position to share one I created for Drafts. I pulled together some separate workflows I had created over the course of the last year or two, and slowly started building it out. It may have grown a bit more than I’d originally anticipated.

You can download the workflow and read through the documentation and guides (yes, that really is how much it grew) at doctordrafts.thoughtasylum.com.

To give you a flavour of some of the sorts of things that are possible in this first release:

  • Creating new drafts in the foreground or background (supporting newlines and tabs too).
  • Searching for drafts by content or title (filtering the results live in Alfred).
  • Copying the content of or URL to open a draft to the clipboard.
  • Prepending and appending to drafts (again supporting newlines and tabs).
  • Jump directly to various documentation pages on a variety of domains (also copy the URL or a Markdown link).
  • View local quick reference information (available offline).
  • Generate Drafts-styled feedback e-mails.
  • Run a Drafts action by name.
  • Open a new draft for dictation.
  • Options for easily building your own custom Alfred workflows on top of it (worked examples provided + technical documentation).
    • Append to a daily log, built to your own titling specification is one of the use case examples.
    • Create a capture board for running a hotkey driven text capture draft is another of the use case examples.

This is a 1.0 release. It has had some beta testing, but there’s always the chance something’s not quite right. Any issues, just post something in this thread.

Likewise, I’m open to suggestions for improvements (though I’m not promising to apply them :wink: ), I will add stuff and check stuff off on the roadmap (on the website). I’d also love to see what customised workflows people come up with. Please do add to this thread and we’ll see where it goes.

There’s a general intro on my blog, but the download and lots more information are on doctordrafts.thoughtasylum.com.

Finally, for the particularly geeky amongst you, and I know there are quite a few of you, here is a high level view of what the workflow looks like in Alfred.

I told you it grew more than I anticipated. :upside_down_face:

20 Likes

Oh my. This is quite amazing. And as always expertly documented.

Quick report: using drfind , if the text string I’m looking for has a slash in it, drfind doesn’t turn up results. E.g.,

draft content:

blork / blap

Alfred query:

drfind blork

has no results. If I delete the slash from the draft, the query finds the draft.

2 Likes

Thanks. I’ll take a look at that. I suspect a bit of escaping may be required.

wow thats impressive - just read the blogpost and a few pages on the documentation and it looks awesome!
No more downloading, quitting and moving the new beta release into the Application folder :smiley: thanks for that.

I’ll try it out soon - thanks for this amazing workflow!

1 Like

This is amazing, thanks so much @sylumer

The use of external triggers like this is really clever. I have already adapted a couple of my scruffier Drafts workflows to use your Flows, and knowing I can update the underlying package without breaking things is great.

1 Like

I use Alfred and the excellent workflow on my MacMini and Macbook Air M1. On my MacMini I can search my drafts with “drtitle” or it already shows them in a list. On my MacBook Air M1 I only get the suggestion that I can search “drtitle” on google, Amazon and Wikipedia. I have compared settings but cannot find any difference. Does anyone have any ideas about this?

1 Like
  1. If you look at the workflow on your Air, can you see “drtitle” as a keyword option?
  2. Do other keywords in the workflow work on the Air?
  1. Yes, seeing the keyword. Exact the same like the MacMini.
  2. Yes, doing a “drsearch” will open Drafts and showing the search results.
  3. Doing “Append to drafts” same. MacMini getting in the second step after searching for a draft a list with results. Not on the Air.

Re-installed. Same behavior.

1 Like

Ok, as I test this a little more, I don’t think the slash is causing this. I think what’s happening is I’m trying to find drafts by typing in part of the title and part of the content in the same query and it seems like drfind is maybe only checking the “body” of a draft and not the entire content? Or maybe I can’t type in search terms on different lines of the same draft?

I have exactly the same problem on my iMac. Haven’t tried on mbp yet. (Thanks for your work on this @sylumer!)

It impacts my “drfind” too. Here are some screen shots.

Searching With Slashes and Across Lines

I know you posted an update to your observation, but just to confirm that in my testing this works fine.

As you can see above it is searching the title. It searches the whole content.

2021-04-23-18.22.30

What it doesn’t do is search multiple lines. For example, if I enter “defined There\nIts”, that will not work because Alfred doesn’t realise that the “\n” represents a newline in its own internal filtering mechanism, and as far as I know it offers no native way to otherwise denote a newline character. if anyone knows any different, please do let me know.

If you are instead hoping to match something like say “drfind forward down” with multiple terms being found in different places in the content, again, this isn’t something Alfred’s inbuilt filtering mechanism currently supports.

I could work around both of these by having the filtering be delayed but more intensive. As it stands I’m letting Alfred handle the filtering - its efficient. But, by repeating the query after a pause in typing and doing so by building a more dynamic query (e.g. splitting the words, double quoted phrases, transforming “\n” to a real newline character) it should be possible to create a version that would do that.

That’s out of scope for this initial release, but would be something I’d certainly consider working on adding to a later update if people would find an option like that useful.


Some Keywords Are Falling Back to Default Alfred Searches on Some Devices

Okay, so the workflow is in place, it is working for some keywords but not for others.

drsearch is a straight URL call. The drtitle and drappend don’t seem to be recognised. Both of those keywords will initiate a search at the points you are noting. I think what you are probably seeing is Alfred getting a null result and falling back to the default search.

My next question would be, have you got Python 3 installed on your M1 as per the pre-requisites?

If you are unsure, I helped someone check for it for using my Keyboard Maestro Alfred workflow the other week over on the Automators forum. Maybe give that a try if you are unsure.

Now it may not be that. it may be down to something in the scripting that isn’t quite working as it should because, for example, something isn’t where the script has been coded to expect it to be. In which case, we may have to look at running some checks and test scripts to get to the bottom of what’s going on.

Thanks for confirming the behaviour. Two people to check independently is very useful. Take a look at the above notes re. Python 3 and see if it is missing or if I have an issue with the script not taking into account a broad enough range of personal set-ups.

2 Likes

OK, I have now reinstalled the whole Python via homebrew. Everything from the beginning Alfred, powerpack, your workflow and now it runs like on the Mac mini. Thanks for the great worklfow and the great support.

Thanks for the update, that’s excellent news.

I’m glad it wasn’t anything too onerous to get it working, and I really hope you’re able to get some good use out of it.

I can’t get any of the text I write in the Alfred search bar to actually appear in Drafts, it’s almost like the workflow does not know I already have Drafts installed. Can someone help?

I’ve installed it and I get the text notifications that a Draft has been created. Then I open drafts and see nothing changed.

Alfred Drafts Notification

when I just did this, I got a security warning that I had to approve. Did you get that? You may need to poke around for that if not.

Also, double check the python install.

drnew is the entry you highlighted, and should be running some AppleScript with the information you are passing in.

What happens if you run the AppleScript below, based on the AppleScript in the workflow, from Apple’s script editor (or your AppleScript editor/IDE of choice)?

to split(someText, delimiter)
	set AppleScript's text item delimiters to delimiter
	set someText to someText's text items
	set AppleScript's text item delimiters to {""}
	return someText
end split

--Example content
set draftContent to "This is my draft content"
--Alfred defaults the unspecified variable to false Boolean when accessed
set draftFlagged to false
--Alfred defaults the unspecified variable to an empty string when accessed
set draftTagsString to ""
set draftTags to split(draftTagsString, ",")

tell application "Drafts"
	make new draft with properties {content:draftContent, flagged:draftFlagged, tags:draftTags}
end tell

It should create a new draft with the content “This is my draft content”.

If that works, the next step is to debug it in the workflow.

In the workflow in Alfred, there’s a “bug” icon in the top right. Select that to enable the debugging window.

Using the command like this:
2021-04-23-22.10.06

Gives me an output like this:

[22:11:40.400] Doctor Drafts[Keyword] Processing complete
[22:11:40.412] Doctor Drafts[Keyword] Passing output 'This is my draft content' to Call External Trigger
[22:11:40.413] Doctor Drafts[External] Processing complete
[22:11:40.413] Doctor Drafts[External] Passing output 'This is my draft content' to Arg and Vars
[22:11:40.414] Doctor Drafts[Arg and Vars] Processing complete
[22:11:40.414] Doctor Drafts[Arg and Vars] Passing output 'This is my draft content' to Replace
[22:11:40.415] Doctor Drafts[Replace] Processing complete
[22:11:40.415] Doctor Drafts[Replace] Passing output 'This is my draft content' to Replace
[22:11:40.416] Doctor Drafts[Replace] Processing complete
[22:11:40.416] Doctor Drafts[Replace] Passing output 'This is my draft content' to Run Script
[22:11:40.430] Doctor Drafts[Run Script] Processing complete
[22:11:40.433] Doctor Drafts[Run Script] Passing output ' This is my draft content' to Arg and Vars
[22:11:40.433] Doctor Drafts[Arg and Vars] Processing complete
[22:11:40.434] Doctor Drafts[Arg and Vars] Passing output ' This is my draft content' to Conditional
[22:11:40.434] Doctor Drafts[Conditional] Processing complete
[22:11:40.435] Doctor Drafts[Conditional] Passing output ' This is my draft content' to Arg and Vars
[22:11:40.435] Doctor Drafts[Arg and Vars] Processing complete
[22:11:40.436] Doctor Drafts[Arg and Vars] Passing output ' This is my draft content' to Run Script
[22:11:40.509] Doctor Drafts[Run Script] Processing complete
[22:11:40.510] Doctor Drafts[Run Script] Passing output 'false
' to Arg and Vars
[22:11:40.511] Doctor Drafts[Arg and Vars] Processing complete
[22:11:40.511] Doctor Drafts[Arg and Vars] Passing output '' to Run Script
[22:11:40.751] Doctor Drafts[Run Script] Processing complete
[22:11:40.766] Doctor Drafts[Run Script] Passing output 'draft id 35602E5D-7ABC-4532-80D3-BB983A5CED52
' to Arg and Vars
[22:11:40.767] Doctor Drafts[Arg and Vars] Processing complete
[22:11:40.768] Doctor Drafts[Arg and Vars] Passing output 'draft id 35602E5D-7ABC-4532-80D3-BB983A5CED52
' to Call External Trigger
[22:11:40.769] Doctor Drafts[External] Processing complete
[22:11:40.769] Doctor Drafts[External] Passing output 'draft id 35602E5D-7ABC-4532-80D3-BB983A5CED52
' to Conditional
[22:11:40.770] Doctor Drafts[Conditional] Processing complete
[22:11:40.771] Doctor Drafts[Conditional] Passing output 'draft id 35602E5D-7ABC-4532-80D3-BB983A5CED52
' to Play Sound
[22:11:40.807] Doctor Drafts[External] Passing output 'draft id 35602E5D-7ABC-4532-80D3-BB983A5CED52
' to Conditional
[22:11:40.829] Doctor Drafts[Conditional] Processing complete
[22:11:40.829] Doctor Drafts[Conditional] Passing output 'draft id 35602E5D-7ABC-4532-80D3-BB983A5CED52
' to Post Notification

Note the detail:

Passing output 'draft id 35602E5D-7ABC-4532-80D3-BB983A5CED52 ' to Arg and Vars

That’s the UUID of the new draft being created by the AppleScript. If there’s some sort of failure, I would hope that the details would be shown in place of or just before the first instance of where this is seen. i.e. when the AppleScript is executed.

Post back with whatever observations you get from the above.

1 Like

I updated my Drafts App and that seems to have resolved the issue.

What version were you running previously? wouldn’t have thought any of the recent updates would affect this. I’ve been using something based on this since AppleScript support for new draft creation was introduced in version 19.0 (ref. public release is now version 26.2) without any issues.

I’m not sure if this is a feature request for you or for @agiletortoise, but I would love to be able to search for a workspace name and open it from Alfred. This workflow allows me to type in a workspace name and open it, but I’d love it could work like the draft workflow, where I can type a few characters of a draft name and Alfred shows me a few options I can choose from. Right now I have my own workflow where I pre-populated the names of workspaces, but this has been hard to keep up-to-date and I’m thinking if drafts can be searched from Alfred in this way, maybe workspaces could be as well?