Connect apps and files

Run Shortcuts from Terminal

Pass files to a Shortcut with the shortcuts command and capture its output without turning file paths into plain text.

10 minute lesson

~~~

macOS ships a command-line tool that turns every shortcut into something scripts can call. The shortcuts command runs a named shortcut. Use the input-path option when the input is a file rather than text from a pipe.

First, confirm the exact name you will call:

shortcuts list
# Prepare Screenshot
# Resize for Blog

Run a shortcut and write its output:

shortcuts run "Prepare Screenshot" \
  --input-path "$HOME/Desktop/sample.png" \
  --output-path "$HOME/Desktop/output"

Quote names and paths. Shortcut names contain spaces almost by definition, and an unquoted name becomes two arguments and a “shortcut not found” error.

Check the result the way you would check any command:

echo $?
# 0
ls "$HOME/Desktop/output"
# sample.jpg

A zero exit status means the shortcut finished. A non-zero one means it failed or was cancelled, which is exactly what a calling script needs to stop a pipeline. And the --output-path flag only receives something because the shortcut ends with Stop and Output — the contract from the previous lesson is what makes this bridge work.

The failure mode specific to this bridge is hidden interaction. Design command-line shortcuts without alerts or selection dialogs, because hidden interaction can leave automation waiting forever. When a scheduled script runs the shortcut, there is nobody in front of the dialog. The run does not error. It just hangs, holding its lock and its schedule slot, until you notice days later. If you take one rule from this lesson, take that one.

Expect a one-time permission prompt too: the first time your terminal runs a given shortcut, macOS may ask whether to allow it. Run the command interactively once and approve it before wiring it into anything unattended, otherwise the first scheduled run blocks on a question nobody sees.

Lesson completed

Take this course offline

Get every free book and course as PDF and EPUB files.

Get the download library →