User Tools

Site Tools


AppleScript

Differences

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

Link to this comparison view

Both sides previous revision Previous revision
AppleScript [2017/10/04 02:52]
peternlewis [Examples]
AppleScript [2017/10/13 20:46] (current)
JMichaelTX ADD section headers, and clarify Local/Instance variables
Line 1: Line 1:
 ====== Using AppleScript to Get and Set Keyboard Maestro Variables ====== ====== Using AppleScript to Get and Set Keyboard Maestro Variables ======
- 
-===== Get and Set Variables ===== 
  
 Keyboard Maestro Version 7.1 introduced a streamlined way to Get and Set Keyboard Maestro Variables from AppleScript. Keyboard Maestro Version 7.1 introduced a streamlined way to Get and Set Keyboard Maestro Variables from AppleScript.
Line 12: Line 10:
 where both the `<KM Variable Name>` and `<New Value>` are text values. where both the `<KM Variable Name>` and `<New Value>` are text values.
  
-==== Examples ​====+**For more details, see [[manual:​Scripting|Scripting article in the Manual Section]].** 
 + 
 +===== Global Variables ​===== 
 + 
 +Global Variables (available in //all// macros) may be got and set like this:
  
 ```applescript ```applescript
Line 44: Line 46:
 ``` ```
  
 +**Real-World Example**
  
 Normally, you would not have both a GET and a SET variable within the same AppleScript tell block.\\ Normally, you would not have both a GET and a SET variable within the same AppleScript tell block.\\
Line 50: Line 52:
  
 ```applescript ```applescript
 +### Requires Keyboard Maestro 7.1+ ###
 +
 --- GET REQUIRED KM VARIABLES --- --- GET REQUIRED KM VARIABLES ---
  
Line 67: Line 71:
 ``` ```
  
-To get or set local or instance variables, you must pass the macro execution instance to the `getvariable` or `setvariable` command. ​ The instance is passed in to your script via the KMINSTANCE environment variable (v8.0.3+).+===== Local & Instance Variables =====
  
-```applescript 
-set inst to system attribute "​KMINSTANCE"​ 
-tell application "​Keyboard Maestro Engine"​ 
- set v to getvariable "​LocalVar"​ instance inst 
- setvariable "​LocalOut"​ instance inst to "​FromAS"​ 
-end tell 
-v 
-``` 
  
-**For more details, see [[manual:Scripting|Scripting article in the Manual Section]].** +To get or set [[manual:Variables#​Instance_Variables_v8|Local or Instance Variables]], you must pass the macro execution instance to the `getvariable` or `setvariable` command. ​ The instance is passed in to your script via the KMINSTANCE environment variable (v8.0.3+).
-===== Get and Set Dictionary Values =====+
  
-You can read and write dictionary values from AppleScript.+  * //Local Variables// are denoted by a Variable with the prefix of "​Local"​. 
 +  * //Instance Variables// are denoted by a Variable with the prefix of "​Instance"​.
  
 ```applescript ```applescript
-tell application "Keyboard Maestro ​Engine"​ +### Requires ​Keyboard Maestro ​8.0.3+ ###
- name of dictionaries +
-end tell +
-```+
  
-```applescript+set kmInst to system attribute "​KMINSTANCE"​
 tell application "​Keyboard Maestro Engine"​ tell application "​Keyboard Maestro Engine"​
- dictionary keys of dictionary ​"First Names"+ set kmLocalVar1 to getvariable ​"Local__SomeLocalVariable"​ instance kmInst 
 + setvariable "​Local__FromAS"​ instance kmInst to "​Variable set in AppleScript."
 end tell end tell
 +
 +log kmLocalVar1
 ``` ```
 +
 +
 +===== Dictionary Values =====
 +
 +You can read and write [[manual:​Dictionaries|Dictionary]] values from AppleScript.
  
 ```applescript ```applescript
 +### Requires Keyboard Maestro 8.0+ ###
 +
 tell application "​Keyboard Maestro Engine"​ tell application "​Keyboard Maestro Engine"​
 + set kmDictList to name of dictionaries
 + set dictKeyList to dictionary keys of dictionary "First Names"
 +
  set value of dictionary key "​P"​ of dictionary "First Names" to "​Fred"​  set value of dictionary key "​P"​ of dictionary "First Names" to "​Fred"​
 end tell end tell
AppleScript.txt · Last modified: 2017/10/13 20:46 by JMichaelTX