Sub topic 5
Making decisions and repeating steps
This chapter assumes you can create a shortcut, add actions, and use variables and magic variables from the earlier chapters. You will add three kinds of action that change the order in which a shortcut runs: If, Repeat and Choose from Menu.
Why a shortcut needs control flow
A plain shortcut runs its actions from top to bottom, once. That is fine when every run should behave the same. Real tasks usually need more. You may want to treat PDFs differently from images, or apply one step to every file in a folder. Control flow actions let a shortcut branch and loop.
If: choosing a branch
The If action tests a value and runs one group of actions or another. Each If creates three markers in the editor: If, Otherwise and End If. Actions placed between the markers belong to that branch. The Otherwise branch is optional, so you can delete it if you only need to act when the test is true.
An If has two parts:
- The input. This is the value being tested. By default it is the output of the previous action, but you can click it and pick any variable.
- The condition. This is the test applied to the input. The choices depend on the input's content type.
| Input type | Typical conditions |
|---|---|
| Text | is, is not, contains, begins with, ends with, has any value |
| Number | is, is greater than, is less than, is between |
| File | name, extension or size, after you extract it with Get Details of Files |
Pick the condition that matches the type. A common error is testing text that looks like a number. The text "10" and the number 10 are different types, so a numeric comparison may not behave as you expect. Convert the value with a Get Numbers from Input action first.
You can nest one If inside another. For three or more outcomes, a nested If works, but Choose from Menu is often clearer. It shows a list of options and runs the branch the user picks.
Repeat and Repeat with Each
Two actions create loops:
- Repeat runs the enclosed actions a fixed number of times. It sets the magic variable Repeat Index, which counts 1, 2, 3 and so on.
- Repeat with Each runs the enclosed actions once per item in a list. It sets Repeat Item (the current item) and Repeat Index.
Use Repeat with Each whenever your data is already a list, such as the files from a Select Files action or Shortcut Input. The output of a Repeat block is a list that collects the final result of each pass. The actions after End Repeat can use that list.
Worked example: sort PDFs from a batch of files
This shortcut asks for files, then moves only the PDFs into a folder.
- Add Select File and turn on Select Multiple.
- Add Repeat with Each. It receives the selected files.
- Inside the loop, add Get Details of Files. Set it to File Extension and use Repeat Item as the input.
- Add If. Set the condition to File Extension is
pdf. - In the If branch, add Move File. Use Repeat Item as the file and choose a destination folder.
- After End Repeat, add Show Notification with a message such as "Done".
Common errors
- Using the wrong input. Inside a loop, an action may default to the previous action's output instead of Repeat Item. Check each input's variable.
- Placing actions outside the block. If an action sits after End If or End Repeat, it runs once, not per item. Drag it between the markers.
- Empty branches. An If with nothing in a branch is legal but usually means you forgot a step.
- Testing before data exists. If the input is empty, a "contains" test is false. Add a has any value check first when the input might be missing.
Exercise
A shortcut uses Repeat with Each over five selected files. After the End Repeat marker you want to show how many files were processed. Which magic variable or output can you use, and where do you place the action?
Show answer
Place a Count action after End Repeat, set to count the Repeat Results (or the original file list), then pass the number to Show Result. Placing it inside the loop would run it five times instead of once.