User Tools

Site Tools


Variables

**This is an old revision of the document!**

Keyboard Maestro Variables

Keyboard Maestro variables are used much like variables in any scripting or macro language. They provide for the storage of data (only plain text, no styles) as determined by you, the Keyboard Maestro user.

Key Facts about Variables

Keyboard Maestro has four types of variables, distinguished based on their name.

  • Password Variables — names start or end with “Password” or “PW”.
  • Local Variables — names start or end with “Local ” or “𝕃”.
  • Instance Variables — names start or end with “Instance ” or “𝕀”.
  • Variables — All other variables.

As described in the user manual, variables contain text, but may be used as numbers in calculations if they contain a valid expression (eg “10*5”).

  • Each Macro may create Variables and read existing Variables.
  • Variables persist, permanently stored on your Mac (much like a file) until deleted.
  • Variables are global, fully accessible (create, read, write, delete) by:
    • All other Macros.
    • Future or simultaneous executions of some macro.
    • Scripts.
    • AppleScript.
    • Keyboard Maestro Editor Preferences panel (Variables tab). :!: Note: When deleted, there is no warning, and there is no Trash to recover deleted Variables from.
    • All Keyboard Maestro variables can contain only plain text.

Setting Variables

Variables are usually created and set by Macro Actions, but can also be set by scripts, and can then be used by other Macro Actions. The Action that sets a Variable will create it if need be. Variables do not need to be declared in any way.

The most common Action to set a variable is, well, Set Variable to Text.

There are a number of other Actions to set variables:

In addition to these, there are a number of more complex Actions that can also set a variable. The For Each Action is an example. Variables can also contain the results of many actions, such as the Execute a Shell Script action.

When setting a variable, you will be using a text field in most cases. :!: Be aware that Text fields (that are not regular expression fields), process backslashed characters \a,\b,\e,\f,\t,\r,\n (bell,backspace,escape,form feed,tab,return,line feed). For more information, see the Text fields Wiki article.

Using Variables

There are many Macro Actions that can use variables. Some of these explicitly provide for entry of the variable name, but most provide for a more general entry of a Keyboard Maestro Token.

Variable Tokens

The %Variable%<VariableName>% token allows you to include a variable in a token text field,
where <VariableName> is the name of the variable.

For Example, using the Insert Text Action:

You can (v7.2+) access a variable as if it is a comma separated array of text using the notation %Variable%<VariableName>[3]% where 3 can be any Calculations. You can use a different item separator by putting the text between the ] and % characters, eg %Variable%<VariableName>[3]:%. Arrays are indexed starting from 1.

You can also use a short form of just %Variable Name% to include variables as long as the variable exists and has a value and there is no corresponding text token, although generally it is better and clearer to use the longer form %Variable%Variable Name%.

You can also access variables in a token field numerically using the %Calculate% token. It is important to note that calculations can contain only numeric values. When you use the %Calculate% token to reference a variable or variable array element, it will convert the element to a number.

Variables in Calculations

Variables can be used in calculations if their value holds a number or a numeric expression that can be evaluated. Variables are used unadorned in calculations, for example My Variable * 7. If a variable contains a sequence of comma-separated numbers, it can be accessed as an array, for example My Variable[7], or using dot notation, for example My Variable.x. See the Calculations user manual section for more information on calculations and using variables in numeric fields.

Using Variables in Scripts

You can get and set Keyboard Maestro Variables in these types of scripts:

And you can generally access Keyboard Maestro variables anywhere else via AppleScript from whatever language you want to use.

Click on the above links for example scripts.

Password Variables

Variables with names that start or end with “Password” or “PW” are considered passwords – their values will not be stored (except in memory) and they cannot be read directly by shell scripts or AppleScripts, though their consequences can easily be extracted, eg with the Set Clipboard to Text action. The Prompt For User Input dialog will display such variables in a password field.

You should set their values immediately after you are done with the password to reduce any change of erroneous access.

Local Variables

Variables with names that start with “Local ” or a double-struck L (𝕃) are considered local (v8+) to a specific instance of a specific macro. Each time the macro is run, they will start empty and no other macro will see or be able to change values.

Instance Variables

Variables with names that start with “Instance ” or a double-struck I (𝕀) are considered local (v8+) to a specific execution sequence. Each time the macro is run, they will start empty but their value can be seen and changed by other macros within the same execution sequence (for example, if you execute a macro using the Execute Macro action, that macro can see and change the instance variable). Since two instances of a macro can be running at the same time, this is useful to ensure they each have their own version of the variable.

See Also


Actions

Forum

Variables.1504676412.txt.gz · Last modified: 2017/09/06 01:40 by peternlewis