User Tools

Site Tools


token:Variable

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
token:Variable [2016/02/11 02:21]
JMichaelTX ADD screenshots, links to scripts
token:Variable [2023/09/12 23:50] (current)
peternlewis
Line 1: Line 1:
-{{:​token:​variable-icon.png |}} +===== Variable Token =====
-====== Keyboard Maestro Variables ======+
  
-Keyboard Maestro variables are used much like variables in any scripting or macro language. ​ They provide for the storage of data as determined by you, the Keyboard Maestro user.  Instead of software code, Keyboard Maestro variables are set by [[:​Actions|Macro Actions]] (or by [[#​Using_Variables_in_Scripts|scripts]]),​ and can then be used by other Macro Actions. +The //​%Variable%//​`<​VariableName>​`% token allows you to include a [[manual:​Variables|variable]] in a [[:Text Fields |token text field]], where `<​VariableName>​` is the name of the variable.
- +
-It should be noted that **all Keyboard Maestro variables are text**. ​ Variables really don't have a type.  However, you can use variables that contain numbers in calculations,​ by using the [[token:​Calculate|Calculate Token]]. +
- +
-===== Setting Variables ===== +
- +
-The most common Action to set a variable is, well, [[action:​Set_Variable_to_Text|Set Variable to Text]]. ​  +
- +
-{{ :​token:​km-wiki-set-variable-action.png?​nolink |}} +
- +
-There are a number of other [[:​Actions|Actions]] to set variables:​ +
-  * [[action:​Set_Variable_to_Calculation]] +
-  * [[action:​Set_Variable_to_Google_Chrome_Field]] +
-  * [[action:​Set_Variable_to_Keychain_Password]] +
-  * [[action:​Set_Variable_to_Safari_Field]] +
- +
-In addition to these, there are a number of more complex Actions that can also set a variable. ​ The [[action:​For_Each|For Each]] Action is an example. +
- +
-===== 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 [[action:​Insert_Text|Insert Text]] Action: For Example, using the [[action:​Insert_Text|Insert Text]] Action:
  
 {{ :​token:​km-wiki-insert-text-action.png?​nolink |}} {{ :​token:​km-wiki-insert-text-action.png?​nolink |}}
 +
 +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 [[manual:​Calculations|Calculation]]. ​ You can use a different item separator by putting the text between the ] and % characters, eg //​%Variable%//​`<​VariableName>​[3]:​`%. In v11+, the separator is processed for backslashes,​ so you can use //​%Variable%//​`<​VariableName>​[3]\n`% to get the 3rd line.  Arrays are indexed starting from 1, and index 0 will give the count of the number of items, negative indices will index from the end of the array.
  
 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 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%//.
  
 +===== See Also =====
  
- +- [[:Text Fields]] 
-==== Variable Arrays ==== +[[:Tokens]] 
- +[[manual:Variables]]
-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[2]. So you can use ClipboardImageSize[1] and ClipboardImageSize[2]. +
- +
-**It is important to note that variable arrays can contain only numeric values**. ​ When you use the [[token:Calculate|%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[2]`,​ it will return "​5"​ because it has evaluated it as number, not the actual string of the element "​05"​. +
- +
-===== Using Variables in Scripts ===== +
- +
-You can get and set Keyboard Maestro Variables in these types of scripts: +
-  * [[:AppleScript|AppleScript]] +
-  ​* ​[[:JavaScript for Automation]] (JXA) +
-  * [[:​Shell_Scripting|Shell Scripts]]+
token/Variable.1455175283.txt.gz · Last modified: 2016/02/11 02:21 by JMichaelTX