Sub topic 16
Running Shortcuts from Terminal
This chapter covers the shortcuts command-line tool on macOS. It lets you list, run, and script Shortcuts without opening the Shortcuts app or speaking to Siri. It assumes you can open Terminal, use basic shell commands such as cd and ls, and have read the earlier chapter on triggering Shortcuts with Siri. You don't need to be a shell scripting expert.
Why run Shortcuts from Terminal
A Shortcut is a saved sequence of actions, such as resizing images, summarizing text, or creating a calendar event. Siri runs a Shortcut when you say its name. The shortcuts tool runs the same Shortcut from a shell instead.
This matters for three reasons:
- You can call a Shortcut from a shell script, a
cron-style scheduler such aslaunchd, or another tool. - You can pass files in and get results out, so a Shortcut becomes a step in a larger pipeline.
- You can test a Shortcut repeatedly without clicking through the app.
The tool is built into macOS 12 Monterey and later. You don't install anything.
Listing your Shortcuts
Open Terminal and list the Shortcuts available to your user account:
# List the Shortcuts available to the current user
shortcuts listEach line is one Shortcut name. The names are exactly what you use with shortcuts run, and they are what Siri listens for.
Two useful variations:
# Show the unique identifier for each Shortcut as well
shortcuts list --show-identifiers
# Show only Shortcuts in a particular folder
shortcuts list --folder-name "Work"Run shortcuts list --help to see which options your macOS version supports. Options can change between releases.
Names with spaces
Shortcut names often contain spaces. Quote them, or the shell splits the name into separate arguments:
shortcuts run "Daily Summary"If two Shortcuts have similar names, use the identifier from --show-identifiers instead of the name. An identifier is unambiguous.
Running a Shortcut
The basic form is:
# Run a Shortcut by name
shortcuts run "Daily Summary"This is the same action Siri performs when you say "Daily Summary". The command waits until the Shortcut finishes, then returns to the prompt. That makes it safe to use in a script where the next line depends on the Shortcut's result.
If the Shortcut needs permission to use something, such as your files, calendar, or an app, macOS may show a dialog the first time. Approve it once. Later runs from Terminal use the saved choice.
Passing input and capturing output
Many Shortcuts accept input at the top, in the "Receive ... input from ..." step. From Terminal you supply that input with --input-path, and you save the result with --output-path:
# 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.txtHere is what happens:
- The tool reads
draft.txtand hands its contents to the Shortcut as input. - The Shortcut runs its actions, for example trimming whitespace and fixing capitalization.
- The final output of the Shortcut is written to
draft-clean.txt.
What counts as output
The output is whatever the last action in the Shortcut produces, or whatever an explicit "Stop and Output" action returns. If the Shortcut ends with an action that produces nothing, such as "Show Notification", there is nothing to save and the output file is empty or not created.
You can also write the output to the terminal. Use - (a dash) or send it to a path like /dev/stdout:
shortcuts run "Clean Up Text" --input-path ~/Documents/draft.txt --output-path - | pbcopyThe pipe sends the output to pbcopy, which puts it on the clipboard. Check that your macOS version accepts - for the path. If it doesn't, write to a temporary file and read it back.
Choosing the output type
Add --output-type when you need a specific format, such as a file type. The value is a uniform type identifier (UTI), a standard name for a data format. For example, public.plain-text means plain text and public.png means a PNG image:
shortcuts run "Make Thumbnail" \
--input-path ~/Pictures/photo.jpg \
--output-path ~/Pictures/photo-thumb.png \
--output-type public.pngIf you leave --output-type out, the tool picks a type based on the result, or on the extension of the output path.
Using Shortcuts in a script
Because shortcuts run blocks until the Shortcut finishes and returns an exit status, it fits in ordinary shell scripts. The following script cleans every .txt file in a folder.
#!/bin/bash
set -euo pipefail
src="$HOME/Documents/drafts"
dest="$HOME/Documents/clean"
mkdir -p "$dest"
for file in "$src"/*.txt; do
[ -e "$file" ] || continue
name=$(basename "$file" .txt)
if shortcuts run "Clean Up Text" \
--input-path "$file" \
--output-path "$dest/$name-clean.txt"; then
echo "Cleaned: $name"
else
echo "Failed: $name" >&2
fi
donePoints worth noting:
set -euo pipefailstops the script on unset variables and most errors. Theifform lets one failure be reported without stopping the whole loop.- The
[ -e "$file" ] || continueline handles an empty folder, where the glob*.txtstays as literal text. - Every path variable is quoted, so file names with spaces work.
Common errors and fixes
| Symptom | Likely cause | What to do |
|---|---|---|
| `Error: Couldn't find shortcut` | The name is misspelled, not quoted, or the Shortcut is in another account. | Run `shortcuts list` and copy the name exactly. Quote it. |
| The command hangs | The Shortcut is waiting for a permission dialog or a "Ask Each Time" prompt. | Look for a dialog on screen. Remove interactive prompts from Shortcuts you plan to script. |
| Output file is empty | The last action produces no output. | Add a "Stop and Output" action or reorder the actions. |
| Input is ignored | The Shortcut doesn't accept the input type you passed. | Open the Shortcut's details and set "Receive" to accept files or text. |
| Works in Terminal, fails from `launchd` | The background job lacks permissions or a logged-in GUI session. | Run it as a user agent in your login session, and grant the host process access in System Settings, Privacy & Security. |
Limits to keep in mind
- Shortcuts that show a user interface, such as a menu or a "Choose from List" prompt, need a person to answer. They are poor candidates for unattended scripts.
- Some actions need the Shortcuts app's context or a signed-in session. They can fail when run from an SSH session with no desktop.
- The tool runs Shortcuts that belong to the current user only.
Connecting back to Siri
Because the tool and Siri run the same Shortcut, a change made in one place applies to both. If you rename a Shortcut, the Siri phrase and the shortcuts run command both change. Scripts that use the old name break, so use identifiers for scripts that must survive renames.
You can also open a Shortcut from Terminal's side of things with shortcuts view "Name", which opens it in the Shortcuts app for editing. Use this when a scripted run fails and you want to inspect the actions.
Exercise
You have a Shortcut named "Summarize Notes" that takes a text file as input and ends with a "Stop and Output" action. Write the command that runs it on ~/notes/meeting.txt and saves the result to ~/notes/meeting-summary.txt. Then name one reason the output file might come out empty.
Show answer
shortcuts run "Summarize Notes" --input-path ~/notes/meeting.txt --output-path ~/notes/meeting-summary.txt. The file can be empty if the Shortcut's last action produces no output, for example if the "Stop and Output" action is missing or a later action such as "Show Notification" replaces the result with nothing.
Exercise
A script calls shortcuts run "Resize Photo" and never returns. Give two likely causes and how you would check each.
Show answer
First, a permission dialog or an "Ask Each Time" prompt is waiting for an answer. Check the screen for a dialog and open the Shortcut to look for interactive actions. Second, the Shortcut was launched from a context with no logged-in desktop session, such as SSH or a system-level job. Run the same command in a Terminal window in your own session and see whether it completes.