User Tools

Site Tools


JavaScript_for_Automation

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
JavaScript_for_Automation [2016/04/27 09:28]
peternlewis
JavaScript_for_Automation [2019/01/12 12:43] (current)
JMichaelTX [Accessing Keyboard Maestro Variables] ADD Link to Get/Set Local/Instance Vars
Line 1: Line 1:
-====== ​Using JavaScript for Automation (JXA) with Keyboard Maestro ​======+====== 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://​developer.apple.com/​library/​mac/​releasenotes/​InterapplicationCommunication/​RN-JavaScriptForAutomation/​Articles/​Introduction.html|JavaScript for Automation]] (JXA) is a more flexible alternative to AppleScript, ​with richer ​set of default libraries for some basics like regexes ​and URL handling. ​Its equivalent of AppleScript '​records'​ is more forgiving and much easier to use. JXA is the same fast JavaScript Core language that Safari uses, but it is running in an osascript context:+=====Using JXA with Keyboard Maestro===== 
 + 
 +==== JXA Scripts ==== 
 + 
 +You can run a JXA script from Keyboard Maestro by either of these _Execute Script_ Actions: 
 + 
 +  * [[action:​Execute_a_JavaScript_For_Automation]] 
 +    * The JXA script may be accessed as text in the Action, or from a file. 
 +  * [[action:​Execute_a_Shell_Script|Execute a Shell Script]] 
 +    * 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("​Keyboard Maestro Engine"​);​ 
 + 
 +//--- GET A KM VARIABLE --- 
 +var myVar = kme.getvariable('​My KM Var'​);​ 
 + 
 +//--- SET A KM VARIABLE --- 
 +//    - Using explicit text 
 +kme.setvariable('​My Other KM Var', { to: "Set by JXA" }); 
 + 
 +//    - Using a JavaScript Variable 
 +var scriptResults = "Text Determined by the script"​ 
 +kme.setvariable('​My Other KM Var', { to: scriptResults }); 
 +</​code>​ 
 + 
 +Also see **[[action:​Execute_a_JavaScript_For_Automation#​Local_Instance_Variables|How To Get/Set Keyboard Maestro Local & Instance Variables using JXA]]** 
 + 
 +JXA can get a list of all variables as described on the [forum](https://​forum.keyboardmaestro.com/​t/​using-km-variables-in-yosemite-javascript-for-applications-jxa/​889) and those methods also provide more convoluted ways to access variables for versions prior to 7.1. 
 + 
 + 
 +**For more info on using JXA with Keyboard Maestro, see:** 
 + 
 +  * [[manual:​Scripting|Scripting]] 
 + 
 +===== JXA Background ===== 
 + 
 +[[https://​developer.apple.com/​library/​mac/​releasenotes/​InterapplicationCommunication/​RN-JavaScriptForAutomation/​Articles/​Introduction.html|JavaScript for Automation]] (JXA) and AppleScript ​are the two primary languages Apple has provided for application automation. ​ While they both can generally achieve the same resultseach language offers advantages and disadvantages. ​ JXA is very flexible providing ​rich set of default libraries for some basics like character, date, and URL handling. ​ It also offers a very powerful Regular Expression engine built-in JXA is the same fast JavaScript Core language that Safari uses, but it is running in an osascript context:
  
 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 ​object.
  
-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, ​one-word script:
  
 `this` `this`
Line 28: Line 88:
 {{:​chromwinkeys.png?​400|}}{{:​undefined:​jssafariactiondocument.png?​400|}} {{:​chromwinkeys.png?​400|}}{{:​undefined:​jssafariactiondocument.png?​400|}}
  
-Keyboard Maestro’s [Execute a JavaScript in Safari](https://​wiki.keyboardmaestro.com/​action/​Execute_a_JavaScript_in_Safari) ​and [Execute a JavaScript in Google Chrome](https://​wiki.keyboardmaestro.com/​action/​Execute_a_JavaScript_in_Google_Chrome) ​actions are for interacting with web pages, the [Execute a JavaScript in Custom Prompt](https://​wiki.keyboardmaestro.com/​action/​Execute_a_JavaScript_in_Custom_Prompt) ​action is for interacting with Custom HTML Prompts, whereas JXA is for automating OS X and OS X Applications via the [Execute a JavaScript For Automation](https://​wiki.keyboardmaestro.com/​action/​Execute_a_JavaScript_For_Automation) ​action. +Keyboard Maestro’s [[action:​Execute a JavaScript in Safari|Execute a JavaScript in Safari]and [[action:​Execute a JavaScript in Google Chrome|Execute a JavaScript in Google Chrome]actions are for interacting with web pages, the [[action:​Execute a JavaScript in Custom Prompt|Execute a JavaScript in Custom Prompt]action is for interacting with Custom HTML Prompts, whereas JXA is for automating OS X and OS X Applications via the [[action:​Execute a JavaScript For Automation|Execute a JavaScript For Automation]action. 
-=====The basics – AppleScript ​and JavaScript for Automation side by side=====+ 
 + 
 +=====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
 </​code>​| ​ </​code>​| ​
 +
 ====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. 
  
-<code javascript>​ 
-var kme = Application("​Keyboard Maestro Engine"​);​ 
-kme.getvariable('​Var'​);​ 
-// kme.setvariable('​Var',​ { to: "Hello There World!"​ }); 
-</​code>​ 
  
-=====The Automation interface=====+===== See Also =====
  
-===Using the Standard Additions library====+----
  
-to follow ...+=== Actions ===
  
-===Interacting with scriptable applications===+* [[action:​Execute a JavaScript For Automation|Execute a JavaScript For Automation]] 
 +* [[action:​Execute a JavaScript in Custom Prompt|Execute a JavaScript in Custom Prompt]] 
 +* [[action:​Execute a JavaScript in Google Chrome|Execute a JavaScript in Google Chrome]] 
 +* [[action:​Execute a JavaScript in Safari|Execute a JavaScript in Safari]] 
 +* [[:​Actions|See all Actions]]
  
-to follow ...+----
  
-=====Using the Safari JSContexts Debugger (El Capitan onwards)=====+==== Forum ====
  
-to follow ​...+- [[https://​forum.keyboardmaestro.com/​t/​using-km-variables-in-yosemite-javascript-for-applications-jxa/​889|Using KM variables in Yosemite Javascript for Applications (JXA)]] 
 +- [[https://​forum.keyboardmaestro.com/​t/​how-to-change-safari-web-inspector-font/​3655|How to Change Safari Web Inspector Font]]
  
-=====The ObjC interface===== +- [[https://​forum.keyboardmaestro.com/​tags/​jxa|Keyboard Maestro Forum topics about JavaScript ​for Automation]]
- +
-to follow ​..+
- +
-===Basic file functions=== +
- +
-to follow ... +
- +
-===Clipboard functions=== +
- +
-to follow ... +
- +
-===Reading the documentation ​for simple ObjC functions=== +
- +
-to follow ...+
JavaScript_for_Automation.1461763724.txt.gz · Last modified: 2016/04/27 09:28 by peternlewis