User Tools

Site Tools


action:Execute_an_AppleScript

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
action:Execute_an_AppleScript [2017/03/14 05:01]
peternlewis
action:Execute_an_AppleScript [2023/09/12 23:04] (current)
peternlewis [Using Keyboard Maestro Variables]
Line 1: Line 1:
-{{:​scripting-icon.png?​nolink|}} 
 ====== Execute AppleScript Action ====== ====== Execute AppleScript Action ======
  
-The Execute an AppleScript action executes a specified AppleScript,​ either from a file or text. For example, say "​hello"​.+The Execute an AppleScript action executes a specified AppleScript,​ either from a file or text.
  
-The results of an AppleScript can be:+===== Setup =====
  
-  *Ignored+When you insert an //Execute AppleScript//​ Action, it will appear as shown below.\\  
-  *Displayed in a floating window. + 
-  *Displayed briefly in a Notification. +{{:​action:​execute-applescript-km8.png?​nolink|}} 
-  *Typed in to the current ​selection. + 
-  *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 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`) | 
-  ​*Asynchronously ignored — the action ​runs while the macro continues on.+| 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: 
 +  ​**Script location**: //Execute text script// 
 +  - **Script Results**: //save results ​to variable//​ 
 +    * This causes the //Save to variable// field to appear at the bottom 
 +    * Since this variable is not needed after the macro finishes, ​//​Local// ​variable ​is used
 +    While the script can set Keyboard Maestro Variables, it is generally better ​to just return ​the result from the script, then allow the user to choose the Variable. 
 + 
 +{{:​action:​execute-applescript-example-km8.png?​nolink|}} 
 + 
 +When editing the script, you can press <​key>​Enter</​key>​ to compile and format the script, and you can press <​key>​Option-Return</​key>​ (v11.0+) to insert ​line break (“¬”) character
 + 
 +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:​Scripting|Scripting]]. ​ +For the latest update on this topic, see the User Manual article: [[manual:​Scripting|Scripting]]. ​
 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 ​an application ​like System Events ​to do the user interaction for you, for example:+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"​ +```applescript 
-    ​activate +tell application "​System Events"​ 
-    display dialog "Hello+  set currentAppName to name of first application process whose frontmost is true 
-  end tell+end tell 
 + 
 +tell application currentAppName 
 +  ​display dialog "Your message here" with title "Your Title here
 +end tell 
 + 
 +```
  
 ===== Using Keyboard Maestro Variables ===== ===== Using Keyboard Maestro Variables =====
  
-:!: **See [[manual:Scripting|Scripting]] for best methods.**+:!: **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 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 select No Variables, or specific variables as desired using the popup menu next to the script (v11.0+). 
 + 
 +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. 
 + 
 +For example: 
 +<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** **Using Keyboard Maestro Variables in a Shell Script from AppleScript**
Line 39: Line 74:
 set myVar to do shell script "echo $KMVAR_My_KM_Variable"​ set myVar to do shell script "echo $KMVAR_My_KM_Variable"​
 </​code>​ </​code>​
-Howeverif setting ​an AppleScript ​Variable ​to Keyboard Maestro ​Variable ​is all you needed ​to dothen you would use this (requires ​Ver 7.1+): + 
-<code applescript>​ +===== Error Handling ===== 
-tell application "​Keyboard Maestro Engine" ​to set myVar to getvariable "​My ​KM Variable" + 
-</​code>​+If the Applescript contains errorsa 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]](https://​forum.keyboardmaestro.com/​t/​display-last-km-macro-error/​5340). 
 + 
 +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] ](https://​forum.keyboardmaestro.com/​t/​km-script-error-handler-sub-macro/​4641). 
 + 
 +===== See Also ===== 
 + 
 +=== Actions === 
 + 
 +* [[action:​Execute_a_Shell_Script|Execute a Shell Script]] 
 +* [[action:​Execute an Automator Workflow|Execute an Automator Workflow]] 
 +* [[action:​Execute a JavaScript For Automation|Execute a JavaScript For Automation]] 
 +* [[action:​Filter|Filter ​Variable ​with Expand Tilde In Path]] 
 +* [[:​Actions|See all Actions]] 
 + 
 + 
action/Execute_an_AppleScript.1489482066.txt.gz · Last modified: 2017/03/14 05:01 by peternlewis