Browse the docs

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:

  • set stores value in the variable.

  • append adds value to a list, or joins it onto the end of the text if the variable isn't a list.

  • increment adds value (default 1) to a number. A variable that doesn't exist yet counts as 0.

  • unset removes the variable.

  • setPath writes inside a record or list. Give path like config.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.

Count and remember
Do
  1. Set n to 1
  2. Add 4 to n
  3. Show “n is {{ n }}”
Open in Switchboard to try it

Parameters #

ParameterTypeRequiredValuesWhat it does
nametextYesThe variable. A shared. prefix makes it shared. For setPath the name alone may carry the path, as in config.items[0].title.
opone ofYesset append increment unset setPath
keytextsetPath only. One plain top-level key, with no path syntax, for names that contain dots.
pathtextsetPath only. Dots for keys, [0] for list positions, ["a b"] for keys with spaces.
valueany valueThe value to store, append or add. For unset it is ignored.

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

Examples #

Build a list and a record
Do
  1. Set names to an empty list
  2. Append “Ada” to names
  3. Append “Grace” to names
  4. Set profile.address.city to “Oslo”
  5. Clear names
Open in Switchboard to try it
Keep a counter between runs
Do
  1. Add 1 to shared.runs
  2. Show “Run number {{ shared.runs }}”
Open in Switchboard to try it

Gotchas #

  • increment on text that isn't a number fails. setPath into 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.

See also #

If, Repeat, Run macro, Variables and expressions