An Alfred Workflow for Drafts

Glad you like it. :slight_smile:

I’m not sure if I’ve missed any previous posts about this, but I don’t see any about anyone else having issues creating new drafts.

Sounds like you are using the AppleScript-based new draft option. While you should have had the security pop-up to allow access (it would show in System Preferences > Security & Privacy > Privacy > Automation > Alfred 4.app : Drafts) I have had it myself where these dialogs don’t seem to pop-up the first time, or I may have accidentally declined them when typing ahead sometimes. Unfortunately, there’s no obvious way to get prompted again, or to manually add the permission At least, none that I’ve come across to date…

The only option I know of is a a bit of a sledgehammer approach. The terminal command below should reset the AppleEvents permission for Alfred, but it will be for all Alfred app control related to events, not just for Drafts. After running, it should then prompt you on the next trigger as if it is the first time.

tccutil reset AppleEvents com.runningwithcrayons.Alfred

For Reference on this utility: tccutil Man Page - macOS - SS64.com

Give it a try and let us know how you get on.

Thanks @sylumer. Sadly that doesn’t work either.

The previous comments and some of your previous advice was on comment 16 of this thread. The applescript code you provided there does work, but just not in the workflow.

Also OOI there are no Drafts options in Automation. I’ll have to just use for seacrhing I fear

Thanks

Nick

Ah, that was the one where the poster seemed to be using a very old version of Drafts that did not support any AppleScript.

Yes. The error you posted is a permissions error related to calling one app control process from another; hence it is specific to Alfred as Alfred is the controller.

The tccutil command should be wiping the flag that sets whether or not to ask for permission to control. Only after confirmation of a prompt should you get somethjng for Alfred/Drafts appearing.

Do note that dropennew would allow you to create a new draft without utilising AppleScript, and so without requiring the permission. But resolving your permissions issue should absolutely be possible.

Thanks @sylumer. Yes dropennew works. Also interestingly drapp works and threw me a security alert to check.

I am going to update the Bug Sur soon, so I will check after upgrading.

Thanks for all your help and a great workflow

@sylumer First, thank you for your work, it’s great.

I am experiencing some of the same problems I’ve seen earlier in the thread: when I call particular actions (drapp or any action that is supposed to list or search my drafts) the workflow fails. In other words, when I ask Doctor Drafts to list drafts or search for drafts by, say, title or content, nothing shows up and I have a blank Alfred window with the Doctor Drafts icon on the right. For example: entering ‘drapp stuff’ and :leftwards_arrow_with_hook: then prompts for ‘Select draft to add to’ but doesn’t list any of my drafts. Then when I type terms that should match either titles or content, nothing shows up and pressing :leftwards_arrow_with_hook: does nothing. I have a blank Alfred window with the Doctor Drafts icon on the right.

  • I am running macOS 11.4, Alfred 4.5[1248] (with PowerPack), Drafts 27.0.11 (with Pro Subscription), and Doctor Drafts 1.2.0.
  • I have Python 2.7.15 at usr/local/bin/python. (The terminal command ‘$ python -V’ gives the result ‘Python 2.7.15’ and ‘$ which python’ gives ‘usr/local/bin/python’.)
  • Per the installation instructions, I installed Python 3 using the installer from InstallPython3.com | Mac. I also installed the certificates at the end of the installation.
  • Python 3.9.6 is now installed at /Library/Frameworks/Python.framework/Versions/3.9/bin/python3. (’$ python3 -V’ gives ‘Python 3.9.6’, and ‘$ which python3’ gives ‘/Library/Frameworks/Python.framework/Versions/3.9/bin/python3’.)
  • I tried restarting Alfred, Drafts, and my computer. Same problems.

The only possible causes I can think of are these:

  1. maybe Python 3 needs to be installed at a different location; or
  2. maybe Python 3 needs to be set as the default version of Python to run.

Notably, Doctor Drafts creates new drafts fine (I have granted permission for Alfred to control Drafts). It’s just that the Doctor Drafts searches for drafts by title or content don’t seem to be working, and hence actions like drapp aren’t working either.

I would appreciate any help you could offer.

In the response above, I referenced a simple Python 3 test workflow I used for helping someone else with a Keyboard Maestro workflow I’d created.

I don’t see anything about you running that, so we’ll go with something similar but perhaps more helpful in the suspected circumstances.

Download and install this Alfred Workflow - Python Version.

When installed, execute it from Alfred using pyveras the trigger keyword. Of course, do feel free to take a look at the workflow first to ensure it is in no way malicious.

When run it should put some version information about Python on screen and onto your clipboard.

It should tell you if the terminal instance used by Alfred is aware of both of your Python installations, and also what the default is. Dr Drafts should explicitly be using the Python 3 version it can find.

As an example, my output from the command is:

Python 2 = 
Python 3 = Python 3.8.2
Python (Default) = 

As noted before, some functionality uses non-Python techniques, but anything that involves searching involves Python. I suspect you are right about it being a Python installation issue given what you have observed. Hopefully the version check workflow will help a little more.

I think I might also look at building in a bit of diagnostic help into the Dr Drafts workflow itself.

1 Like

Somewhat oddly, it returns this:

Python 2 =
Python 3 =
Python (Default) =

My terminal defaults to bash, not zsh. Could that be playing a part?

EDIT: I changed default shell to zsh and reran pyver. Same results.

You should have zsh available, so that should not be an issue. You can alsofreely switch shells.

But this looks like it is your issue. Python does not seem to be available to Alfred.

My guess would be from this that there’s no python3 for your path.

In Alfred, my PATH variable is set to:

/usr/bin:/bin:/usr/sbin:/sbin

You can check your own by modifying the previous pyverscript to:\

p2=$(python2 --version)
p3=$(python3 --version)
pd=$(python --version)

echo "Python 2 = $p2"
echo "Python 3 = $p3"
echo "Python (Default) = $pd"
echo
echo PATH
echo $PATH

It will then output the PATH variable as well as the Python versions.

In my /usr/bin directory, I have python3, which is what should be getting called by Alfred when the workflow executes one of the Python scripts.

Things to do next:

  1. Check the content of your PATH variable in Alfred; not as you in the terminal, but in Alfred specifically.
  2. Check if you can find the python3 executable in one of the paths … most likely would be /usr/bin.

Assuming it is not available, try reinstalling Python 3.

In case it helps, I’m 95% sure that I followed the instructions in this post when I set-up Python 3. I think it is often an unusually painful experience with Python on the Mac, but all I can say is once it works, it opens up a lot of things.

Have a look, and do let me know how you get on.

1 Like

Good news and a little bug report related to the workflow.

First, the good news: I got it to work. In case it may help someone else, here’s how (after trial and error — erroneous steps omitted).

  1. Uninstall Python 3 (because I didn’t follow the instructions in the link provided above: The right and wrong way to set up Python 3 on MacOS | Opensource.com).
  2. Install Command Line Developer Tools.
  3. In Terminal, install or update Homebrew. This may take some special commands because of a ‘shallow/unshallow’ hiccup with fetch. Read the messages you get in Terminal for more information and the correct command to run.
  4. Follow the instructions in the link provided above: The right and wrong way to set up Python 3 on MacOS | Opensource.com. (Install pyenv.) (Check python.org for the latest version to install with pyenv.) (What was weird for me was that, even after installing Python 3.9.6, when I run the pyver workflow, it results in ‘Python 3 = 3.8.2’ — just like the example above.)
  5. Test and enjoy.

Second, the little bug report related to the workflow. In Alfred, typing ‘dr’ starts bringing up the list of Doctor Drafts commands. For me, the first command is ‘drtitle’. If I type in ‘dr’ and then :leftwards_arrow_with_hook: (to select drtitle) the workflow seems to execute the drtitle command but then it sets the search to ‘drtitle’ instead of having it blank (to list the drafts or optionally allow entry of an argument). I think the expected functionality is to execute the command but have a blank search field. (This may happen with other commands as well; I didn’t test but you might look into it.) Perhaps this could be corrected in the next version.

And one last question: is there a way to have the Doctor Drafts searches / listings show the results in reverse chronological order (newest drafts on top)? If not, it would be a nice option to add.

Thank you so much for all your help, @sylumer !

1 Like

Glad to hear it is working now. :sunglasses:

Also, thanks for taking the time to put your steps in. That is exactly the sort of thing people find useful.

Thanks, and got it. I can see why it is doing that. “drtitle” should be blanked out, but there is a typographic error in that part of the workflow. I’ll include the fix for it in the next release.

It is certainly possible. If I go down the road of doing that, then I need to offer it across all searches potentially, and also to allow people to order by different things ascending and descending - creation date, modification date, last access date, title, etc.

It pretty much opens up a whole raft of complexity. But, I would like to add that sort of additional flexibility across the workflow in time. As such, I’ve added it to the roadmap. There’s no specific order to what I’ll add in each release, it is just a list of to do items that I’m adding to.

1 Like

New version of the workflow is out (v1.3.0).

It includes a couple of small fixes and a small number of pieces of new functionality.


Paste Content

drpaste now lets you insert content from a draft directly where your cursor is.

There’s also a new use case showing how you can set up a snippet trigger some you can type a string in, and it will be replaced by the content of a particular draft.

Remember, if you have any thoughts on extending Doctor Drafts with a use case, or have created one yourself that you think others might find useful, don’t forget to post about them.

Diagnostic Report

drdiag will generate a report to help users who are finding things are not working as they expect. It builds on the checks above, but checks Python versions, some details about Alfred and Drafts, the local PATH variable (in Alfred), and provided some high level advice on any issues found.

Storage Report

drstor will generate a report about Drafts storage utilisation. This is based on some of the points of discussion I noted in this thread on maintenance.


Hopefully this has not introduced any new issues, but if anyone spots anything, please do let me know and I’ll take a look.

Documentation on the website has been updated, and hopefully cross-referenced everywhere it needs to be.

Visit https://doctordrafts.thoughtasylum.com to grab the update and read the documentation, and enjoy.

I know its only been a week since the last update, but I had an update waiting for the release of Alfred 4.5, which happened this week.

In Doctor Drafts 1.4.0, it now includes definitions for the new Universal Actions feature in Alfred. Depending on your Alfred settings for Universal Actions, you may just find that you get a lot of Doctor Drafts actions showing up as Universal Actions anyway. That’s fine, but some are not exactly compatible with the idea of universal actions, which is why many people switch the settings to provide more fine grained control. If you do that, this latest update has you covered and will provide you with what is hopefully a good fit of Drafts-related universal actions.

Since Doctor Drafts already includes lots of powerful file specific actions, these universal actions focus on working with selected text.

In addition, I have added a set of Drafts-related Alfred fallback searches that you can enable. These provide keyword-less trigger options for searching for terms, and include options to search for drafts by title and content.

Finally, I’ve added a new option to allow you to quickly search the Drafts forum. The existing forum search allows you to jump in at various points. This new flow will let you pre-populate and run a search in your browser. This is also tied in to one of the available fallback searches.

Of course, there’s new documentation for all of the features and updates to the documentation for the many things that have changed as a result of this release’s additions. I had a quick count and there are now over 100 pages of documentation for the workflow on the site, and all searchable.

Download the latest version of Doctor Drafts from the usual spot, and as always, do let me know if you come across any issues.

1 Like

Doctor Drafts v1.5.0 is now available for download. It includes one feature update and one new workflow. Details are as follows.

Workflow Update Check

There is a new flow that allows you to check for a workflow update on the website. It will not install, or offer to install the update for you. It will prompt you to go and get a copy if a newer version is available.

In addition, the various drcomplete... flows that terminate almost all other flows will now also trigger this flow. There are some variables that are used to determine when the last check was carried out, how often checks should be carried out, and a couple to do with audio notifications. The idea is that even if you miss an update here on the Drafts forum, you’ll get a periodic prompt from the workflow itself.

If you want to change the checking frequency, add the UCfrequency variable and set it to the number of days between checks. A value of -1 will disable the regular checking.

I believe the checks run pretty fast. At least the do in my testing. While I have set things up to default to only carrying out a check once per week, using the drwfu keyword will always force a check. This is regardless of your settings - as long as you have an Internet connection that is.

You can build this check into any other scheduled software checks you might have via an AppleScript call, should you want to schedule it on some other basis - e.g. every Monday at 9 AM, every time you open Drafts, or as part of your Mac start up/restart procedure. There is a new use case that leads you through the steps to do this.

Full details on this new flow and the settings it uses can be found here.

Quick Reference Viewer

Previously I have been using a a Quick Look debugger resource viewer for viewing the quick reference information. While this worked, I was always frustrated by not being able to remove the “[DEBUG]” entry in the window title.

In this update I’ve switched to using a web viewer, which I think looks a bit nicer and does not include “[DEBUG]” in the title.

Note

This is based on an Automator workflow. While I fully realise Automator is being deprecated, I figured this was a reasonable interim step and will work for a while. I suspect I’ll be able to update this with a Shortcuts based version before too long for users who have the option, assuming the Shortcuts replacement for Automator allows for an equivalent of this sort of web viewer.


I hope everyone finds these quality of life updates useful, and as ever, you can download the latest version of Doctor Drafts from the website.

Hi @sylumer,

Thanks for the wonderful workflow. Thorough is you middle name?

In the description at the beginning of this thread you mention: “Run a Drafts action by name”. It is probably just me, by I cannot find that in the workflow or on the documentation web site.
What I am looking for is an easy way to run actions in Drafts without having to use the mouse, so an Alfred style triggering mechanism would indeed be great?

Am I missing something in the documentation?

Glad you like it and no, not my middle name, but I do have a reputation in my day job for something like that :wink:

There is a search for action flow built in, but to run a specific action directly, there’s a use case covering this.

At some point I’ll take another go at writing something to provide a searchable list of actions directly in Alfred, but the structure to get the right details out of Drafts is a little more complicated for the actions.

But I think there is a built in feature that will do what you want as well, and so will require no additional configuration effort on your part.

There’s a feature called Quick Search (accessed by Shift-⌘-F). You can type in the name of an action and it will find it as it runs a search against a variety of Drafts ‘items’ such as drafts, actions, workspaces, and some “common” commands.

As you type in the search term, Quick Search will narrow the search, and your action should appear at or towards the top. Arrow down to it and hit return to run it.

Hope that helps.

OK, got it. I’ll have to curb my “trigger everything through Alfred”-obsession then :slight_smile:

That is very useful indeed. Thanks for pointing me in that direction, had not found that myself… That pointed me to the shortcut control-option-L selects the action search / filter, which provides an even clearer overview. So thanks for the pointer.

The thing with the action list search is, as far as I know, is you can’t keyboard navigate into the list of actions from the search field, whereas you can with the Quick Search. At least that was why I recommended Quick Search over the action list search then run via pointer interaction.

You can TAB-into the list and then navigate with the arrow keys.
Alternatively you could use Vimac-app to create a short-hand access to the action.

Both options (Quick Search and Action list search) could do the trick, it’s seems good to have multiple options. The main benefit is that both get the action you need relatively quickly available instead of having to remember in which action group they sit and having to select that action group first.

Anyway, I’m diverting from the real topic here.

1 Like

Ah, okay. When I’ve tried TAB before, including earlier today, it tabbed me between various parts of the UI as expected, but never highlighted that the focus had shifted to that list. Only after also then pressing an arrow key did the focus appear on the first option in the list.

Thanks.

I stand corrected indeed. You are right, have to also press a down arrow key. If I tab our of the search box the focus appears t one on the word Search and pressing the right arrow allows me to browse through all the action groups.
Interestingly, this browsing functionality is not presented if you mouse click on the Action Group label, so which user interface element gets actually selected in not clear to me.

Thanks for showing an automators perspective on the UI.