This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Next revision Both sides next revision | ||
AppleScript [2017/01/13 20:37] JMichaelTX Last update for @Tom LOL |
AppleScript [2017/06/04 22:18] JMichaelTX [Set Variable] |
||
---|---|---|---|
Line 77: | Line 77: | ||
* 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 --- | + | on setKMVar(pKMVarNameStr, pValueStr) |
- | set myKMVar to "My KM Var Name" | + | |
- | set myASVar to "My data" | + | --- 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 | |
- | tell application "Keyboard Maestro Engine" | + | |
- | if variable myKMVar exists then | + | set varCreatedBool to false |
- | --- SET KM Variable --- | + | |
- | set value of variable myKMVar to myASVar | + | tell application "Keyboard Maestro Engine" |
- | else | + | try |
- | --- Create & Set KM Variable --- | + | set value of variable pKMVarNameStr to pValueStr |
- | make new variable with properties {name:myKMVar, value:myASVar} | + | |
- | end if | + | on error errMsg number errNum |
- | end tell | + | 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 | ||
- | --- Requires KM 7.0.2+ --- | ||
- | --- Set AppleScript Variables to KM Variable Name and Value --- | + | set myKMVar to my getKMVar("MY_KM_Variable") |
- | set myKMVar to "My KM Var Name" | + | |
- | set myASVar to "TBD" -- default value of KM variable, will be updated | + | on getKMVar(pKMVarNameStr) |
- | tell application "Keyboard Maestro Engine" | + | --- Compatible with Keyboard Maestro 6+ --- |
- | if variable myKMVar exists then | + | -- • Returns "" if variable is not found (just like KM 7 getvariable) |
- | --- GET Existing KM Variable --- | + | |
- | set myASVar to value of variable myKMVar | + | tell application "Keyboard Maestro Engine" |
- | else | + | try |
- | --- KM Variable NOT Found --- | + | set kmVar to value of variable pKMVarNameStr |
- | set myASVar to "" | + | |
- | -- Optionally, throw an AppleScript error -- | + | on error errMsg number errNum |
- | error "[ERROR] KM Variable '" & myKMVar & "' was NOT found." | + | if (errNum = -1728) then |
- | end if | + | --- KM Variable NOT Found --- |
- | end tell | + | set kmVar to "" |
+ | else | ||
+ | error errMsg | ||
+ | end if | ||
+ | -- END on error | ||
+ | | ||
+ | end try | ||
+ | end tell | ||
+ | |||
+ | return kmVar | ||
+ | | ||
+ | end getKMVar | ||
``` | ``` |