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 [2021/07/21 17:58]
JMichaelTX ADD section on error handling
action:Execute_an_AppleScript [2025/02/27 01:45] (current)
peternlewis [Using Keyboard Maestro Variables]
Line 11: 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 //​Asynchronously//​ (the script runs while the macro immediately continues on to the next //​Action//​.) | | 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//​.) |
  
Line 23: Line 23:
  
 {{:​action:​execute-applescript-example-km8.png?​nolink|}} {{:​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 a line break (“¬”) character.
  
 If the script fails, the action will fail (v9.0+), potentially aborting the macro. If the script fails, the action will fail (v9.0+), potentially aborting the macro.
Line 47: Line 49:
 ===== Using Keyboard Maestro Variables ===== ===== Using Keyboard Maestro Variables =====
  
-:!: **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 (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.
  
-In AppleScript with Keyboard Maestro Version ​7.1+, you can tell the application "Keyboard Maestro ​Engine"​ to:+Also, you can (7.1+) access ​Keyboard Maestro ​variables directly like this:
  
-    ​getvariable <KM Variable Name> +```applescript 
-    setvariable <KM Variable Name> to <New Value>+tell application "​Keyboard Maestro Engine"​ 
 +    set v to getvariable ​"<KM Variable Name>" 
 +    setvariable ​"<KM Variable Name>" ​to "<New Value>
 +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 =====
-<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**+To get or set [[manual:Variables#​Scope|Local or Instance Variables]],​ you must pass the macro execution instance to the `getvariable` or `setvariable` command. ​ The instance is passed ​in to your script via the `KMINSTANCE` environment variable (v8.0.3+).
  
-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:+```applescript 
 +set kmInst to system attribute ​"​KMINSTANCE"​ 
 +tell application "Keyboard Maestro Engine
 + set v to getvariable "​Local__SomeLocalVariable"​ instance kmInst 
 + setvariable "​Local__FromAS"​ instance kmInst to "​Variable set in AppleScript." 
 +end tell
  
-<code applescript>​ +log v 
-set myVar to do shell script "echo $KMVAR_My_KM_Variable"​ +```
-</​code>​+
  
 ===== Error Handling ===== ===== Error Handling =====
Line 89: Line 96:
 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). 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.1626904734.txt.gz · Last modified: 2021/07/21 17:58 by JMichaelTX