Trigger Alfred Workflow with Cron

Besides keywords or hotkeys triggering Alfred workflows, there’s External input triggers:

These input will trigger when the corresponding Apple Script code snippet is executed, the input node provides the snippet “Sample Code”:

I’ve used this many times to have one workflow trigger something in another workflow, however this week I had a need to cron trigger a workflow every minute, and run a workflow for me if a condition was met – and it turns out it was simple to throw this external trigger in cron.

To set this up in cron, I did:

$ crontab -e

I added the above Apple Script external trigger, like so:

* * * * * /usr/bin/osascript -e 'tell application id "com.runningwithcrayons.Alfred" to run trigger "com.davidsword.tools.triggername" in workflow "com.davidsword.tools" with argument "hello world"'

Note the argument was changed to hello world in the above. Saving this, then tying a notification to the external event, every minute my Alfred Workflow will run and say hello:

Pretty simple demo above, but it unlocks a lot of possibility. My use case for needing cron was intricate:

I have a busy light outside my office door (an LED on a ESP8266 board that’s either red for in a meeting, green for working, or off when afk). I change this light with Alfred as part of a change “Slack Status” command (ss meeting|active|offline ) which changes my Slack Status/Presence and this LED light (via HomeAssistant & ESPHome). I needed a script that ran every minute, detected if I’m in a Zoom meeting or not, checked what the current light status is, then ran the appropriate “Slack Status” command for me if I was in a call (turning my LED red and changing my Slack Status to “in a meeting”), or prompted me with a notification to change my busy light to active it was detected I was no longer in a call but the light was still (incorrectly) red.

Here’s the thing, I could have wrote this script outside of Alfred and just ended the script with an Apple Script call to an Alfred External trigger for the Slack Status command to change the status itself, however I did not want to maintain this script outside of Alfred as it is only for this workflow, its just a helper script. I really appreciate that it’s possible to do this with external triggers and I can keep the script as close to home as possible. Plus its fun coding things with the workflow UI view.

Using Alfred && Apple Shortcuts to do Really Cool Things

Alfred is absolutely amazing at doing high level stuff, but actually doing application level tasks is sort of limited to AppleScript, external APIs, and other sort of work arounds. Shortcuts on the other hand, is kind of clunky to do high level stuff, but any respectable third party app will have a ton of hooks and actions for Shortcuts to interact and do things within apps.

Using both these apps together, we’d get the best of both worlds. & after reading this post and Workflow by the Alfred team, it seems there’s actually a super easy way to trigger and interact with Shortcuts from terminal, and thus Alfred:

$ /usr/bin/shortcuts --help

This is a game changer. Here’s a Alfred workflow + Shortcut mash up I did to solve a real problem I was having:

My biggest improvement working day to day was when on Zoom calls I’d sometimes forget an action item I need to follow up on. One could argue I just needed to ensure I have my note taking window open, but that doesn’t work for me as I often am deep in many windows during a call and focused on the call. What I needed and wanted was to smash a global hotkey and be presented with a text prompt, type my string, hit enter, and it’d be stashed somewhere safe (much like Things quick entry, but I don’t use Things.app for work, I keep my Todos in my Note app).

To build this, I created a “@daily quick note” Apple Shortcut which prepends a string of text to add to a note in my note taking app (currently Bear.app) in a note I use everyday.

I tried using Apple Shortcut’s text input prompt, and using their keyboard shortcut listener but:

  1. When hitting return in the Shortcut text prompt, the dialog box doesn’t close, it creates a new line 🤦‍♂️. Tab and other tricks to escape don’t work either, so a mouse move is apparently required. & That’s not gonna fly.
  2. I got inconsistent results from the global keyboard shortcut from Apple Shortcuts, it worked sometimes but not every time – it must be more reliable.

So instead I the Apple Shortcut to accept an incoming argument:

With this Shortcut setup to accept input, I then created an Alfred workflow with a keyword “+” input, where I can add my action item.

This allows me to capture a thought pretty quickly (as I’m always using and very fluent in Alfred), & globally no matter where my note apps window currently is:

After the Alfred “+” keyword input, I’ve got a simple action run script in bash that takes advance of how Apple setup shortcuts:

query=$1

/usr/bin/shortcuts run "@daily quick note" -i <<< "${query}"

So typing + email that person about that thing and hitting enter will run my “@daily quick note” Shortcut, and pass in my string from Alfred, which interacts with my note taking app, and prepends the string to my daily note, capturing my action item that I don’t want to forget for parsing later.

For bonus points, in Alfred I also setup the keyboard shortcut hotkey (which works very reliably compared to Apple Shortcuts) to run “Utilities > Show Alfred” and auto populated the “+” keyword, so I can quickly pop open Alfred with the + keyword there, and enter my string right away. A second saver to help me retire early.

This example is pretty rudimentary, I’m simply changing the user input from the unreliable Shortcuts app to the easier and more familiar Alfred app, which is great, but the bigger take away here is there’s much more potential to do more interesting things with these two apps being able to work together.

I just setup an Alfred Workflow for some global hotkeys that bring Zoom.us into focus and toggles audio andor video (download here). The hotkey for un/mute (⌃⌥⌘␣) is easy enough, but I would love to just have a small custom macro keypad for Alfred, or rather, a big red button on my desktop sitting behind my keyboard for this workflow. Kind of like this, with a Raspberry Pi Pico and a button like this instead. I’ve already got all the parts – so if I ever find myself with a spare two hours, this is what I’m gonna do. 🎙