Repeat
Runs a list of steps a set number of times, once per item in a list, or while conditions hold.
Repeat count times
Keyboard Maestro: Repeat, While, For
Details #
Pick one of three modes. The first that applies wins, in this order:
forEach with in: runs the steps once per item. The current item is stored in the variable named by forEach.
count: runs the steps that many times.
while: checks the conditions before each pass and stops as soon as they fail.
Set indexVar to also get the pass number, counting from 1. After the loop, the variables keep their last values. The step's output holds iterations, the number of passes that ran.
in can be a list, a record, a text or an expression. A record gives one item per key, in key order, each shaped like {key, value}. A text gives one item per line. A bare name such as names is read as an expression. forEach without in fails.
Break out of repeat leaves the loop. Next repeat skips to the next pass.
Set progress to show how far the loop is: a message "Exporting · 3 of 7" with a bar and a Cancel button, updated before and after each pass. When the macro ends it turns into a check, or into the failure. On a long list it adds "about 40 s left" once it has seen three passes and five seconds, when at least ten seconds remain. A while loop has no length, so it shows the label and a moving stripe. Loops with progress inside each other show the inner count after the outer: "Folder 2 of 4 · file 3 of 7". A Show message step with the key run inside the loop changes the label.
- Set names to “Ada”, “Grace”, “Linus”
- Repeat for each name in names
- Show “{{ i }}: {{ name }}” for 1 s
Parameters #
| Parameter | Type | Required | Values | What it does |
|---|---|---|---|---|
steps | list of steps | Yes | ||
count | any value | A number, or {{ }} that gives one. A fraction is cut down to a whole number. Negative counts run zero times. | ||
forEach | text | Name of the variable that holds the current item. | ||
in | any value | The list to go through. Required with forEach. | ||
indexVar | text | Variable that holds the pass number, from 1. | ||
match | one of | all any | all (default) or any, for while. | |
max | whole number | Safety limit on passes, from 1 to 10,000. The default is 10,000. | ||
progress | any value | A label (supports {{ }}, read once when the loop starts), or true for the step's name, or the macro's. Shows the progress message described above. Leave it out to show nothing. | ||
while | list of condition | Conditions to keep going. Same condition types as If. |
Every step also accepts label, onFailure, timeout and dryRunSafe.
Examples #
- Repeat 8 times, showing “Exporting”
- Wait 1 s
- Set n to 0
- Repeat while n is below 3
- Add 1 to n
- Show “Ended at {{ n }}”
- Repeat 10 times
- If i is 3
- Break out of the repeat
Gotchas #
A step inside that fails stops the loop unless it handles its own failure.
Each pass appears in the Trace, labelled with its iteration number. A macro records at most 2,000 step rows, so very long loops get cut off in the Trace only.
while needs the condition to change. Update a variable inside the loop.