Sub topic 9
Testing and Debugging Your Shortcut
This chapter assumes you can build a multi-step shortcut in the Shortcuts app. You should know how actions pass output to the next action, how variables and magic variables work, and how If and Repeat actions behave. Terminal experience helps for the last section but is not required.
Shortcuts have no compiler and no debugger in the usual sense. You debug by running the shortcut, looking at what each action produced, and comparing it with what you expected. The Mac editor gives you several tools for this. Learn them in order, from the cheapest check to the most invasive.
Why shortcuts fail
Most failures fall into a few groups. Knowing them tells you where to look first.
| Symptom | Usual cause |
|---|---|
| An action receives nothing, or the wrong thing | The previous action's output was not what you assumed, or a variable was set to a different value than you thought |
| The shortcut does nothing when run from Finder | Shortcut Input is empty, or the input types do not accept the files you selected |
| An If branch never runs | The comparison uses the wrong type, such as text "10" compared with a number |
| A Repeat loop runs once, or not at all | The list has one item, or is empty |
| A permission prompt appears, or the action fails | The shortcut has not been allowed to access a file, folder, app or script |
| Output looks right in the editor but wrong in a script | Text with trailing newlines, or a list passed where a single value was expected |
Running the shortcut from the editor
Open the shortcut in the editor and click the Run button (the triangle) in the toolbar. The shortcut runs from the first action to the last. A progress indicator appears in the toolbar while it runs. If an action fails, the shortcut stops and shows an error message that names the action.
Running from the editor gives you the same behavior as running from the menu bar, with one difference. Nothing passes in as Shortcut Input unless you supply it. If your shortcut expects files from Finder or text from another app, running it in the editor tests only the "no input" case. Test each way the shortcut will really be used: with input, and without it.
Testing Shortcut Input
To test with input, run the shortcut from the place it will run in real use. For a Finder Quick Action, select a file in Finder and run the Quick Action from the context menu. For a shortcut that accepts text, select text in an app and use the Services menu if you enabled it for that shortcut.
Add a temporary Show Result action as the first action, with Shortcut Input as its content. This shows exactly what arrived. Remove it when you are done, or leave it disabled by deleting it once the shortcut works.
Inspecting action results
When a shortcut runs, each action can show a small result below it in the editor. Click the result area to open a larger preview of what that action produced. This is the main debugging tool. Walk down the shortcut and, at each action, ask two questions:
- Is this output what I expected?
- Is it what the next action needs?
A common problem is that an action produces a list when the next action expects one item. A Get Contents of Folder action returns many files. If you feed that into an action that handles one file, the action may run once for each item, or only on the first, depending on the action. The result preview shows how many items there are. Check the count first.
Checking the type of a value
Each result preview shows what kind of content it is: text, number, file, URL, date, list, or dictionary. A value that looks like a number may be text. If an If action compares "Is greater than 5", Shortcuts tries to treat the input in a way that fits the condition, but text that includes spaces or units, such as "12 MB", will not compare as a number. Use a Get Numbers from Input action, or a Get Details of Files action, to get a true number.
Show Result and Quick Look as probes
Because you cannot set a breakpoint, add probes. A probe is an action you insert only to see a value.
- Show Result displays text, numbers or other values in a dialog and stops the shortcut until you click Done. Use it for values you want to read.
- Quick Look previews a file, image or document. Use it when the value is a file and you need to see its contents.
- Show Notification displays a message without stopping the shortcut. It is useful inside Repeat loops, where a dialog on every pass would be tedious.
Label each probe. Put a Text action containing the label, such as after filter, before the value in the Show Result, or use a Text action that combines the label with a variable. When several probes fire in a row, labels tell you which is which.
Disabling and isolating actions
Binary search works well for shortcuts. If a long shortcut fails, find the middle, put a Show Result there, and run it. If the value is right, the fault lies after that point. If it is wrong, the fault lies before it. Repeat on the half that contains the fault.
You can also add a Stop and Output action, or a Nothing action, to end the shortcut early at the point you are testing. Actions after it do not run, so you can build and verify a shortcut in stages. Remove the early stop when the stage is verified.
Debugging variables
Variables are the most common source of subtle faults.
- Stale values. A variable keeps the value from its latest Set Variable action. If you set it inside an If branch, it may still hold an older value when the branch does not run.
- Name mismatch. Variable names are matched exactly. A variable named
Countand one namedcountare different, and you will get empty output if you refer to one you never set. - Magic variables from a loop. Inside a Repeat with Each, the Repeat Item is the current item. After the loop ends, Repeat Results holds the collected outputs. Using Repeat Item outside the loop gives you nothing useful.
- Accumulation. To build up a list, use Add to Variable. Using Set Variable inside a loop overwrites the value on each pass, so you end with only the last item.
To see a variable's current value, insert a Show Result with that variable and run the shortcut. The type picker on a variable token lets you view its properties, such as Name or File Size, so you can confirm you chose the right property.
Debugging control flow
For an If action, probe the value being tested immediately before the If. For a Repeat, probe the list before the loop, and probe the Repeat Item inside it. An empty list produces no passes, and a Repeat with Each over a single value runs once. Both look like "the loop is broken" when the real fault is earlier.
Permissions and prompts
Some failures are not logic faults. Shortcuts asks for your approval the first time an action reaches into something sensitive, such as a file location, another app, or a script. If you click Don't Allow, the action fails the next time too, and the error can look like a bug in the shortcut.
When an action fails with a permission-related message:
- Run the shortcut from the editor so you can see any prompts.
- Read the prompt and allow it if you trust the shortcut.
- Check System Settings, in the Privacy & Security section, for the app that is running the shortcut if it needs access to files or folders.
Review a shortcut's privacy settings through its details pane in the editor. The sharing chapter covers permission review in more detail.
Debugging shell script actions
A Run Shell Script action is hard to inspect because its work happens outside the editor. Two habits help.
First, test the script in Terminal before you put it into the action. Run it with sample input, and see what it prints. Second, remember how input arrives. With input passed to stdin, the script reads standard input. With input passed as arguments, the script reads "$@". A script written for one mode fails silently in the other.
This script expects arguments and prints the size of each file:
for f in "$@"; do
wc -c < "$f"
doneIf you set the action to pass input to stdin, "$@" is empty, the loop never runs, and the output is empty. The fix is to change the action's Pass Input setting, not the script.
The shell's output becomes the action's result. Text that ends with a newline can affect later comparisons, so probe the result with Show Result and check that it looks right. Error messages from the script go to stderr. If the action shows an error, run the same script in Terminal to read it in full.
Debugging from Terminal
The shortcuts command lets you run a shortcut outside the editor, which helps when you want a repeatable test with a known input. First list your shortcuts so you can copy the name exactly:
shortcuts listThen run the shortcut with a test file:
shortcuts run "Uppercase Text" --input-path ~/Desktop/note.txtNames must match exactly, including capitals and spaces. If the name is wrong, the command reports that it cannot find the shortcut. A Terminal run is a good regression test. After you change the shortcut, run the same command with the same file and compare the output with the earlier result.
A debugging checklist
- Run the shortcut in the editor and note where it stops, or where the output first looks wrong.
- Open the result preview of each action in order. Check type and count as well as value.
- Insert a labeled Show Result before the first doubtful action.
- Fix one thing, then run again. Changing several things at once hides the cause.
- Test with the real input and from the real trigger.
- Remove probes and early stops.
Exercise
A shortcut uses Repeat with Each over the files from Shortcut Input. Inside the loop, Set Variable stores each file's name in a variable called Names. After the loop, Show Result displays Names, but only the last file's name appears. What is wrong and how do you fix it?
Show answer
Set Variable overwrites the variable on every pass, so only the final value remains. Replace it with Add to Variable inside the loop, which appends each name to a list. After the loop, Names holds every name. To confirm, add a Show Result before the loop to check how many files arrived.
Exercise
Your Finder Quick Action works when you click Run in the editor but does nothing when you use it on a selected file in Finder. Name two likely causes and one way to check each.
Show answer
First, the shortcut may not be receiving the file as Shortcut Input, because the accepted input types or the Finder Quick Action setting do not match. Check by adding a Show Result with Shortcut Input as the first action and running it from Finder. Second, a permission may have been denied, so a file or folder action fails. Check by running from the editor to see the prompt, allowing it, and reviewing Privacy & Security in System Settings.