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, so3equals"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 asthe left side (list) can't be compared with number using gt.contains: for text, a substring test; for a list, whether an item equalsvalue; for a record, whether it has the keyvalue.startsWith,endsWith: text tests.matches:valueis a regular expression searched anywhere inleft. 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.valueis 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 #
| Parameter | Type | Required | Values | What it does |
|---|---|---|---|---|
left | any value | Yes | The value to test. Any text, number, list or record, usually a {{ }} template. | |
op | one of | Yes | eq ne lt lte gt gte contains startsWith endsWith matches isEmpty before after | One of eq, ne, lt, lte, gt, gte, contains, startsWith, endsWith, matches, isEmpty, before, after. Required. |
caseInsensitive | yes / no | true 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. | ||
value | any value | What to compare with. Defaults to empty text. |
Examples #
- Set title to “Quarterly report”
- If {{length(title)}} is above 10
- Show “Longer than 10 characters”
- Else
- Show “10 or fewer”
- Set city to “Paris”
- If {{city}} starts with “pa”
- Show “Starts with pa”
Gotchas #
With
caseInsensitiveleft off the comparison is exact, soParisis notparis.lt,lte,gtandgtefail (rather than return false) when the sides can't be ordered, for example a list against a number.matchesis a regular expression, not a wildcard pattern.