AppleScript
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| AppleScript [2017/01/06 22:49] – [Get and Set Variables] Revised per @Tome JMichaelTX | AppleScript [2025/02/27 06:45] (current) – [Local & Instance Variables] peternlewis | ||
|---|---|---|---|
| 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. | ||
| + | |||
| + | In AppleScript, | ||
| + | |||
| + | ```applescript | ||
| + | tell application " | ||
| + | set v to getvariable "< | ||
| + | setvariable "< | ||
| + | end tell | ||
| + | ``` | ||
| + | |||
| + | where both the `<KM Variable Name>` and `<New Value>` are text values. | ||
| + | |||
| + | **For more details, see:** | ||
| + | - [[manual: | ||
| + | - [[action: | ||
| + | |||
| + | ===== Global Variables ===== | ||
| + | |||
| + | Global Variables (available in //all// macros) may be got and set like this: | ||
| ```applescript | ```applescript | ||
| Line 16: | Line 33: | ||
| ### 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 -- | ||
| + | |||
| + | -- Use Explicit Quoted Text -- | ||
| + | set myASVar to getvariable "My KM Var Name" | ||
| + | -- OR, Use Previously Defined AppleScript Variables -- | ||
| set myASVar to getvariable myKMVar | 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 -- | ||
| + | |||
| + | -- Use Explicit Quoted Text -- | ||
| + | setvariable "My KM Var Name" to "A new value" | ||
| + | -- OR, Use Previously Defined AppleScript Variables -- | ||
| setvariable myKMVar to myASVar | setvariable myKMVar to myASVar | ||
| | | ||
| Line 25: | Line 52: | ||
| ``` | ``` | ||
| - | ==== Example | + | **Real-World |
| 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 31: | Line 58: | ||
| ```applescript | ```applescript | ||
| + | ### Requires Keyboard Maestro 7.1+ ### | ||
| + | |||
| --- GET REQUIRED KM VARIABLES --- | --- GET REQUIRED KM VARIABLES --- | ||
| Line 48: | Line 77: | ||
| ``` | ``` | ||
| + | ===== Local & Instance Variables ===== | ||
| - | **For more details, see [[manual: | ||
| - | ---- | + | To get or set [[manual: |
| - | ===== Prior to Ver 7.1 ===== | + | ```applescript |
| + | set kmInst | ||
| + | tell application " | ||
| + | set v to getvariable " | ||
| + | setvariable " | ||
| + | end tell | ||
| - | * 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. | + | log v |
| - | * They present an alternate method. | + | ``` |
| - | ==== Create & Set Variable ==== | ||
| - | ```applescript | + | ===== Dictionary Values ===== |
| - | --- Requires KM 7.0.2+ --- | + | |
| - | --- Set AppleScript Variables to KM Variable Name and Value --- | + | You can read and write [[manual: |
| - | set kmVarName to "My KM Var Name" | + | |
| - | set kmVarValue to "My data" | + | |
| + | ```applescript | ||
| + | ### Requires Keyboard Maestro 8.0+ ### | ||
| tell application " | tell application " | ||
| - | if variable kmVarName exists then | + | set kmDictList to name of dictionaries |
| - | --- SET KM Variable --- | + | set dictKeyList |
| - | | + | |
| - | else | + | set value of dictionary key " |
| - | --- Create & Set KM Variable --- | + | |
| - | make new variable with properties {name: | + | |
| - | end if | + | |
| end tell | end tell | ||
| ``` | ``` | ||
| - | ==== Get Existing | + | ===== 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 | ```applescript | ||
| - | --- Requires KM 7.0.2+ --- | + | my setKMVar(" |
| - | --- Set AppleScript Variables to KM Variable | + | on setKMVar(pKMVarNameStr, |
| - | set kmVarName | + | |
| - | set kmVarValue | + | |
| + | -- • Creates the KM Variable | ||
| + | | ||
| + | |||
| + | | ||
| + | |||
| + | tell application | ||
| + | try | ||
| + | | ||
| + | |||
| + | on error errMsg number errNum | ||
| + | if (errNum = -10006) then --- KM Variable Does NOT Exist --- | ||
| + | |||
| + | --- Create & Set KM Variable --- | ||
| + | make new variable | ||
| + | set varCreatedBool to true | ||
| + | |||
| + | else | ||
| + | error (" | ||
| + | end if | ||
| + | -- END on error | ||
| + | |||
| + | end try | ||
| + | end tell | ||
| + | |||
| + | return varCreatedBool | ||
| + | |||
| + | end setKMVar | ||
| + | ``` | ||
| - | tell application | + | |
| - | | + | ==== Get Variable ==== |
| - | --- GET Existing KM Variable | + | (returns empty string |
| - | set kmVarValue | + | |
| - | else | + | ```applescript |
| - | --- KM Variable NOT Found --- | + | |
| - | set kmVarValue | + | set myKMVar to my getKMVar(" |
| - | -- Optionally, throw an AppleScript | + | |
| - | | + | on getKMVar(pKMVarNameStr) |
| - | end if | + | |
| - | end tell | + | |
| + | -- • Returns "" | ||
| + | |||
| + | tell application " | ||
| + | | ||
| + | | ||
| + | |||
| + | | ||
| + | if (errNum = -1728) then | ||
| + | | ||
| + | set kmVar to "" | ||
| + | | ||
| + | | ||
| + | end if | ||
| + | | ||
| + | | ||
| + | | ||
| + | end tell | ||
| + | |||
| + | return kmVar | ||
| + | | ||
| + | end getKMVar | ||
| ``` | ``` | ||
AppleScript.1483742947.txt.gz · Last modified: by JMichaelTX
