Browse the docs

Variable

Passes when a variable compares true against a value.

name op value

Keyboard Maestro: Variable, Text, Calculation

Details #

Reads the variable name from the run and compares it with value using op. name is the variable's name, not text to fill in; value may use {{ }} expressions. A variable that was never set is nothing, and counts as empty.

Operators: == or eq, != or ne, < or lt, <= or lte, > or gt, >= or gte, contains, startsWith, endsWith, matches, isEmpty, before and after (alphabetical order of the two texts).

  • Equality is loose: the number 5 equals the text "5". Two texts must be identical.

  • Ordering compares numbers (or text that reads as a number) as numbers and other text as text. Comparing things that can't be ordered, such as a list and a number, is an error.

  • contains looks for an item in a list, a key in a record, or a piece of text in text.

  • matches searches for a regular expression, case sensitive.

  • isEmpty is true for nothing, empty text, an empty list or an empty record. It ignores value.

Every condition accepts "negate": true, which flips the verdict. The trace records what was compared before the flip, so a negated condition that blocks a macro still shows the real values.

Conditions in an Only if list combine with all: every one must pass, and an empty list always passes. An if step or a repeat loop takes a match of all (default) or any.

The trace shows the variable's value under its own name, plus op and value.

Parameters #

ParameterTypeRequiredValuesWhat it does
nametextYesThe variable to read.
opone ofYeseq ne gt lt == != < <= > >= contains matches isEmpty gte lte startsWith endsWith after beforeOne of the operators above.
valueany valueWhat to compare with. Omit for isEmpty.

Examples #

Branch on a count
Do
  1. Set count to “3”
  2. If count is at least “3”
  3. Show “Three or more”
  4. Else
  5. Show “Fewer than three”
Open in Switchboard to try it

Gotchas #

  • An onlyIf list is checked before the first step, so a variable set inside do isn't visible to it. Use an if step to test a variable you set during the run, as the example does.

  • When a trigger is being considered, the check runs before any run exists. Variables are empty then, so a variable condition belongs in an if step in most macros.

See also #