Sub topic 12
Running shortcuts from Terminal with the shortcuts command
This chapter assumes you can build and run a multi-step shortcut in the Shortcuts app. It also assumes you know how Shortcut Input and the Run Shell Script action work. You need basic Terminal skills: running commands, quoting arguments with spaces, and reading file paths.
The shortcuts command lets you start shortcuts from Terminal, shell scripts, cron-style schedulers, and other tools that can run a command. It does not replace the app. It is a thin front end that asks macOS to run a shortcut from your library.
What the command does
Every Mac that has the Shortcuts app also has /usr/bin/shortcuts. The command talks to the same shortcut library you see in the app. It does not hold its own copy of your shortcuts.
The main subcommands are:
| Subcommand | Purpose |
|---|---|
| `shortcuts list` | Print the names of shortcuts in your library |
| `shortcuts run` | Run one shortcut, optionally with input and output files |
| `shortcuts view` | Open a shortcut in the Shortcuts editor |
| `shortcuts sign` | Sign a shortcut file so it can be shared |
Run shortcuts help to see the subcommands on your macOS version. Run shortcuts help run to see the options for one subcommand. Options can differ between macOS releases, so treat the built-in help as the reference for your machine.
Listing shortcuts to find the exact name
shortcuts run matches a shortcut by its name. If the name is slightly wrong, the command fails. Start by printing the names in your library:
shortcuts listThe output is one name per line. Copy the name exactly, including capital letters and spaces. A trailing space or a different kind of apostrophe is enough to cause a mismatch.
Two options help when your library is large or has duplicate names:
shortcuts list --foldersprints your folder names instead of shortcuts.shortcuts list --folder-name "Folder Name"prints only the shortcuts in that folder.shortcuts list --show-identifiersprints a unique identifier next to each name. You can pass the identifier toshortcuts runinstead of the name.
Use identifiers in scripts you plan to keep. Someone can rename a shortcut, but its identifier stays the same.
Running a shortcut
The simplest form takes only a name:
shortcuts run "Uppercase Text"Quote the name whenever it contains spaces. Without quotes, the shell splits the name into separate words and shortcuts receives the wrong arguments.
The command waits until the shortcut finishes. It then returns control to the shell. If the shortcut shows a dialog, such as Ask for Input or Show Result, the dialog appears on screen and the command waits for you to answer it.
Giving a shortcut input
Use --input-path to hand a file to the shortcut as its Shortcut Input:
shortcuts run "Uppercase Text" --input-path ~/Desktop/note.txtInside the shortcut, the file arrives as the Shortcut Input variable. This is the same variable that Finder passes when a Quick Action runs on a selected file. A shortcut built to receive files from Finder usually works from Terminal without any change.
The input type matters. If the shortcut is set to accept only certain content types, such as images, and you pass a text file, the run can fail or the input can be ignored. Check the shortcut's input settings in the editor if a run behaves differently from the Finder version.
Capturing the result
A shortcut can produce output from its last action. Use --output-path to save it to a file:
shortcuts run "Uppercase Text" \
--input-path ~/Desktop/note.txt \
--output-path ~/Desktop/note-upper.txtThe output is the result of the final action in the shortcut. If the last action is Show Result, it still displays a dialog. To return data cleanly to Terminal, end the shortcut with an action such as Stop and Output, or leave your text as the last action's result. Use shortcuts help run to see whether your macOS version also supports --output-type, which lets you request a specific type such as plain text.
Combining the pieces gives a repeatable command you can paste into scripts. Each run reads one input file and writes one output file, so the shortcut behaves like any other command-line filter.
Combining the shell script action and the command
The previous chapter used the Run Shell Script action to call a shell script from inside a shortcut. This chapter goes the other way: a shell script calls a shortcut. The two can be combined.
The Run Shell Script action offers two ways to pass input: to standard input (stdin) or as arguments. This script uses stdin and converts text to uppercase:
tr '[:lower:]' '[:upper:]'This script uses arguments, and prints the size in bytes of each file the shortcut received:
for f in "$@"; do
wc -c < "$f"
doneSet Pass Input to "as arguments" for the second script. If it is set to stdin, $@ is empty and the loop prints nothing. This is a common reason for a script that works in Terminal but returns nothing in a shortcut.
A run from Terminal can then reach this action through the Shortcut Input variable. This command passes the file in as input:
shortcuts run "File Sizes" --input-path ~/Desktop/note.txtHere "File Sizes" is a shortcut you create: Receive input, then Run Shell Script with the arguments version above, then output the result.
Using the command in your own scripts
Because the command returns an exit status, you can check whether it worked. A status of 0 means success. A nonzero status means something failed, such as a missing shortcut name:
#!/bin/zsh
set -u
shortcut="Uppercase Text"
input="$HOME/Desktop/note.txt"
output="$HOME/Desktop/note-upper.txt"
if [[ ! -f "$input" ]]; then
echo "Input file not found: $input" >&2
exit 1
fi
if shortcuts run "$shortcut" --input-path "$input" --output-path "$output"; then
echo "Wrote $output"
else
echo "Shortcut failed: $shortcut" >&2
exit 1
fiSave this as a file, run chmod +x on it, and run it. The script checks the input first, quotes every variable, and reports a failure instead of continuing silently.
You can also run the command once per file with a loop:
for f in ~/Desktop/notes/*.txt; do
shortcuts run "Uppercase Text" \
--input-path "$f" \
--output-path "${f%.txt}-upper.txt"
doneThe ${f%.txt} syntax removes the .txt ending, so a.txt becomes a-upper.txt. Each run starts the shortcut again, so many files take longer than one run that receives all files at once. For large batches, build the shortcut to take a list of files.
Common errors and how to fix them
- Shortcut not found. The name does not match. Run
shortcuts list, copy the name, and check for extra spaces or different capitalization. Use an identifier when names are duplicated. - The command waits and does nothing. The shortcut is probably showing a dialog, or it is waiting for a permission prompt. Look for a window in the Shortcuts app or in front of other windows.
- No output file, or an empty one. The last action did not produce output. Run the shortcut in the editor and look at the result of the final action.
- Works in the app, fails in a script. The environment differs. A shell script run by
launchdorcronmay lack your login session, yourPATH, or access to files in protected folders. Run it from Terminal first to rule this out. - A permission prompt appears the first time. macOS asks before a shortcut accesses files, apps, or the shell. Approve it once. The decision is remembered for that shortcut.
When to use the command
Use it when something else already starts your work: a script, a build step, a scheduled job, or a launcher. It suits cases where you want one tested piece of automation to run both from the Shortcuts app and from the shell.
Choose a different tool when the work is entirely about files and text and needs no Mac apps. A plain shell script is simpler there. A shortcut earns its place when it uses actions that only the Shortcuts app offers, such as working with Calendar, Reminders, Photos, or system settings.
Exercise
You created a shortcut named "Count Words" that takes a text file as Shortcut Input and ends with the word count as its output. Write the Terminal command that runs it on ~/Documents/essay.txt and saves the result to ~/Documents/essay-count.txt. Then say what you would do if the command reports that the shortcut cannot be found.
Show answer
shortcuts run "Count Words" --input-path ~/Documents/essay.txt --output-path ~/Documents/essay-count.txt. If the shortcut is not found, run shortcuts list and copy the exact name, checking for different capitalization, extra spaces, or duplicate names. If names are duplicated, run shortcuts list --show-identifiers and pass the identifier to shortcuts run instead of the name.
Exercise
A Run Shell Script action contains the loop for f in "$@"; do wc -c < "$f"; done, and it prints nothing when you run the shortcut. Name the setting most likely to cause this and the fix.
Show answer
Pass Input is probably set to stdin, so $@ is empty and the loop never runs. Change Pass Input to "as arguments".