Sub topic 16
Running Shortcuts from Terminal
This chapter assumes you can open Terminal, run basic shell commands, and have already created or run a Shortcut in the Shortcuts app. It builds on the previous chapter, where Siri ran a Shortcut by its name. The command-line tool runs the same Shortcuts, so you can call them from scripts.
What the shortcuts tool does
macOS includes a command-line tool named shortcuts. It lists the Shortcuts available to your user account and runs them by name. Siri does the same when you say a Shortcut's name. Running one from Terminal is the same action without the voice step.
Start by listing what you have:
# List the Shortcuts available to the current user
shortcuts list
# Run a Shortcut by name
shortcuts run "Daily Summary"Put quotation marks around any name that contains spaces. The name must match exactly, including capitalization. If you are unsure of the spelling, copy it from the output of shortcuts list.
Passing input and saving output
A Shortcut can receive input and produce output. In the Shortcuts app, these are the first and last steps of the flow. The --input-path option sends a file into the Shortcut. The --output-path option writes the result to a file.
# Pass a text file as input to a Shortcut and save its output
shortcuts run "Clean Up Text" --input-path ~/Documents/draft.txt --output-path ~/Documents/draft-clean.txtThis is what makes Shortcuts useful in scripts. A script can hand a file to a Shortcut, wait for it to finish, and then read the result file.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Shortcut not found | The name is misspelled or the quotation marks are missing | Copy the name from `shortcuts list` and quote it |
| No output file appears | The Shortcut does not end by returning a result | Add an action that outputs the result, then run it again |
| Nothing happens, or the command hangs | The Shortcut is waiting for a permission prompt or a choice from you | Run it once in the Shortcuts app and approve the prompts |
| Wrong content processed | The input file is not the type the Shortcut expects | Check what input type the Shortcut accepts |
Using it in a script
Because shortcuts run is an ordinary command, you can use it in a shell loop. This example cleans every text file in a folder:
for f in ~/Documents/drafts/*.txt; do
shortcuts run "Clean Up Text" --input-path "$f" --output-path "${f%.txt}-clean.txt"
doneQuote "$f" so file names with spaces do not break the command.
Exercise
You run shortcuts run "Clean Up Text" --input-path draft.txt and the Shortcut finishes, but you have no result file. Which option did you leave out, and what must the Shortcut itself do?
Show answer
You left out --output-path, which tells the tool where to write the result. The Shortcut must also end by returning output, otherwise there is nothing to write.