User Tools

Site Tools


AppleScript

Differences

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

Link to this comparison view

Next revision
Previous revision
Last revision Both sides next revision
AppleScript [2015/12/16 13:10]
ccstone created
AppleScript [2018/07/03 21:29]
JMichaelTX [Using AppleScript to Get and Set Keyboard Maestro Variables]
Line 1: Line 1:
-====== AppleScript ​Tips and Tricks for Keyboard Maestro ====== +====== ​Using AppleScript ​to Get and Set Keyboard Maestro ​Variables ​======
-\\+
  
-==== Create ​Keyboard Maestro ​variable using AppleScript. ​====+Keyboard Maestro ​Version 7.1 introduced a streamlined way to Get and Set Keyboard Maestro Variables from AppleScript.
  
-Keyboard Maestro ​— Create variable ​"<variable name>"+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. 
 + 
 +**For more details, see:** 
 +  - [[manual:​Scripting|Scripting article in the Manual Section]]. 
 +  - [[action:​Execute_an_AppleScript|Execute AppleScript Action]]. 
 + 
 +===== Global Variables ===== 
 + 
 +Global Variables (available in //all// macros) may be got and set like this: 
 + 
 +```applescript 
 +### Requires Keyboard Maestro 7.1+ ### 
 + 
 +--- Set AppleScript Variables to KM Variable Name and Value --- 
 +set myKMVar to "My KM Var Name"​ 
 +set myASVar to "​TBD"​ -- default value of KM variable, will be updated
  
-<​code>​ 
 tell application "​Keyboard Maestro Engine"​ tell application "​Keyboard Maestro Engine"​
-  ​make new variable with properties {name:"MyVariableName", value:​MyVariableValue}+  ​ 
 +  ### GET ### 
 +  -- IF KM Variable does NOT exist, the AS Variable will be set to empty string -- 
 + 
 +  -- Use Explicit Quoted Text -- 
 +  set myASVar to getvariable ​"My KM Var Name" 
 +  -- ORUse Previously Defined AppleScript Variables -- 
 +  set myASVar to getvariable myKMVar 
 +   
 +   
 +   
 +  ### SET ### 
 +  -- IF KM Variable does NOT exist, it will be created -- 
 + 
 +  -- 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
-</​code>​+```
  
-Note that the name MUST be a string, although you can hold that string in a varible:+**Real-World Example**
  
-<​code>​ +Normally, you would not have both a GET and a SET variable within the same AppleScript tell block.\\ 
-set someVariable ​to "​MyVariableName"​ +Here's a real-world example, but with the error checking removed ​to simplify.
-make new variable with properties {name:​someVariable,​ value:​MyVariableValue} +
-</​code>​+
  
-==== Set Keyboard Maestro ​variable using AppleScript====+```applescript 
 +### Requires ​Keyboard Maestro ​7.1+ ###
  
-<​code>​ +--- GET REQUIRED KM VARIABLES ---
-# Keyboard Maestro — Set value of variable "<​variable name>"​ +
-# Requires KM7.0.2 or higher. +
-set myText to "​whatever"​+
  
 tell application "​Keyboard Maestro Engine"​ tell application "​Keyboard Maestro Engine"​
-  ​if variable ​"myKMVariableName" ​exists then +  ​set mainStr to getvariable ​"SCPT__MainString
-    set value of variable "​myKMVariableName" ​to myText +  set subStr ​to getvariable ​"SCPT__StringToFind"
-  else +
-    make new variable with properties {name:"myKMVariableName", value:​myText} +
-  end if+
 end tell end tell
-</​code>​ 
  
-==== Get Keyboard Maestro variable using AppleScript. ====+--- PROCESS THESE KM VARIABLES ---
  
-<​code>​ +--- Get Start of StringToFind --- 
-Keyboard Maestro ​— Get value of variable ​"<variable ​name>+set posSubStr to offset of subStr in mainStr 
-# Requires ​KM7.0.2 or higher.+ 
 +--- SET OUTPUT KM VARIABLE --- 
 + 
 +tell application "Keyboard Maestro ​Engine" ​to setvariable "​SCPT__PosSubStr"​ to posSubStr 
 +``` 
 + 
 +===== Local & Instance Variables ===== 
 + 
 + 
 +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+). 
 + 
 +  * //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 
 +### Requires ​Keyboard Maestro 8.0.3+ ### 
 + 
 +set kmInst to system attribute "​KMINSTANCE"​
 tell application "​Keyboard Maestro Engine"​ tell application "​Keyboard Maestro Engine"​
-  if variable ​"myKMVariableName" ​exists then + set kmLocalVar1 to getvariable ​"Local__SomeLocalVariable" ​instance kmInst 
-    set myAppleScriptVariableName ​to value of variable ​"myKMVariableName" + setvariable "​Local__FromAS"​ instance kmInst ​to "Variable set in AppleScript."
-  end if+
 end tell end tell
-</code>+ 
 +log kmLocalVar1 
 +``` 
 + 
 + 
 +===== Dictionary Values ===== 
 + 
 +You can read and write [[manual:​Dictionaries|Dictionary]] values from AppleScript. 
 + 
 +```applescript 
 +### Requires Keyboard Maestro 8.0+ ### 
 + 
 +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"​ 
 +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