Browse the docs

If

Runs one list of steps when its conditions pass and another when they don't.

If conditions

Keyboard Maestro: IfThenElse

Details #

Checks the conditions. If they pass, the steps in then run. If not, the steps in else run, when there are any. The Trace shows which branch ran, and what each condition compared.

By default every condition must pass (match: all). Set match to any to pass when one is enough. An empty list of conditions always passes.

Any condition type works here, the same ones as in Only if. Conditions can use {{ }} in their values.

Branch on a variable
Do
  1. Set count to 3
  2. If count is above 2
  3. Show “Plenty”
  4. Else
  5. Show “Not enough”
Open in Switchboard to try it

Parameters #

ParameterTypeRequiredValuesWhat it does
conditionslist of conditionYesA list of conditions. Required. Each may be negated.
thenlist of stepsYesSteps to run when the conditions pass. Required.
elselist of stepsSteps to run when they don't. Optional.
matchone ofall anyall (default) or any.

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

Examples #

Either of two conditions
Do
  1. If a file exists at ~/Desktop/a.txt and a file exists at ~/Desktop/b.txt
  2. Show “Found one”
Open in Switchboard to try it

Gotchas #

  • A step inside then or else that fails stops the macro, unless that step handles its own failure. If it still fails, this step's own onFailure applies.

  • A condition that can't be checked (a bad regular expression, comparing a list with <) fails the step. It does not count as false.

  • Return a value, Stop macro, Break out of repeat and Next repeat inside a branch act on the whole macro or the surrounding loop.

See also #

Repeat, Group, Set variable