Sub topic 5
Making decisions and repeating steps
This chapter covers the actions that change a shortcut's path: If, Otherwise, Repeat, Repeat with Each and Choose from Menu. It assumes you can create a shortcut, add actions, and use variables and magic variables from the earlier chapters. It also assumes you have used Ask for Input and Show Result.
Why control flow matters
A shortcut without control flow runs the same steps in the same order every time. Control flow lets it react to its data. It can skip a step, take another branch, or do the same work for every item in a list.
The Shortcuts app on macOS gives you these control flow actions:
| Action | What it does | Typical use |
|---|---|---|
| If | Runs its steps only when a condition is true | Skip files that are too large |
| Otherwise | Runs when the If condition is false | Show a different message |
| End If | Closes an If block | Added automatically |
| Repeat | Runs its steps a fixed number of times | Retry, or do something three times |
| Repeat with Each | Runs its steps once for each item in a list | Process every file |
| Choose from Menu | Lets the user pick a branch | Pick an output format |
| Stop and Output | Ends the shortcut and returns a value | Exit early with a result |
Each block action has a matching closing marker, shown as an End action in the editor. The editor adds the marker for you. Everything between the opening action and the End marker belongs to that block, and the editor indents it.
The If action
If tests one value against one condition. You set two things: the input, and the condition.
- Search for If in the action list and add it.
- Click the input field and choose a variable, such as the output of an earlier action.
- Click the condition and choose an operator.
- Enter the comparison value if the operator needs one.
- Add actions inside the If branch. Optionally add Otherwise and put other actions there.
The operators available depend on the type of the input. Text offers is, is not, contains, does not contain, begins with, ends with and has any value. Numbers offer is equal to, is greater than, is less than and is between. This is why the content types from the data chapter matter. The same value can behave differently depending on whether Shortcuts treats it as text or as a number.
The pattern is easiest to see in a small example. This shortcut asks for a number and responds differently depending on its size:
- Ask for Input with the input type set to Number and the prompt "How many files?"
- If Provided Input is greater than
100 - Inside the If branch: Show Alert with the text "That is a large batch."
- Otherwise
- Inside the Otherwise branch: Show Result with the text "Small batch, continuing."
- End If
Checking for empty values
A common check is whether an earlier action produced anything. The operator has any value is true when the input exists and is not empty. Its opposite, does not have any value, is true when nothing came back. Use these after actions that can return nothing, such as Find Files, and before using the result.
Combining conditions
An If action tests one condition only. There is no built-in "and" or "or" in the basic If. You have two common workarounds:
- Nest the Ifs to get "and". Place a second If inside the first one's branch. Both conditions must be true to reach the inner actions.
- Use Otherwise If style chaining for "or". Repeat the same action in two separate If blocks, or collect the outcomes in a variable.
Deeply nested blocks become hard to read. If you find yourself three levels deep, consider whether Choose from Menu or a different approach would be clearer.
Choose from Menu
Choose from Menu pauses the shortcut and shows the user a list of options. Each option has its own branch of actions.
- Add Choose from Menu.
- Set the prompt, for example "Convert to which format?"
- Edit the menu items, for example "PNG" and "JPEG".
- Add actions under each item.
Only the branch the user picks runs. After it finishes, the shortcut continues after the End Menu marker. The chosen item's title is also available as the action's output, which you can use in later steps.
The choice between If and Choose from Menu comes down to who decides:
- Use If when the data decides.
- Use Choose from Menu when the person running the shortcut decides.
A shortcut run from an automation or from a Finder Quick Action may be running with no one watching. A menu in that case waits for an answer, so avoid it in fully unattended shortcuts.
Repeat
Repeat runs the actions inside it a fixed number of times. You set the count in the action.
Inside a Repeat block, the Repeat Index magic variable holds the current pass number, starting at 1. Use it to number things, such as file names.
This example shows a numbered message three times:
- Repeat
3times - Show Notification with the text "Pass " followed by the Repeat Index variable
- End Repeat
Repeat is useful for retrying and for generating a set number of items. When you have a list to go through, use Repeat with Each instead.
Repeat with Each
Repeat with Each runs its actions once for every item in a list. The list can be files from Finder, lines of text, or results from any action that returns several items.
Inside the block you get two magic variables:
- Repeat Item is the current item.
- Repeat Index is the current position, starting at 1.
Here is a practical example. It takes the files passed to a shortcut and tells you which ones are over a size limit:
- Receive files through Shortcut Input, covered in a later chapter. For now, use Select File and allow multiple selection.
- Repeat with Each item in Files
- Inside: Get Details of Files and choose File Size for Repeat Item.
- If File Size is greater than
10MB - Inside the If: Add to Variable named
LargeFiles, adding Repeat Item. - End If
- End Repeat
- Show Result with the variable
LargeFiles.
Before the loop, add Set Variable to initialize LargeFiles. A good way is to set it to an empty text value. This makes sure the variable exists and starts clean each time the shortcut runs.
How data leaves the loop
By default, the output of a Repeat with Each action is a list of the final result of each pass. Whatever the last action inside the loop produces for each item is collected, and the combined list becomes the output after End Repeat. This often removes the need for a variable.
If the last action in a pass produces nothing, that pass adds nothing to the list. If different branches produce different types, the list can mix types, which can cause trouble in later actions.
Use Add to Variable when you want full control over what is collected. Use the loop's own output when every pass produces one value.
Variables inside blocks
Variables created inside a block are still available after it ends, but you must be careful about when they have a value.
- A magic variable from an action inside an If branch only has a value if that branch ran. After the End If, using it may give an empty value.
- A variable set with Set Variable inside a loop is replaced on every pass. Only the last value remains.
- Add to Variable appends instead of replacing, so use it for accumulating results.
Stopping early
Two actions help you end a shortcut cleanly:
- Stop and Output ends the shortcut and returns a value to whatever called it, such as Finder or Terminal.
- Nothing is used when you need an explicit empty value, for instance in a branch where you want no output.
A common pattern is an early exit: check the input first, and if it is invalid, show an alert and stop. This keeps the main steps free of deep nesting.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| If never runs its branch | Comparing text with a numeric operator, or the wrong variable chosen | Check the input type and pick the variable again |
| Repeat with Each runs zero times | The list is empty because an earlier action found nothing | Add an If with "has any value" before the loop |
| Only the last item remains after the loop | Set Variable used instead of Add to Variable | Use Add to Variable |
| Output list contains unexpected empty entries | Some passes ended without a value | Return a value on every branch, or filter after the loop |
| A shortcut hangs when run automatically | Choose from Menu or Ask for Input is waiting for a person | Replace prompts with fixed values in unattended shortcuts |
| The loop runs far too long | A very large list or a slow action inside the loop | Filter the list first, and move slow actions out of the loop |
Testing control flow
Control flow is where bugs appear, because only some paths run on any given test. Test each branch on purpose:
- Run the shortcut with data that makes the If true.
- Run it again with data that makes it false.
- Run it with an empty list or empty input.
- Run it with a single item, then with several.
The editor's Run button and the result previews on each action show what happened in each pass. The debugging chapter covers this in more detail.
Exercise
A shortcut uses Repeat with Each over a list of five files. Inside the loop, it uses Set Variable to store each file's name in a variable called Names. After the loop, Show Result displays Names. What does the user see, and how would you change the shortcut to show all five names?
Show answer
The user sees only the fifth file's name, because Set Variable replaces the value on every pass. Replace Set Variable inside the loop with Add to Variable (after initializing Names before the loop), so each name is appended and Names holds all five.
Exercise
You want a shortcut to ask "Resize or Convert?" and then run different steps for each answer. It will be run by a person from the menu bar. Should you use If or Choose from Menu, and why?
Show answer
Use Choose from Menu. The person running the shortcut makes the decision, not the data, and a menu gives each answer its own branch of actions. If would require storing the answer first and testing it, which adds steps without benefit.
Exercise
A Repeat with Each loop runs over the output of Find Files. The shortcut sometimes finds no files, and the later steps then show confusing errors. Which action would you add, and where?
Show answer
Add an If action before the loop, with the Find Files output as input and the condition "has any value". Put the loop inside the If branch, and use Otherwise to show a message such as "No files found" or to stop the shortcut.