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
Next revision
Previous revision
Next revision Both sides next revision
AppleScript [2017/06/04 19:04]
JMichaelTX [Get Existing Variable] Replace script with ver for KM 6+
AppleScript [2017/10/04 02:52]
peternlewis [Examples]
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 ===== ===== Get and Set Variables =====
  
Line 67: Line 67:
 ``` ```
  
 +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+).
 +
 +```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]].** **For more details, see [[manual:​Scripting|Scripting article in the Manual Section]].**
 +===== Get and Set Dictionary Values =====
  
-----+You can read and write dictionary values from AppleScript. 
 + 
 +```applescript 
 +tell application "​Keyboard Maestro Engine"​ 
 + name of dictionaries 
 +end tell 
 +``` 
 + 
 +```applescript 
 +tell application "​Keyboard Maestro Engine"​ 
 + dictionary keys of dictionary "First Names"​ 
 +end tell 
 +``` 
 + 
 +```applescript 
 +tell application "​Keyboard Maestro Engine"​ 
 + set value of dictionary key "​P"​ of dictionary "First Names" to "​Fred"​ 
 +end tell 
 +```
  
 ===== Prior to Ver 7.1 ===== ===== Prior to Ver 7.1 =====
Line 77: Line 106:
   * They present an alternate method.   * They present an alternate method.
  
-==== Create & Set Variable ====+==== Set Variable ==== 
 +(will be created if necessary)
  
 ```applescript ```applescript
---- Requires KM 7.0.2+ --- +my setKMVar("MY_KM_Variable""some new value")
- +
---- Set AppleScript Variables to KM Variable Name and Value --- +
-set myKMVar to "My KM Var Name" +
-set myASVar to "My data"+
  
- +on setKMVar(pKMVarNameStr,​ pValueStr) 
-tell application "​Keyboard Maestro Engine"​ +   
-  ​if ​variable ​myKMVar exists then +  --- Compatible with Keyboard Maestro 6+ --- 
-    --- SET KM Variable --- +  --     • Creates the KM Variable if does not exist (just like KM 7 setvariable) 
-    set value of variable myKMVar to myASVar +  --     • Returns true if new Variable was created 
-  else +   
-    --- Create & Set KM Variable --- +  set varCreatedBool to false 
-    make new variable with properties {name:myKMVar, value:myASVar+   
-  end if +  ​tell application "​Keyboard Maestro Engine"​ 
-end tell+    try 
 +      set value of variable ​pKMVarNameStr to pValueStr 
 +      ​ 
 +    ​on error errMsg number errNum 
 +      if (errNum = -10006) then --- KM Variable ​Does NOT Exist --- 
 +         
 +        --- Create & Set KM Variable --- 
 +        make new variable with properties {name:pKMVarNameStr, value:pValueStr
 +        set varCreatedBool to true 
 +         
 +      else 
 +        error ("​Error " & errNum & ": ​ " & errMsg) 
 +      ​end if 
 +      -- END on error 
 +       
 +    end try 
 +  ​end tell 
 +   
 +  return varCreatedBool 
 +   
 +end setKMVar
 ``` ```
  
-==== Get Existing ​Variable ====+ 
 +==== Get Variable ==== 
 +(returns empty string ""​ if Variable does //not// exist)
  
 ```applescript ```applescript
Line 105: Line 153:
  
 on getKMVar(pKMVarNameStr) on getKMVar(pKMVarNameStr)
 +
   --- Compatible with Keyboard Maestro 6+ ---   --- Compatible with Keyboard Maestro 6+ ---
 +  --     • Returns ""​ if variable is not found (just like KM 7 getvariable)
   ​   ​
   tell application "​Keyboard Maestro Engine"​   tell application "​Keyboard Maestro Engine"​
AppleScript.txt · Last modified: 2019/03/28 14:41 by JMichaelTX