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/01/06 15:24]
JMichaelTX ADD script example
AppleScript [2017/06/04 19:06]
JMichaelTX [Get Existing Variable]
Line 4: Line 4:
  
 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.
 +
 +In AppleScript,​ you can tell the application "​Keyboard Maestro Engine"​ to:
 +
 +    getvariable <KM Variable Name>
 +    setvariable <KM Variable Name> to <New Value>
 +
 +where both the `<KM Variable Name>` and `<New Value>` are text values.
 +
 +==== Examples ====
  
 ```applescript ```applescript
Line 9: Line 18:
  
 --- Set AppleScript Variables to KM Variable Name and Value --- --- Set AppleScript Variables to KM Variable Name and Value ---
-set kmVarName ​to "My KM Var Name"​ +set myKMVar ​to "My KM Var Name"​ 
-set kmVarValue ​to "​TBD"​ -- default value of KM variable, will be updated+set myASVar ​to "​TBD"​ -- default value of KM variable, will be updated
  
 tell application "​Keyboard Maestro Engine"​ tell application "​Keyboard Maestro Engine"​
Line 16: Line 25:
   ### GET ###   ### GET ###
   -- IF KM Variable does NOT exist, the AS Variable will be set to empty string --   -- IF KM Variable does NOT exist, the AS Variable will be set to empty string --
-  ​set kmVarValue ​to getvariable ​kmVarName+ 
 +  -- Use Explicit Quoted Text -- 
 +  ​set myASVar ​to getvariable ​"My KM Var Name"​ 
 +  -- OR, Use Previously Defined AppleScript Variables -- 
 +  set myASVar to getvariable myKMVar 
 +   
 +  ​
   ​   ​
   ### SET ###   ### SET ###
   -- IF KM Variable does NOT exist, it will be created --   -- IF KM Variable does NOT exist, it will be created --
-  ​setvariable ​kmVarName ​to kmVarValue+ 
 +  -- Use Explicit Quoted Text -- 
 +  ​setvariable ​"My KM Var Name" ​to "A new value"​ 
 +  -- OR, Use Previously Defined AppleScript Variables -- 
 +  setvariable myKMVar to myASVar
   ​   ​
 end tell end tell
 ``` ```
  
-==== 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 64: Line 83:
  
 --- Set AppleScript Variables to KM Variable Name and Value --- --- Set AppleScript Variables to KM Variable Name and Value ---
-set kmVarName ​to "My KM Var Name"​ +set myKMVar ​to "My KM Var Name"​ 
-set kmVarValue ​to "My data"+set myASVar ​to "My data"
  
  
 tell application "​Keyboard Maestro Engine"​ tell application "​Keyboard Maestro Engine"​
-  if variable ​kmVarName ​exists then+  if variable ​myKMVar ​exists then
     --- SET KM Variable ---     --- SET KM Variable ---
-    set value of variable ​kmVarName ​to kmVarValue+    set value of variable ​myKMVar ​to myASVar
   else   else
     --- Create & Set KM Variable ---     --- Create & Set KM Variable ---
-    make new variable with properties {name:kmVarName, value:kmVarValue}+    make new variable with properties {name:myKMVar, value:myASVar}
   end if   end if
 end tell end tell
Line 82: Line 101:
  
 ```applescript ```applescript
---- Requires KM 7.0.2+ --- 
  
---- Set AppleScript Variables to KM Variable Name and Value --- +set myKMVar ​to my getKMVar("MY_KM_Variable")
-set kmVarName to "My KM Var Name"​ +
-set kmVarValue ​to "TBD" ​-- default value of KM variable, will be updated+
  
-tell application "​Keyboard Maestro Engine"​ +on getKMVar(pKMVarNameStr) 
-  if variable kmVarName exists then + 
-    --- GET Existing KM Variable ​--- +  --- Compatible with Keyboard Maestro 6+ --- 
-    set kmVarValue ​to value of variable ​kmVarName +  --     • Returns ""​ if variable is not found (just like KM 7) 
-  else +   
-    --- KM Variable NOT Found --- +  tell application "​Keyboard Maestro Engine"​ 
-    set kmVarValue ​to ""​ +    ​try 
-    -- Optionally, throw an AppleScript ​error -- +      ​set kmVar to value of variable ​pKMVarNameStr 
-    ​error "​[ERROR] KM Variable '"​ & kmVarName & "'​ was NOT found."​ +       
-  end if +    ​on error errMsg number errNum 
-end tell+      if (errNum = -1728) then 
 +        ​--- KM Variable NOT Found --- 
 +        set kmVar to ""​ 
 +      ​else 
 +        ​error errMsg 
 +      end if 
 +      ​-- END on error 
 +      ​ 
 +    ​end try 
 +  end tell 
 +   
 +  return kmVar 
 +  ​ 
 +end getKMVar
 ``` ```
AppleScript.txt · Last modified: 2019/03/28 14:41 by JMichaelTX