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/02/22 16:52] – [Indexing by number: AS lists ⇄ JS arrays] ComplexPoint | 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 |
| - | =====Executing JavaScript from KM – which action ?===== | + | ===== Scope and Summary ===== |
| + | |||
| + | This Wiki article is a broad topic, | ||
| + | |||
| + | - 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: | ||
| 1. Run it in Chrome or Safari web pages (' | 1. Run it in Chrome or Safari web pages (' | ||
| - | 2. (in OS X Yosemite onwards) run it in Apple' | + | 2. Run it in Apple' |
| + | 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 19: | Line 80: | ||
| the result will be a listing of all the pre-defined names in JXA's osascript environment. | the result will be a listing of all the pre-defined names in JXA's osascript environment. | ||
| - | We can either run this directly from from one of KM's ' | + | We can either run this directly from from one of Keyboard Maestro's ' |
| {{: | {{: | ||
| Line 27: | Line 88: | ||
| {{: | {{: | ||
| - | Keyboard Maestro's 'Execute a JavaScript in Safari' | + | Keyboard Maestro’s [[action:Execute a JavaScript in Safari|Execute a JavaScript in Safari]] |
| - | =====The basics – AppleScript | + | =====Comparing JXA with AppleScript===== |
| ====Evaluating simple expressions==== | ====Evaluating simple expressions==== | ||
| Line 35: | Line 97: | ||
| The simplest scripts evaluate an expression and return its value. | The simplest scripts evaluate an expression and return its value. | ||
| - | **NB** | + | **NB** |
| - If the result of an evaluation is not a string (or something simple like a number, that can automatically be converted to a string), the action may puzzle you by appearing to produce no result. | - If the result of an evaluation is not a string (or something simple like a number, that can automatically be converted to a string), the action may puzzle you by appearing to produce no result. | ||
| - It's a good idea to test your action code first in (El Capitan or Yosemite) Script Editor, which shows all results, whatever their type, including error messages. | - It's a good idea to test your action code first in (El Capitan or Yosemite) Script Editor, which shows all results, whatever their type, including error messages. | ||
| - | The following all return the same number to KM (in string format, and at a slightly higher level of precision – more decimal points – from JavaScript). | + | The following all return the same number to Keyboard Maestro |
| ^ AppleScript | ^ AppleScript | ||
| Line 61: | Line 123: | ||
| | <code applescript> | | <code applescript> | ||
| - | ====Assigning names to values | + | ====Assigning names to functions |
| If we often need to calculate the [[https:// | If we often need to calculate the [[https:// | ||
| - | ===Named ' | + | ===Named |
| ^ AppleScript | ^ AppleScript | ||
| 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 | + | |
| + | ====Structuring | ||
| to follow ... | to follow ... | ||
| Line 274: | Line 334: | ||
| to follow ... | to follow ... | ||
| - | =====The Automation interface===== | ||
| - | ===Using the Standard Additions library==== | ||
| - | to follow ... | ||
| - | ===Interacting with scriptable applications=== | + | ===== See Also ===== |
| - | to follow ... | + | ---- |
| - | =====Using the Safari JSContexts Debugger (El Capitan onwards)===== | + | === Actions |
| - | to follow ... | + | * [[action: |
| + | * [[action: | ||
| + | * [[action: | ||
| + | * [[action: | ||
| + | * [[: | ||
| - | =====The ObjC interface===== | + | ---- |
| - | to follow ... | + | ==== Forum ==== |
| - | ===Basic file functions=== | + | - [[https:// |
| + | - [[https:// | ||
| - | to follow | + | - [[https:// |
| - | + | ||
| - | ===Clipboard functions=== | + | |
| - | + | ||
| - | to follow ... | + | |
| - | + | ||
| - | ===Reading the documentation | + | |
| - | + | ||
| - | to follow ... | + | |
JavaScript_for_Automation.1456159979.txt.gz · Last modified: by ComplexPoint
