Browse the docs

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:

  1. forEach with in: runs the steps once per item. The current item is stored in the variable named by forEach.

  2. count: runs the steps that many times.

  3. 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.

Say each name
Do
  1. Set names to “Ada”, “Grace”, “Linus”
  2. Repeat for each name in names
  3. Show “{{ i }}: {{ name }}” for 1 s
Open in Switchboard to try it

Parameters #

ParameterTypeRequiredValuesWhat it does
stepslist of stepsYes
countany valueA number, or {{ }} that gives one. A fraction is cut down to a whole number. Negative counts run zero times.
forEachtextName of the variable that holds the current item.
inany valueThe list to go through. Required with forEach.
indexVartextVariable that holds the pass number, from 1.
matchone ofall anyall (default) or any, for while.
maxwhole numberSafety limit on passes, from 1 to 10,000. The default is 10,000.
progressany valueA 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.
whilelist of conditionConditions to keep going. Same condition types as If.

Every step also accepts label, onFailure, timeout and dryRunSafe.

Examples #

Export with a progress message
Do
  1. Repeat 8 times, showing “Exporting”
  2. Wait 1 s
Open in Switchboard to try it
Loop until a counter reaches 3
Do
  1. Set n to 0
  2. Repeat while n is below 3
  3. Add 1 to n
  4. Show “Ended at {{ n }}”
Open in Switchboard to try it
Stop early
Do
  1. Repeat 10 times
  2. If i is 3
  3. Break out of the repeat
Open in Switchboard to try it

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.

See also #

Break out of repeat, Next repeat, If, Set variable