By Jason Snell
January 26, 2022 5:02 PM PT
Shortcuts, AppleScript, Terminal: Working around automation roadblocks
When you’re automating something, sometimes you run into a roadblock. On iOS, that roadblock is often impassable, though that happens less often now than back in the day. On macOS, there’s usually a way around if you know the tricks.
Yesterday (and I swear this is not going to turn into yet another post about that podcast note script Dan and I keep bashing around) I decided that there was a feature I could more easily implement in Shortcuts… but that meant that I would need to abandon my original AppleScript script and change how I was kicking off my automations by pressing a button on a Stream Deck.
The current Stream Deck plugin for Shortcuts doesn’t let you pass input on to the Shortcut, which is how I pass text to my note script. So I had to find another way. I ended up using the Run OSA Script Stream Deck plugin to activate my shortcut… via AppleScript.
This is what I mean by getting around roadblocks.
One of the many ways you can activate Shortcuts on the Mac involves using a system app provided by Apple called Shortcuts Events. Shortcuts Events is completely invisible, and always available. It lets you run shortcuts from any AppleScript script, without launching the Shortcuts app.
So I wired my Stream Deck button up to this one-line AppleScript script:
tell application "Shortcuts Events" to run the shortcut named "Podcast Note" with input "cough"
Would it have been simpler if the Shortcuts plugin for Stream Deck supported this natively? Sure. And it probably will, eventually. But for now, there’s a workaround.
Similarly, if I had wanted to control Shortcuts from the Terminal, I could do it there, too. In macOS Monterey, Apple provides a command called
shortcuts that lets you list and run shortcuts. This is another way around a roadblock.
There’s one catch with
-i flag to supply input only accepts files, not text. So this command will fail:
shortcuts run "Podcast Note" -i "test"
That’s kind of a bummer. I don’t want to save text to a file and then attach it, I just want to pass my text in the same command.
But it turns out you can pass text to
shortcuts — you just need to use the
<<< operator (which I was informed about by Dr. Drang, with my memory jogged a lot later by John Gruber) to feed input right into the
This command does exactly1 what I want:
shortcuts run "Podcast Note" <<< "test"
Which means that, if I had wanted to, I could have wired up my Stream Deck button with this one-line AppleScript script:
do shell script "shortcuts run 'Podcast Note' <<< 'test'"
Among the roadblocks modern Mac users face are apps, like my Stream Deck plugins, that only offer a single pathway out—whether it’s AppleScript or shell script or something else. But if that pathway will let you run an AppleScript script or a command-line command, you can run a Shortcut from there.2
And, conversely, if you’re using a Mac app that only supports Shortcuts, you can use that path to execute AppleScript scripts or command-line scripts. (And yes, you can run AppleScript scripts from the Terminal too, using the
What I’m saying is, no matter what task you’re trying to perform, in macOS Monterey there are many paths. As long as there’s some way to run something, you can probably get to everything else. Yes, it might be ridiculous to run an AppleScript script that runs a Shortcut that runs a command-line command… but if it helps you do your job, who cares?
- Okay, not exactly. It appends a newline to the end of the input, which the shortcut has been altered to detect and remove. ↩
- In fact, just yesterday I discovered that in order to use a Keyboard Maestro variable inside an AppleScript script, I needed to script Keyboard Maestro itself from within AppleScript! Ridiculously recursive. ↩
If you appreciate articles like this one, support us by becoming a Six Colors subscriber. Subscribers get access to an exclusive podcast, members-only stories, and a special community.