Browse the docs

Compare text

Passes when two values you give it compare the way you ask, such as equal, greater than, contains or matches.

left op value

Keyboard Maestro: Text, Calculation, Clipboard, Environment Variable, Typed String, Variable

Details #

Compares left with value using op. Unlike Variable, which reads a variable by name, left is any value or {{ }} template, so you can test an expression, a token or a function result directly: {{length(name)}}, {{clipboard}}, {{env("HOME")}}.

The operators:

  • eq, ne: equal and not equal. A number equals text that reads as the same number, so 3 equals "3". Two pieces of text must be identical. Lists and records compare item by item.

  • lt, lte, gt, gte: order two numbers, or two pieces of text. If the two sides can't be ordered, the condition fails with a message such as the left side (list) can't be compared with number using gt.

  • contains: for text, a substring test; for a list, whether an item equals value; for a record, whether it has the key value.

  • startsWith, endsWith: text tests.

  • matches: value is a regular expression searched anywhere in left. An invalid pattern fails with '...' isn't a valid regular expression.

  • isEmpty: passes for an empty text, list or record, and for nothing at all. value is ignored.

  • before, after: alphabetical order of the two texts.

The trace shows left (cut to 200 characters), op and 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.

Parameters #

ParameterTypeRequiredValuesWhat it does
leftany valueYesThe value to test. Any text, number, list or record, usually a {{ }} template.
opone ofYeseq ne lt lte gt gte contains startsWith endsWith matches isEmpty before afterOne of eq, ne, lt, lte, gt, gte, contains, startsWith, endsWith, matches, isEmpty, before, after. Required.
caseInsensitiveyes / notrue ignores upper and lower case in eq, ne, contains, startsWith, endsWith, before and after. Default false. It has no effect on lt, gt or matches.
valueany valueWhat to compare with. Defaults to empty text.

Examples #

Is the text long enough
Do
  1. Set title to “Quarterly report”
  2. If {{length(title)}} is above 10
  3. Show “Longer than 10 characters”
  4. Else
  5. Show “10 or fewer”
Open in Switchboard to try it
Case-insensitive starts with
Do
  1. Set city to “Paris”
  2. If {{city}} starts with “pa”
  3. Show “Starts with pa”
Open in Switchboard to try it

Gotchas #

  • With caseInsensitive left off the comparison is exact, so Paris is not paris.

  • lt, lte, gt and gte fail (rather than return false) when the sides can't be ordered, for example a list against a number.

  • matches is a regular expression, not a wildcard pattern.

See also #