This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision Last revision Both sides next revision | ||
action:Execute_a_JavaScript_For_Automation [2017/04/05 17:04] JMichaelTX Replaced example script, added script for dialogs |
action:Execute_a_JavaScript_For_Automation [2023/09/12 22:49] peternlewis [Script Results] |
||
---|---|---|---|
Line 16: | Line 16: | ||
*Asynchronously ignored — the action runs while the macro continues on. | *Asynchronously ignored — the action runs while the macro continues on. | ||
+ | By default, errors are not included in the output, and the output is trimmed of white space. These can be adjusted in the action (gear) ⚙ menu. | ||
+ | |||
+ | ===== Modern Syntax ===== | ||
+ | |||
+ | In version 11.0, scripts default to Modern Syntax, which essentially wraps the script in: | ||
+ | |||
+ | ```javascript | ||
+ | (function () { | ||
+ | … | ||
+ | })() | ||
+ | ``` | ||
+ | |||
+ | This helps keep your script from interacting with the web page in unexpected ways. A result of this is that you need to return values to the action using the `return` syntax. So a trivial action would be: | ||
+ | |||
+ | ```javascript | ||
+ | return "Hello"; | ||
+ | ``` | ||
+ | |||
+ | You can turn Modern Syntax on or off in the popup menu next to the script. | ||
===== Get/Set Keyboard Maestro Variables ===== | ===== Get/Set Keyboard Maestro Variables ===== | ||
+ | |||
+ | ==== Global Variables ==== | ||
- | ** Example JXA Script shows how to Get/Set Keyboard Maestro Variables, and Get/Set the System Clipboard.** | + | ** Example JXA Script shows how to Get/Set Keyboard Maestro //Global// Variables, and Get/Set the System Clipboard.** |
```javascript | ```javascript | ||
Line 62: | Line 83: | ||
``` | ``` | ||
- | --- | + | |
+ | Local and Instance variables are available (as described below), but Password variables are not. | ||
+ | |||
+ | ==== Local & Instance Variables ==== | ||
+ | |||
+ | **How To Get/Set Keyboard Maestro Local & Instance Variables** | ||
+ | |||
+ | Keyboard Maestro Ver 8 introduced [[manual:Variables#Instance_Variables_v8|Local and Instance Variables]]. In order to get or set these, you need to use different parameters in the JXA get/set methods. Here is an example. | ||
+ | |||
+ | ```javascript | ||
+ | var app = Application.currentApplication() | ||
+ | app.includeStandardAdditions = true | ||
+ | |||
+ | var kmInst = app.systemAttribute("KMINSTANCE"); | ||
+ | var kmeApp = Application("Keyboard Maestro Engine") | ||
+ | |||
+ | var myLocalVar = kmeApp.getvariable("Local__MyVar", {instance: kmInst}); | ||
+ | kmeApp.setvariable("Local__FromJXA", {instance: kmInst, to: "Set in JXA Script"}) | ||
+ | ``` | ||
===== Displaying User Dialogs ===== | ===== Displaying User Dialogs ===== |