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
JavaScript_for_Automation [2017/03/05 20:54]
peternlewis [Forum]
JavaScript_for_Automation [2017/04/04 20:38] (current)
JMichaelTX Revised title and outline to better reflect contents; Moved the KM stuff to top
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>​ 
 + 
 +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 30: Line 87:
 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. 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 273: Line 331:
 to follow ... to follow ...
  
-=====Accessing Keyboard Maestro Variables===== 
  
-You can access Keyboard Maestro variables (7.1+) 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 World!"​ }); 
-</​code>​ 
- 
-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. 
- 
-=====The Automation interface===== 
- 
-===Using the Standard Additions library==== 
- 
-to follow ... 
- 
-===Interacting with scriptable applications=== 
- 
-to follow ... 
- 
-=====Using the Safari JSContexts Debugger (El Capitan onwards)===== 
- 
-to follow ... 
- 
-=====The ObjC interface===== 
- 
-to follow ... 
- 
-===Basic file functions=== 
- 
-to follow ... 
- 
-===Clipboard functions=== 
- 
-to follow ... 
- 
-===Reading the documentation for simple ObjC functions=== 
- 
-to follow ... 
  
 ===== See Also ===== ===== See Also =====
JavaScript_for_Automation.txt · Last modified: 2017/04/04 20:38 by JMichaelTX