action:Execute_an_AppleScript
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| action:Execute_an_AppleScript [2018/07/03 07:23] – peternlewis | action:Execute_an_AppleScript [2025/02/27 06:45] (current) – [Using Keyboard Maestro Variables] peternlewis | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | {{: | ||
| ====== Execute AppleScript Action ====== | ====== Execute AppleScript Action ====== | ||
| Line 12: | Line 11: | ||
| **There are two setup options:** | **There are two setup options:** | ||
| ^ Setup Option ^ Choices\\ (Default shown first) ^ | ^ Setup Option ^ Choices\\ (Default shown first) ^ | ||
| - | | Script location | Execute text script (type or paste script into Action text field)\\ Execute script file (This is faster if it is a compiled script file `.scpt`) | | + | | Script location | Execute text script (type or paste script into Action text field)\\ Execute script file (This may be faster if it is a compiled script file `.scpt`) | |
| | Script Results | Ignored.\\ Displayed in a floating window.\\ Displayed briefly in a Notification.\\ Typed in the current text field that has focus.\\ Pasted in text field that has focus.\\ Saved to a Keyboard Maestro Variable.\\ Saved to the System or Named Clipboard.\\ Ignore Results and run // | | Script Results | Ignored.\\ Displayed in a floating window.\\ Displayed briefly in a Notification.\\ Typed in the current text field that has focus.\\ Pasted in text field that has focus.\\ Saved to a Keyboard Maestro Variable.\\ Saved to the System or Named Clipboard.\\ Ignore Results and run // | ||
| Line 25: | Line 24: | ||
| {{: | {{: | ||
| - | --- | + | When editing the script, you can press < |
| + | |||
| + | If the script fails, the action will fail (v9.0+), potentially aborting the macro. | ||
| ===== Scripting ===== | ===== Scripting ===== | ||
| - | For the latest update on this topic, see the User Manual article: [[manual: | + | For the latest update on this topic, see the User Manual article: [[manual: |
| This provides the latest method to get and set Keyboard Maestro variables via AppleScript. | This provides the latest method to get and set Keyboard Maestro variables via AppleScript. | ||
| - | AppleScripts are executed in the background via osascript. This means they are not allowed to do user interaction. You can work around this by asking the current | + | AppleScripts are executed in the background via osascript. This means they are not allowed to do user interaction. You can work around this by asking the current |
| ```applescript | ```applescript | ||
| Line 45: | Line 46: | ||
| ``` | ``` | ||
| - | |||
| - | --- | ||
| ===== Using Keyboard Maestro Variables ===== | ===== Using Keyboard Maestro Variables ===== | ||
| - | :!: **See [[: | + | Keyboard Maestro sets the environment variables for the script |
| - | In AppleScript with Keyboard Maestro Version | + | Also, you can (7.1+) access |
| - | | + | ```applescript |
| - | setvariable <KM Variable Name> to <New Value> | + | tell application " |
| + | set v to getvariable | ||
| + | setvariable | ||
| + | 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 ===== |
| - | < | + | |
| - | tell application " | + | To get or set [[manual:Variables# |
| - | </code> | + | |
| - | --- | + | ```applescript |
| + | set kmInst to system attribute " | ||
| + | tell application " | ||
| + | set v to getvariable "Local__SomeLocalVariable" | ||
| + | setvariable " | ||
| + | end tell | ||
| + | |||
| + | log v | ||
| + | ``` | ||
| + | |||
| + | ===== Error Handling ===== | ||
| + | |||
| + | If the Applescript contains errors, a system notification will be displayed when the action is run (unless you have disabled them in System Preferences). | ||
| + | |||
| + | If a Notifications is displayed, it can happen the error message gets cut off. | ||
| + | |||
| + | To see the entire message, view the Keyboard Maestro Engine Log, which can be found using the Keyboard Maestro.app Editor menu _Help > Open Logs Folder_, or just open this file: | ||
| + | `~/ | ||
| + | |||
| + | You should find a log entry like this:\\ | ||
| + | 2021-07-21 16:43:53 Execute an AppleScript failed with script error: | ||
| + | [path to file]/TEST Handle Script Error in KM.scpt: | ||
| + | execution error: [ERROR]: | ||
| + | |||
| + | Unfortunately the Keyboard Maestro Engine log file is very verbose, and it can be very hard to find the actual error. | ||
| + | [MACRO: | ||
| + | |||
| + | Also, if you are writing a lot of AppleScripts, | ||
| + | Here is an example of such an Action/ | ||
| + | |||
| + | ===== See Also ===== | ||
| + | |||
| + | === Actions === | ||
| - | **Using Keyboard Maestro Variables in a Shell Script | + | * [[action: |
| + | * [[action: | ||
| + | * [[action: | ||
| + | * [[action: | ||
| + | * [[: | ||
| - | 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: | ||
| - | <code applescript> | ||
| - | set myVar to do shell script "echo $KMVAR_My_KM_Variable" | ||
| - | </ | ||
action/Execute_an_AppleScript.1530602603.txt.gz · Last modified: by peternlewis
