Six Colors
Six Colors

by Jason Snell & Dan Moren

This Week's Sponsor

Magic Lasso Adblock: 2.0x faster web browsing in Safari

By Jason Snell

Run shortcuts from the Mac command line

I was reminded by Simon Støvring, maker of the excellent Mac and iOS utility Data Jar (which is a persistent data store that’s accessible via Shortcuts), that people may not be aware of just how well integrated Shortcuts is into macOS.

For example, if you’re someone who works in the Terminal a lot, you may not realize that you can integrate Shortcuts (including accessing data from apps like Data Jar) directly into your shell scripts and commands via the shortcuts command-line app.

I created a small example shortcut called songtitle that outputs the title of the currently playing song.

two-step shortcut to get a track title

Now from Terminal, I can use that output in any way you would normally process data. If I want to open the result in BBEdit, for example, I’d type:

shortcuts run songtitle | bbedit

If I wanted to output the result to a file, I can use the -o flag to direct the output:

shortcuts run songtitle -o ~/songtitle.txt

And if I just want to see the output, I can pipe the result to cat:

shortcuts run songtitle | cat

By the way, shortcuts will also provide command-line access to the names of all your available shortcuts by typing shortcuts list.

And yes, if you’re writing AppleScript scripts, you can use the do shell script command to gain access to shortcuts, though the proper way to do this1 is to instead use the new Shortcuts Events helper app:

tell application "Shortcuts Events"
    set theResult to run shortcut "songtitle"
end tell

  1. In the current macOS Monterey 12.1 beta, no result is returned! Betas. 

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.


Search Six Colors