Sub topic 10
Running shortcuts automatically with automations
This chapter assumes you can build a multi-step shortcut and use Shortcut Input. It also assumes you know the difference between running a shortcut by hand and running it from a trigger. The shell script example uses basic Terminal knowledge, which is optional. Automations are available in recent macOS versions, so the trigger list in your Shortcuts app may differ from the one described here.
What an automation is
An automation is a rule with two parts: a trigger and a shortcut. The trigger is an event the Mac can detect. When the event happens, the Mac runs the shortcut you attached to it.
Everything you have built so far starts when you click, press a key or choose a menu item. An automation removes that step. The shortcut itself is unchanged. Only the way it starts is different.
Where automations live
Open the Shortcuts app and look in the sidebar for the Automations section. If you don't see it, update macOS. Older versions don't include automations on the Mac.
Automations are listed separately from your shortcuts. Each entry shows the trigger and the shortcut it runs. An automation is a pointer to a shortcut, not a copy of it. If you edit the shortcut later, the automation runs the edited version.
Creating an automation
- Open Shortcuts and choose the Automations section in the sidebar.
- Click the add button and pick a trigger.
- Fill in the trigger's settings, such as which folder or which time.
- Choose the shortcut to run, or create a new one.
- Choose the run mode and save.
Trigger settings are specific to each trigger. A folder trigger asks which folder and whether to fire when items are added, removed or both. A time trigger asks for a time and a repeat pattern.
Common triggers
The exact list depends on your macOS version. These are the kinds you can expect.
| Trigger type | Fires when | Typical use |
|---|---|---|
| Time of Day | A set time arrives, with optional daily, weekly or monthly repeat | Daily backup, morning summary |
| Folder | Items are added to or removed from a chosen folder | Rename or convert new downloads |
| File | A specific file changes | React to a log or export file |
| Wi-Fi or Bluetooth | You join a network or connect a device | Change settings at the office |
| Focus | A Focus turns on or off | Open the apps you need for work |
| Email or Message | A matching message arrives | Log or forward certain messages |
Check your own list before you design around a trigger. If the trigger you want is missing, you may need a workaround, such as a time-based check or a Folder Action.
Run modes
Every automation has a run mode that controls whether it asks first.
- Run Immediately runs the shortcut as soon as the trigger fires, with no confirmation. This is the mode that makes it truly automatic.
- Ask Before Running shows a notification, and the shortcut runs only if you confirm.
- Notify When Run is an extra option. It shows a notification after the shortcut runs, so you know it happened.
Start with Ask Before Running while you test. Switch to Run Immediately once you trust the shortcut. A shortcut that deletes, moves or overwrites files is a bad candidate for Run Immediately until you have tested it on copies.
What the shortcut receives
When a trigger carries data, that data arrives as Shortcut Input, the same mechanism you used for Finder and Share Sheet input. For a Folder trigger, the input is the list of files that were added. Your shortcut can then loop over them with Repeat with Each, as you learned in the control flow chapter.
Other triggers pass no useful input. A Time of Day automation has nothing to give your shortcut, so Shortcut Input is empty. If your shortcut assumes it always has input, it will fail when started this way. Handle this with an If action that checks whether Shortcut Input has any value. You can then provide a fallback, such as a fixed folder path.
A shortcut that works with several start methods is easier to maintain. One shortcut can serve a Quick Action, a keyboard shortcut and an automation, as long as it handles missing input.
A worked example: log every new download
This automation records each file that lands in a folder. It combines a Folder trigger, Shortcut Input and a short shell script.
Step 1: build the shortcut
Create a shortcut named Log New Files with these actions:
- Run Shell Script, with Shell set to
/bin/zsh, Input set to Shortcut Input and Pass Input set to as arguments. - Paste in the script below.
for f in "$@"; do
printf '%s\t%s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "$f" >> "$HOME/Documents/shortcut-log.txt"
doneThe script receives each file path as an argument. For each one it appends a line with a timestamp and the path to a log file in Documents.
Step 2: test it by hand
Test before you attach any trigger. In Terminal, create a sample file and run the shortcut with it:
touch ~/Desktop/sample.txt
shortcuts run "Log New Files" --input-path ~/Desktop/sample.txt
cat ~/Documents/shortcut-log.txtYou should see one line with the current time and the path to sample.txt. If the file is missing or empty, fix the shortcut now. Debugging is harder once it runs unattended.
Step 3: attach the trigger
- In Automations, add a Folder trigger.
- Choose your Downloads folder and select the option for files being added.
- Choose Log New Files as the shortcut.
- Set the run mode to Ask Before Running for the first few days.
Download a file and confirm that a notification appears. After you confirm, a new line should appear in the log. If this works, switch the mode to Run Immediately.
Reliability and limits
Automations depend on the Mac's state, not only on your settings.
- The Mac must be awake. A trigger can't run a shortcut on a sleeping or shut-down Mac. A Time of Day automation at 03:00 won't fire if the Mac is asleep, and you shouldn't assume it runs when the Mac wakes.
- Interactive actions are a problem. Actions such as Ask for Input, Choose from Menu and Show Result need you to be present. With Run Immediately and nobody at the screen, they can stall or be missed. Prefer actions that need no interaction in an automation, or use Show Notification for status messages.
- Permissions apply. The shortcut needs the same access it would need if you ran it yourself, including files, apps and shell commands.
- Triggers are not guaranteed to be instant. Folder and file events can arrive in a burst. A download that writes a temporary file and then renames it can fire the trigger more than once. Filter the input if that matters, for example by skipping files that end in
.downloador.crdownload.
Debugging an automation
You can't step through an automation as it runs from a trigger, so debug the shortcut first and the trigger second.
- Run the shortcut manually with realistic input, and inspect each action's result.
- Run it from Terminal with
shortcuts runand--input-pathto simulate the input a trigger would supply. - Attach the trigger with Ask Before Running and fire it deliberately.
- Use Show Notification or a log file to leave a trace, as in the worked example.
- If nothing happens at all, check that the automation is enabled, that its settings match the real event (the right folder, the right direction) and that the Mac was awake.
A log file is the most useful tool here. It tells you whether the trigger fired, which input it carried and when.
Disabling and removing automations
Select an automation in the list to turn it off or delete it. Turn it off when you want to pause it without losing the setup. Deleting an automation does not delete the shortcut it ran.
Review your automations from time to time, especially any set to Run Immediately. They run without asking, so you should know what is active.
Folder Actions as an alternative
macOS has an older mechanism called Folder Actions, set up through Automator or the Finder's Services menu. It also reacts to files arriving in a folder. If the Shortcuts Folder trigger is unavailable on your Mac, or you need behavior it lacks, Folder Actions is the fallback. For new work, prefer a Shortcuts automation, because you can maintain it in the same place as your other shortcuts.
Exercise
You build an automation with a Folder trigger on ~/Downloads. The shortcut converts each new image to JPEG and saves the result back into ~/Downloads. After the first run, the Mac keeps creating files and never stops. What is the cause, and give two ways to fix it.
Show answer
The saved JPEG is a new file in the watched folder, so it fires the Folder trigger again, and the shortcut converts its own output in a loop. Fix it by saving results to a different folder, or by adding an If action that skips files that are already JPEG or that your shortcut created. Changing the run mode to Ask Before Running would help you stop the loop while testing but doesn't remove the cause.
Exercise
A Time of Day automation runs a shortcut that begins with Get File from Shortcut Input and then resizes it. When the time arrives, the shortcut fails. Why, and how would you make the shortcut work both from a Quick Action and from this automation?
Show answer
A Time of Day trigger passes no input, so Shortcut Input is empty and the first action has nothing to work on. Add an If action that checks whether Shortcut Input has any value. If it does, use it. Otherwise, use a fallback such as a file chosen from a fixed folder with Get Contents of Folder. The same shortcut then works from both start methods.