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:12]
JMichaelTX ADD section at top with ver 7.1 AppleScript code
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 =====
  
 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
 ``` ```
  
-**For more details, see [[manual:​Scripting|Scripting article in the Manual Section]].** 
  
-===== Prior to Ver 7.1 ===== 
- 
-  * All of the below scripts were designed for use with Keyboard Maestro prior to version 7.1, but they will still work with the latest version. 
-  * They present an alternate method. 
  
-==== Create & Set Variable ====+Normally, you would not have both a GET and a SET variable within the same AppleScript tell block.\\ 
 +Here's a real-world example, but with the error checking removed to simplify.
  
 ```applescript ```applescript
---- Requires ​KM 7.0.2+ ​---+--- GET REQUIRED ​KM VARIABLES ​---
  
---- Set AppleScript Variables to KM Variable Name and Value --- +tell application "​Keyboard Maestro Engine"​ 
-set kmVarName ​to "My KM Var Name+  set mainStr ​to getvariable ​"SCPT__MainString
-set kmVarValue ​to "My data"+  set subStr ​to getvariable ​"SCPT__StringToFind" 
 +end tell
  
 +--- PROCESS THESE KM VARIABLES ---
  
 +--- Get Start of StringToFind ---
 +set posSubStr to offset of subStr in mainStr
 +
 +--- SET OUTPUT KM VARIABLE ---
 +
 +tell application "​Keyboard Maestro Engine"​ to setvariable "​SCPT__PosSubStr"​ to posSubStr
 +```
 +
 +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"​ tell application "​Keyboard Maestro Engine"​
-  if variable kmVarName exists then + set to getvariable "​LocalVar"​ instance inst 
-    --- SET KM Variable --- + setvariable "​LocalOut"​ instance inst to "​FromAS"​
-    ​set value of variable kmVarName ​to kmVarValue +
-  ​else +
-    --- Create & Set KM Variable --- +
-    make new variable with properties {name:​kmVarName,​ value:​kmVarValue} +
-  end if+
 end tell end tell
 +v
 ``` ```
  
-==== Get Existing Variable ​====+**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 ```applescript
---- Requires KM 7.0.2+ ---+tell application "​Keyboard Maestro Engine"​ 
 + name of dictionaries 
 +end tell 
 +```
  
---- Set AppleScript Variables to KM Variable Name and Value --- +```applescript 
-set kmVarName to "My KM Var Name+tell application ​"Keyboard Maestro Engine
-set kmVarValue to "TBD" ​-- default value of KM variable, will be updated+ dictionary keys of dictionary ​"First Names" 
 +end tell 
 +```
  
 +```applescript
 tell application "​Keyboard Maestro Engine"​ tell application "​Keyboard Maestro Engine"​
-  if variable kmVarName exists then + set value of dictionary key "P" ​of dictionary ​"First Names" ​to "Fred"
-    --- GET Existing KM Variable --- +
-    ​set kmVarValue to value of variable kmVarName +
-  else +
-    --- KM Variable NOT Found --- +
-    set kmVarValue to ""​ +
-    -- Optionally, throw an AppleScript error -- +
-    error "[ERROR] KM Variable '" ​& kmVarName & "' was NOT found." +
-  end if+
 end tell end tell
 +```
 +
 +===== Prior to Ver 7.1 =====
 +
 +  * All of the below scripts were designed for use with Keyboard Maestro prior to version 7.1, but they will still work with the latest version.
 +  * They present an alternate method.
 +
 +==== Set Variable ====
 +(will be created if necessary)
 +
 +```applescript
 +my setKMVar("​MY_KM_Variable",​ "some new value"​)
 +
 +on setKMVar(pKMVarNameStr,​ pValueStr)
 +  ​
 +  --- Compatible with Keyboard Maestro 6+ ---
 +  --     • Creates the KM Variable if does not exist (just like KM 7 setvariable)
 +  --     • Returns true if new Variable was created
 +  ​
 +  set varCreatedBool to false
 +  ​
 +  tell application "​Keyboard Maestro Engine"​
 +    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 Variable ====
 +(returns empty string ""​ if Variable does //not// exist)
 +
 +```applescript
 +
 +set myKMVar to my getKMVar("​MY_KM_Variable"​)
 +
 +on getKMVar(pKMVarNameStr)
 +
 +  --- Compatible with Keyboard Maestro 6+ ---
 +  --     • Returns ""​ if variable is not found (just like KM 7 getvariable)
 +  ​
 +  tell application "​Keyboard Maestro Engine"​
 +    try
 +      set kmVar to value of variable pKMVarNameStr
 +      ​
 +    on error errMsg number errNum
 +      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