The Execute an AppleScript action executes a specified AppleScript, either from a file or text.
When you insert an Execute AppleScript Action, it will appear as shown below.
There are two setup options:
|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
|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 Asynchronously (the script runs while the macro immediately continues on to the next Action.)
Here is a typical example with these options:
If the script fails, the action will fail (v9.0+), potentially aborting the macro.
For the latest update on this topic, see the User Manual article: Scripting. 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 application to do the user interaction for you, for example:
tell application "System Events" set currentAppName to name of first application process whose frontmost is true end tell tell application currentAppName display dialog "Your message here" with title "Your Title here" end tell
See Using AppleScript to Get and Set Keyboard Maestro Variables for best methods.
In AppleScript with Keyboard Maestro Version 7.1+, 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.
tell application "Keyboard Maestro Engine" to set myVar to getvariable "My KM Variable"
Using Keyboard Maestro Variables in a Shell Script from AppleScript
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:
set myVar to do shell script "echo $KMVAR_My_KM_Variable"
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:
~/Library/Logs/Keyboard Maestro/Engine.log and searching for “AppleScript”.
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]: Can’t divide 5.0 by zero.
Unfortunately the Keyboard Maestro Engine log file is very verbose, and it can be very hard to find the actual error. So, you may find this custom macro useful:
MACRO: Display Last KM Macro Error Ver 4 [Pub].
Also, if you are writing a lot of AppleScripts, then you will find it very beneficial to always include a custom error handler in your script, and then use a custom Action after the Execute AppleScript Action to check for an error and provide much better reporting.
Here is an example of such an Action/Macro to handle errors: [KM] Script Error Handler [Sub-Macro] .