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.

Leave a Reply

Your email address will not be published.