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 [2016/07/07 18:39] – ADD Links to User Manual Scripting Section JMichaelTX | 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 ====== | ||
| - | The Execute an AppleScript action executes a specified AppleScript, | + | The Execute an AppleScript action executes a specified AppleScript, |
| - | The results of an AppleScript can be: | + | ===== Setup ===== |
| - | *Ignored. | + | When you insert an //Execute AppleScript// |
| - | *Displayed in a floating window. | + | |
| - | *Displayed briefly in a Notification. | + | {{: |
| - | *Typed in to the current | + | |
| - | *Pasted in to the current selection. | + | **There are two setup options: |
| - | *Saved to a variable. | + | ^ Setup Option ^ Choices\\ (Default shown first) ^ |
| - | *Saved to the system or a Named Clipboard. | + | | 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.\\ |
| + | |||
| + | |||
| + | Here is a typical example with these options: | ||
| + | | ||
| + | - **Script Results**: //save results | ||
| + | * This causes the //Save to variable// field to appear at the bottom | ||
| + | * Since this variable is not needed after the macro finishes, | ||
| + | * While the script can set Keyboard Maestro Variables, it is generally better | ||
| + | |||
| + | {{: | ||
| + | |||
| + | When editing the script, you can press < | ||
| + | |||
| + | If the script fails, | ||
| ===== Scripting ===== | ===== Scripting ===== | ||
| - | For the latest update on this topic, see the User Manual article: | + | 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 | + | 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 |
| - | | + | ```applescript |
| - | | + | tell application " |
| - | display dialog "Hello" | + | set currentAppName to name of first application process whose frontmost is true |
| - | end tell | + | end tell |
| + | |||
| + | tell application currentAppName | ||
| + | | ||
| + | end tell | ||
| + | |||
| + | ``` | ||
| ===== Using Keyboard Maestro Variables ===== | ===== Using Keyboard Maestro Variables ===== | ||
| - | :!: **See [[manual:Scripting|Scripting]] 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. |
| + | |||
| + | Also, you can (7.1+) access Keyboard Maestro variables directly like this: | ||
| + | |||
| + | ```applescript | ||
| + | tell application " | ||
| + | set v to getvariable "< | ||
| + | setvariable "< | ||
| + | end tell | ||
| + | ``` | ||
| + | |||
| + | where both the `<KM Variable Name>` and `<New Value>` are text values. | ||
| + | |||
| + | ===== Local & Instance Variables ===== | ||
| + | |||
| + | To get or set [[manual:Variables# | ||
| + | |||
| + | ```applescript | ||
| + | set kmInst to system attribute " | ||
| + | tell application " | ||
| + | set v to getvariable " | ||
| + | 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 === | ||
| + | |||
| + | * [[action: | ||
| + | * [[action: | ||
| + | * [[action: | ||
| + | * [[action: | ||
| + | * [[: | ||
| - | **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: | ||
| - | <code applescript> | ||
| - | set myVar to do shell script "echo $KMVAR_My_KM_Variable" | ||
| - | </ | ||
| - | However, if setting an AppleScript Variable to a Keyboard Maestro Variable is all you needed to do, then you would use this (requires Ver 7.1+): | ||
| - | <code applescript> | ||
| - | tell application " | ||
| - | </ | ||
action/Execute_an_AppleScript.1467916789.txt.gz · Last modified: by JMichaelTX
