# Run macOS Shortcuts Remotely from the Command Line

Canonical: https://figment.so/blog/mac-shortcuts-command-line

Run `shortcuts run "Name"` from a script, with -i for file input and -o for file output. Test each shortcut's exit code and output size once, then schedule it as a LaunchAgent.

**Run a shortcut from a script with `shortcuts run "Name"`, pass files with `-i` and collect output with `-o`, check the exit code and output size once, then schedule it as a LaunchAgent in a logged-in user session.** Exit code `0` means success and `1` means an action in the shortcut failed.

Shortcuts can reach work that shell scripts cannot touch directly: PDF export, image processing through system frameworks, URL-scheme calls and cross-app automation. Apple's `shortcuts` tool makes any of them callable from a script, a CI step or a launchd job.

## Which `shortcuts` commands do I need?

Apple documents the tool in [Run shortcuts from the command line](https://support.apple.com/guide/shortcuts-mac/run-shortcuts-from-the-command-line-apd455c82f02/mac). These are the operations that matter for automation:

| Command | Purpose |
|---|---|
| `shortcuts run "Name"` | Execute a shortcut by name |
| `shortcuts run "Name" -i /path/to/file` | Pass one or more file paths as input |
| `shortcuts run "Name" -o /path/to/output` | Write output to a file |
| `shortcuts run "Name" --output-type public.pdf` | Pin the output format with a UTI |
| `shortcuts list` | List installed shortcuts |
| `shortcuts list --folders` | List shortcut folders |
| `shortcuts list -f FolderName` | List shortcuts in a folder |
| `shortcuts view "Name"` | Open the shortcut in the editor |
| `shortcuts sign --mode people-who-know-me --input in.shortcut --output out.shortcut` | Sign a shortcut for sharing |

Piping data into `shortcuts run` treats it as text input. Use `-i` when the shortcut should receive file references. Run `shortcuts --help` on your macOS version to confirm the current flags.

## What should I decide about each shortcut before automating it?

The common failure is assuming a shortcut handles inputs and errors the way it does when a person taps Run. Fill this in once per shortcut you plan to schedule:

| Field | Your shortcut's value |
|---|---|
| Shortcut name | Exact name as shown in Shortcuts.app |
| Input type | File path, text or none |
| Input format | For example `.jpg`, `.txt`, or no input |
| Output type | File, stdout text or none |
| Output format | For example `public.pdf`, `public.plain-text` |
| Minimum output size | Bytes or character count you consider valid |
| Side effects | Files written, URLs opened, emails sent |
| Needs user session | Yes for UI-touching actions, otherwise no |

The last row matters most. Remote desktop gives you a logged-in session, so UI-touching actions work interactively. A launchd daemon at system level is headless, so schedule UI-touching shortcuts as LaunchAgents that run in the user session, not as LaunchDaemons.

## How do I test a shortcut's exit status and output?

Run this once before wiring a shortcut into launchd or CI. It checks the exit code and the output size against your contract.

```bash
#!/usr/bin/env bash
# test-shortcut.sh — verify a shortcut's input/output contract
# Usage: ./test-shortcut.sh "My Shortcut" /path/to/input /tmp/out.pdf 1000
set -euo pipefail

SHORTCUT="${1:?name required}"
INPUT="${2:-}"
OUTPUT="${3:-}"
MIN_BYTES="${4:-1000}"
LOG=$(mktemp -t shortcut-test.XXXXXX)

echo "Testing: $SHORTCUT" | tee "$LOG"
CMD=(shortcuts run "$SHORTCUT")
[[ -n "$INPUT"  ]] && CMD+=(-i "$INPUT")
[[ -n "$OUTPUT" ]] && CMD+=(-o "$OUTPUT")

if ! "${CMD[@]}" >>"$LOG" 2>&1; then
  echo "FAIL: shortcuts run exited non-zero. See $LOG and Console.app." >&2
  exit 1
fi

if [[ -n "$OUTPUT" ]]; then
  [[ -f "$OUTPUT" ]] || { echo "FAIL: expected $OUTPUT not created" >&2; exit 1; }
  BYTES=$(wc -c < "$OUTPUT" | tr -d ' ')
  if (( BYTES < MIN_BYTES )); then
    echo "FAIL: output $BYTES bytes, expected >= $MIN_BYTES" >&2
    exit 1
  fi
  echo "PASS: $OUTPUT ($BYTES bytes)"
else
  echo "PASS: no output file expected"
fi
```

This is an example, not a benchmark. Run it on your own input and set `MIN_BYTES` to what "clearly not empty" means for the shortcut.

## How do I schedule a shortcut with launchd?

When the contract test passes, put the plist at `~/Library/LaunchAgents/com.example.nightly-shortcut.plist` so it runs in your user session:

```xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN"
  "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
  <key>Label</key><string>com.example.nightly-shortcut</string>
  <key>ProgramArguments</key>
  <array>
    <string>/usr/bin/shortcuts</string>
    <string>run</string>
    <string>My Shortcut Name</string>
    <string>-i</string><string>/Users/agent/inputs/source.jpg</string>
    <string>-o</string><string>/Users/agent/outputs/result.pdf</string>
  </array>
  <key>StartCalendarInterval</key>
  <dict>
    <key>Hour</key><integer>6</integer>
    <key>Minute</key><integer>30</integer>
  </dict>
  <key>StandardOutPath</key><string>/Users/agent/Library/Logs/nightly-shortcut.log</string>
  <key>StandardErrorPath</key><string>/Users/agent/Library/Logs/nightly-shortcut-err.log</string>
</dict>
</plist>
```

Load it with `launchctl bootstrap gui/$(id -u) ~/Library/LaunchAgents/com.example.nightly-shortcut.plist`. Each `<string>` in `ProgramArguments` is one argv entry, so a shortcut name with spaces needs no shell escaping. See Apple's [Creating Launch Daemons and Agents](https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/CreatingLaunchdJobs.html). Confirm the binary path with `which shortcuts` before pinning `/usr/bin/shortcuts`.

## What do I do when a scheduled shortcut fails?

| Symptom | Likely cause | Fix |
|---|---|---|
| Exit 1, no output | A shortcut action errored | Open Console.app, filter `shortcuts`, find the action error |
| Exit 1, "not found" | Name mismatch or shortcut removed | Run `shortcuts list` and match the exact name |
| Exit 0, no output file | Shortcut has no save-file action | Add "Save File", or drop `-o` from the contract |
| Output exists but empty | Action produced no data | Re-run with the real input; check the shortcut's expected input UTI |
| Works interactively, fails under launchd | Action needs the user session | Use a LaunchAgent, not a LaunchDaemon |

For a specific action failure, run the shortcut once from a terminal in a remote desktop session with Console.app open. The system log shows action-level detail that the CLI exit code does not.

## How do I move shortcuts to a new Mac?

Export each shortcut from Shortcuts.app (File, then Export, or `shortcuts sign` for a distributable file), import it on the new Mac, re-run the contract test with the same inputs, then load the plist.

## Does a Shortcuts run need an Apple silicon Mac?

`shortcuts run` executes locally on the Mac, so any plan runs it. The Intel Mac VM ($25 a month, $2 a day, macOS Sequoia) covers standard actions such as text handling, file transformation, PDF export and URL-scheme calls. Choose the Apple Silicon Mac VM ($39 a month, $6 a day) if an action needs Apple silicon, the shortcut calls an app that ships only for Apple silicon, or you need Xcode 26.4 or later ([Apple's porting guidance](https://developer.apple.com/documentation/apple-silicon/porting-your-macos-apps-to-apple-silicon)). VM vCPUs are shared, so watch `top` before adding concurrent jobs. See [Figment Mac Hosting](/mac-hosting).

**Related guides:**
- [Keep a hosted Mac up to date with automated maintenance](/blog/mac-agent-update-maintenance)
- [Rebuild a hosted Mac's Brewfile environment](/blog/mac-agent-brewfile-rebuild)
- [GitHub Actions self-hosted runner on a Mac](/blog/mac-github-actions-self-hosted-runner)
