Set variable
Sets, appends to, counts up, removes or edits inside a variable.
Set name to value
Keyboard Maestro: SetVariableToText, SetVariableToCalculation
Details #
Choose what to do with op:
setstores value in the variable.appendadds value to a list, or joins it onto the end of the text if the variable isn't a list.incrementadds value (default 1) to a number. A variable that doesn't exist yet counts as 0.unsetremoves the variable.setPathwrites inside a record or list. Give path likeconfig.items[2].title. Missing records and lists on the way are created, and text that holds JSON is parsed first.
Anything in value can use {{ }}. A value that is exactly one {{ expression }} keeps its type, so "{{ n + 1 }}" stores a number, not text.
Scope: a variable is local to the run unless its name starts with shared.. Shared variables are kept between runs and across macros, and are stored on this Mac only. Everything else disappears when the run ends. There is no other scope.
- Set n to 1
- Add 4 to n
- Show “n is {{ n }}”
Parameters #
| Parameter | Type | Required | Values | What it does |
|---|---|---|---|---|
name | text | Yes | The variable. A shared. prefix makes it shared. For setPath the name alone may carry the path, as in config.items[0].title. | |
op | one of | Yes | set append increment unset setPath | |
key | text | setPath only. One plain top-level key, with no path syntax, for names that contain dots. | ||
path | text | setPath only. Dots for keys, [0] for list positions, ["a b"] for keys with spaces. | ||
value | any value | The value to store, append or add. For unset it is ignored. |
Every step also accepts label, onFailure, timeout and dryRunSafe.
Examples #
- Set names to an empty list
- Append “Ada” to names
- Append “Grace” to names
- Set profile.address.city to “Oslo”
- Clear names
- Add 1 to shared.runs
- Show “Run number {{ shared.runs }}”
Gotchas #
incrementon text that isn't a number fails.setPathinto a number or text fails too.A dry run executes this step, but does not save
shared.variables.Values longer than 2 KB are cut in the Trace. The variable itself keeps the full value.