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
Next revision Both sides next revision
action:Execute_an_AppleScript [2017/06/04 22:45]
JMichaelTX [Using Keyboard Maestro Variables] minor clarification
action:Execute_an_AppleScript [2019/11/26 21:57]
peternlewis [Scripting]
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 is 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, a //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|}} 
 + 
 +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 applicationto ​do the user interaction for you, for example: 
 + 
 +```applescript 
 +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
  
-  tell application "​System Events"​ +```
-    activate +
-    display dialog "​Hello"​ +
-  end tell+
  
 ===== Using Keyboard Maestro Variables ===== ===== Using Keyboard Maestro Variables =====
Line 43: Line 60:
 tell application "​Keyboard Maestro Engine"​ to set myVar to getvariable "My KM Variable"​ tell application "​Keyboard Maestro Engine"​ to set myVar to getvariable "My KM Variable"​
 </​code>​ </​code>​
---- 
  
 **Using Keyboard Maestro Variables in a Shell Script from AppleScript** **Using Keyboard Maestro Variables in a Shell Script from AppleScript**
action/Execute_an_AppleScript.txt · Last modified: 2023/09/12 23:04 by peternlewis