JavaScript_for_Automation
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| JavaScript_for_Automation [2016/04/27 13:28] – [The Automation interface] peternlewis | JavaScript_for_Automation [2019/01/12 17:43] (current) – [Accessing Keyboard Maestro Variables] ADD Link to Get/Set Local/Instance Vars JMichaelTX | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | ====== | + | ====== JavaScript for Automation (JXA) Discussion |
| - | ===== Which Execute JavaScript Should You Use? ===== | + | ===== Scope and Summary |
| + | |||
| + | This Wiki article is a broad topic, which covers these major areas: | ||
| + | |||
| + | - Introduction to JXA | ||
| + | - Using JXA with Keyboard Maestro | ||
| + | - JXA Background | ||
| + | - Comparison with AppleScript | ||
| + | |||
| + | |||
| + | ===== Introduction ===== | ||
| + | |||
| + | Keyboard Maestro supports use of JavaScript in two basic ways: | ||
| + | |||
| + | - Execute JavaScript in a Browser | ||
| + | - Execute JavaScript for Automation (JXA) | ||
| + | |||
| + | This article deals only with the latter, JavaScript for Automation (JXA). | ||
| JavaScript, once a language which ran only in web browsers, can now be used in a variety of environments. From Keyboard Maestro you can: | JavaScript, once a language which ran only in web browsers, can now be used in a variety of environments. From Keyboard Maestro you can: | ||
| Line 9: | Line 26: | ||
| 3. Run it in Custom HTML Prompt windows. | 3. Run it in Custom HTML Prompt windows. | ||
| - | [[https:// | + | =====Using JXA with Keyboard Maestro===== |
| + | |||
| + | ==== JXA Scripts ==== | ||
| + | |||
| + | You can run a JXA script from Keyboard Maestro by either of these _Execute Script_ Actions: | ||
| + | |||
| + | * [[action: | ||
| + | * The JXA script may be accessed as text in the Action, or from a file. | ||
| + | * [[action: | ||
| + | * The JXA script can be embedded in a Shell Script | ||
| + | |||
| + | |||
| + | ==== Accessing Keyboard Maestro Variables ==== | ||
| + | |||
| + | You can access Keyboard Maestro variables (7.1+) from JXA scripts using the Keyboard Maestro Engine application. | ||
| + | |||
| + | <code javascript> | ||
| + | //--- GET A REFERENCE TO THE KM ENGINE --- | ||
| + | var kme = Application(" | ||
| + | |||
| + | //--- GET A KM VARIABLE --- | ||
| + | var myVar = kme.getvariable(' | ||
| + | |||
| + | //--- SET A KM VARIABLE --- | ||
| + | // - Using explicit text | ||
| + | kme.setvariable(' | ||
| + | |||
| + | // - Using a JavaScript Variable | ||
| + | var scriptResults = "Text Determined by the script" | ||
| + | kme.setvariable(' | ||
| + | </ | ||
| + | |||
| + | Also see **[[action: | ||
| + | |||
| + | JXA can get a list of all variables as described on the [forum](https:// | ||
| + | |||
| + | |||
| + | **For more info on using JXA with Keyboard Maestro, see:** | ||
| + | |||
| + | * [[manual: | ||
| + | |||
| + | ===== JXA Background ===== | ||
| + | |||
| + | [[https:// | ||
| 1. With access to the functions and constants of an Automation object, and | 1. With access to the functions and constants of an Automation object, and | ||
| - | 2. with additional access to a lot of low-level Apple system functions through the ObjC object. | + | 2. with additional access to a lot of low-level Apple system functions through the Objective-C |
| - | The first script to try in JXA consists of just one word: | + | You can view the built-in, default function provided by JXA by running this simple, |
| `this` | `this` | ||
| Line 28: | Line 88: | ||
| {{: | {{: | ||
| - | Keyboard Maestro’s [Execute a JavaScript in Safari](https:// | + | Keyboard Maestro’s [[action: |
| - | =====The basics – AppleScript | + | |
| + | |||
| + | =====Comparing JXA with AppleScript===== | ||
| ====Evaluating simple expressions==== | ====Evaluating simple expressions==== | ||
| Line 253: | Line 315: | ||
| // "Sun -> Mon -> Tue | // "Sun -> Mon -> Tue | ||
| - | |||
| - | |||
| - | |||
| - | |||
| Line 263: | Line 321: | ||
| // of JavaScript arrays | // of JavaScript arrays | ||
| </ | </ | ||
| + | |||
| ====Indexing by name – AS records and JS objects==== | ====Indexing by name – AS records and JS objects==== | ||
| to follow ... | to follow ... | ||
| + | |||
| ====Structuring scripts – branching, nesting, mapping, filtering and reducing==== | ====Structuring scripts – branching, nesting, mapping, filtering and reducing==== | ||
| Line 274: | Line 334: | ||
| to follow ... | to follow ... | ||
| - | =====Accessing Keyboard Maestro Variables===== | ||
| - | You can access Keyboard Maestro variables from JXA scripts using the Keyboard Maestro Engine application. | ||
| - | var kme = Application(" | ||
| - | kme.getvariable(' | ||
| - | // kme.setvariable(' | ||
| - | =====The Automation interface===== | + | ===== See Also ===== |
| - | ===Using the Standard Additions library==== | + | ---- |
| - | to follow ... | + | === Actions === |
| - | ===Interacting with scriptable applications=== | + | * [[action: |
| + | * [[action: | ||
| + | * [[action: | ||
| + | * [[action: | ||
| + | * [[: | ||
| - | to follow ... | + | ---- |
| - | =====Using the Safari JSContexts Debugger (El Capitan onwards)===== | + | ==== Forum ==== |
| - | to follow | + | - [[https:// |
| + | - [[https:// | ||
| - | =====The ObjC interface===== | + | - [[https:// |
| - | + | ||
| - | to follow | + | |
| - | + | ||
| - | ===Basic file functions=== | + | |
| - | + | ||
| - | to follow ... | + | |
| - | + | ||
| - | ===Clipboard functions=== | + | |
| - | + | ||
| - | to follow ... | + | |
| - | + | ||
| - | ===Reading the documentation | + | |
| - | + | ||
| - | to follow ... | + | |
JavaScript_for_Automation.1461763685.txt.gz · Last modified: by peternlewis
