Sub topic 12
Running shortcuts from Terminal with the shortcuts command
This chapter assumes you have built at least one shortcut in the Shortcuts app and that you can open Terminal and run basic commands such as cd and ls. You should also know what Shortcut Input is and how a Run Shell Script action receives input.
Why use the command line
The Shortcuts app on macOS includes a command-line tool named shortcuts. It lets you run a shortcut from Terminal, from another script, or from a scheduled job. This is useful when you want to chain a shortcut into a larger shell workflow, or when you want to test a shortcut without clicking through the app.
The tool talks to the Shortcuts app on your Mac. It uses the shortcuts in your own library, so a shortcut must exist there before you can run it.
Find the exact shortcut name
You run a shortcut by name, and the name must match exactly. Start by listing the shortcuts in your library.
shortcuts listThe command prints one shortcut name per line. Copy the name from this output rather than typing it from memory. Spaces, capital letters and punctuation all count.
Run a shortcut
The basic form is shortcuts run followed by the name. Put the name in quotes if it contains spaces.
shortcuts run "Uppercase Text" --input-path ~/Desktop/note.txtThis runs a shortcut named Uppercase Text and gives it the file note.txt as its input. Inside the shortcut, that file arrives as Shortcut Input, the same as if you had chosen it from a Finder Quick Action.
Here is the shortcut that this command assumes. It receives the file and runs a Run Shell Script action with input passed to stdin. The script is:
tr '[:lower:]' '[:upper:]'The tr command reads the text from stdin and converts each lowercase letter to uppercase. The converted text becomes the output of the action.
How the pieces connect
Pass input and capture output
You can supply input in more than one way. The options below are the ones you will use most.
| Option | What it does |
|---|---|
| `--input-path` | Uses the file at this path as the shortcut's input. |
| `--output-path` | Writes the shortcut's output to this path. |
| `--output-type` | Sets the content type of the output, as a uniform type identifier. |
To save the result instead of printing it, add --output-path.
shortcuts run "Uppercase Text" \
--input-path ~/Desktop/note.txt \
--output-path ~/Desktop/note-upper.txtIf you give no --output-path, the output goes to standard output. Check shortcuts run --help on your Mac for the exact options your macOS version supports, because the available flags can differ between versions.
Pass files as arguments
A shortcut can pass the files it received to a shell script as arguments instead of stdin. In the Run Shell Script action, set Pass Input to "as arguments". Then each file arrives as a path in "$@".
for f in "$@"; do
wc -c < "$f"
doneThis prints the size in bytes of each file the shortcut received. Quote "$f" so that file names with spaces still work.
Common errors
- The shortcut is not found. The name does not match. Run
shortcuts listand copy the name exactly. - The command hangs. The shortcut may be waiting for you. If it uses Ask for Input, Choose from Menu or Show Result, it opens a prompt in the Shortcuts app and waits there. Shortcuts meant for Terminal should avoid prompts.
- A permission prompt appears. The shortcut may need access to a folder or an app. macOS asks once, and you approve it in the dialog.
- The output is empty. Check that the last action in the shortcut produces the output you expect. Open the shortcut and use the step-by-step results from the debugging chapter.
- Spaces break the file path. Quote any path that contains spaces, or escape the spaces with a backslash.
Exercises
Exercise
You typed shortcuts run Uppercase Text and got an error. What is the likely cause, and how do you fix it?
Show answer
The name contains a space, so the shell treats it as two separate words. Put the name in quotes: shortcuts run "Uppercase Text". If it still fails, run shortcuts list and confirm the name matches exactly.
Exercise
Write a command that runs a shortcut named "Count Bytes" with the file ~/Desktop/report.pdf as input and saves the output to ~/Desktop/size.txt.
Show answer
shortcuts run "Count Bytes" --input-path ~/Desktop/report.pdf --output-path ~/Desktop/size.txt