Sub topic 9
Testing and debugging your shortcut
This chapter assumes you can build a multi-step shortcut with variables, Ask for Input, and If or Repeat actions. You should also know how to open the shortcut editor. Here you will learn how to find out why a shortcut misbehaves and how to fix it.
How the editor runs a shortcut
When you click the Run button (the triangle) in the editor toolbar, the shortcut runs from the top action to the bottom one. Each action shows a small result indicator as it finishes. A shortcut that stops partway shows an error banner on the action that failed.
Most bugs fall into three groups:
- An action received the wrong data, such as a file where it expected text.
- A variable was empty or held an old value.
- A branch of an If or Repeat ran when you did not expect it, or did not run when you did.
Inspect results as the shortcut runs
After a run, each action that produced output shows a result below it. Click the result bubble to open Quick Look at the output. You can see the exact text, number, list or file that the next action received.
When a shortcut gives a wrong answer, work down from the top. Find the first action whose result is not what you expected. That action, or the one just before it, holds the bug.
The Show Result action also helps. Add it after a suspicious step to display the value on screen, then delete it when you are done.
Test one piece at a time
Run only part of a shortcut by selecting the actions you want to test. A selected group has its own Run option in the context menu, so you can check one stage without running the rest. Because later steps rely on earlier output, a partial run works best on actions that do not depend on a variable you set above them.
A practical method is to build in small stages:
- Add two or three actions.
- Run the shortcut and check the results.
- Add the next stage only when the current one works.
Common errors and fixes
| Symptom | Likely cause | Fix |
|---|---|---|
| "Nothing was passed" or an empty result | A variable was never set, or an action ran before the one that sets it | Check the order of actions and click the variable to see its source |
| Wrong text in the output | A Get Variable or magic variable points to an earlier action | Click the variable bubble and choose the correct source |
| If branch never runs | The comparison uses the wrong type or value, such as text "5" against a number | Check the condition and the type of the value |
| Repeat runs the wrong number of times | The list has more or fewer items than you thought | Add Count before the Repeat and show the result |
| Shell script gives no output | The input mode (stdin or arguments) does not match the script | Match the mode to the script, as in the earlier shell chapter |
Watch the data flow
The diagram shows a typical debugging loop. You run, find the first wrong result, fix it, and run again.
Testing with real input
A shortcut that receives files or text through Shortcut Input behaves differently inside the editor, because nothing is passed in. To test it, open the shortcut details and turn on the input types it accepts. You can then run it from Finder, from a Quick Action, or from Terminal with a known file:
shortcuts run "Uppercase Text" --input-path ~/Desktop/note.txtUsing a small test file with known contents makes it easy to tell whether the output is right. Test the edge cases too: an empty file, a file with a long name, and a case where you cancel a prompt.
Handling failures on purpose
Some failures are normal, such as a user cancelling a prompt or a folder having no matching files. Use an If action to test for an empty result, and use Stop and Output or Show Alert to end the shortcut with a clear message. This is easier to debug than a shortcut that fails silently.
Exercise
A shortcut should count the files passed to it and show the number. It always shows 0 when run from a Finder Quick Action. After running it in the editor, what should you check first, and why?
Show answer
Check whether Shortcut Input is set to accept files, and look at the result of the first action after the input. In the editor nothing is passed in, so a test there can look fine or empty. From Finder the input must arrive as files. If the input type is wrong or unset, Count receives nothing and returns 0.
Exercise
Your Repeat with Each loop runs five times, but you expected three. Name two ways to find the cause.
Show answer
Add a Count action before the loop and show its result to see the real list length. Or open the result bubble of the action that builds the list and read its items. Extra items often come from folders, hidden files or duplicates.