This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
action:Execute_an_AppleScript [2023/09/12 04:24] peternlewis |
action:Execute_an_AppleScript [2025/02/27 01:45] (current) peternlewis [Using Keyboard Maestro Variables] |
||
---|---|---|---|
Line 49: | Line 49: | ||
===== Using Keyboard Maestro Variables ===== | ===== Using Keyboard Maestro Variables ===== | ||
- | :!: **See [[:AppleScript|Using AppleScript to Get and Set Keyboard Maestro Variables]] for best methods.** | + | Keyboard Maestro sets the environment variables for the script to include all (by default) your variables, using a prefix of `KMVAR_` and your variable name with spaces changed in to underscores (‗). For example, your Keyboard Maestro “File Name” variable will be available as the environment variable `KMVAR_File_Name`. By default, all variables are included, but you can limit which variables are included by using the popup menu next to the script (v11.0+). Note that these provide a readonly copy of the value of the variables when the script starts. |
- | In AppleScript with Keyboard Maestro Version 7.1+, you can tell the application "Keyboard Maestro Engine" to: | + | Also, you can (7.1+) access Keyboard Maestro variables directly like this: |
- | getvariable <KM Variable Name> | + | ```applescript |
- | setvariable <KM Variable Name> to <New Value> | + | tell application "Keyboard Maestro Engine" |
+ | set v to getvariable "<KM Variable Name>" | ||
+ | setvariable "<KM Variable Name>" to "<New Value>" | ||
+ | end tell | ||
+ | ``` | ||
where both the `<KM Variable Name>` and `<New Value>` are text values. | where both the `<KM Variable Name>` and `<New Value>` are text values. | ||
- | For example: | + | ===== Local & Instance Variables ===== |
- | <code applescript> | + | |
- | tell application "Keyboard Maestro Engine" to set myVar to getvariable "My KM Variable" | + | |
- | </code> | + | |
- | **Using Keyboard Maestro Variables in a Shell Script from AppleScript** | + | To get or set [[manual:Variables#Scope|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+). |
- | AppleScripts can access variables by using environment variables (using system attribute) or by talking to the Keyboard Maestro Engine. Note that AppleScript’s system attribute is not safe for international characters, although it can use code like: | + | ```applescript |
+ | set kmInst to system attribute "KMINSTANCE" | ||
+ | tell application "Keyboard Maestro Engine" | ||
+ | set v to getvariable "Local__SomeLocalVariable" instance kmInst | ||
+ | setvariable "Local__FromAS" instance kmInst to "Variable set in AppleScript." | ||
+ | end tell | ||
- | <code applescript> | + | log v |
- | set myVar to do shell script "echo $KMVAR_My_KM_Variable" | + | ``` |
- | </code> | + | |
===== Error Handling ===== | ===== Error Handling ===== |