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.
Keyboard Maestro Variables have some aspects that are quite different from traditional programming languages:
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.
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.
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.
<VariableName>% token allows you to include a variable in a token text field,
<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>% where 3 can be any Calculations. You can use a different item separator by putting the text between the ] and % characters, eg %Variable%
<VariableName>:%. 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%.
Variables can contain an array of comma separated numbers, like the image size (123,456) or window frame (100,120,600,550). In a calculation field, you can refer to these using a normal (1-based) index notation, like Variable. So you can use ClipboardImageSize and ClipboardImageSize.
It is important to note that variable arrays can contain only numeric values. When you use the %Calculate% function to reference a variable array element, it will convert the element to a number. So, in effect, you can only use variable arrays to store/reference numbers.
For example, suppose the variable
MyArray has this value:
10,05,00,12. In any Keyboard Maestro Action where you would like to use the array element
%Calculate%MyArray%, it will return “5” because it has evaluated it as number, not the actual string of the element “05”.
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.