User Tools

Site Tools


action:Execute_a_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
action:Execute_a_JavaScript_For_Automation [2017/11/16 19:58]
JMichaelTX ADD example script for Local/Instance variables.
action:Execute_a_JavaScript_For_Automation [2023/09/12 22:56] (current)
peternlewis
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.
  
-===== Get/Set Keyboard Maestro Variables ​=====+===== Modern Syntax ​=====
  
-  +In version 11.0, scripts default ​to Modern Syntaxwhich essentially wraps the script in:
-** Example JXA Script shows how to Get/Set Keyboard Maestro Variablesand Get/​Set ​the System Clipboard.**+
  
 ```javascript ```javascript
-'use strict';​ +(function () { 
-(function ​run() {      // this will auto-run when script is executed+… 
 +})() 
 +```
  
-  ​// --- SET CURRENT APP VARIABLE NEEDED FOR DIALOGS & StandardAdditions.osax --- +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: 
-  var app = Application.currentApplication() + 
-  app.includeStandardAdditions = true+```javascript 
 +return "​Hello";​ 
 +``` 
 + 
 +You can turn Modern Syntax on or off in the popup menu next to the script. 
 + 
 +===== Using Keyboard Maestro Variables ===== 
 + 
 +In Modern Syntax (v11.0+), reference the variable like this: 
 + 
 +```javascript 
 +var v = kmvar.My_KM_Data 
 +``` 
 + 
 +Alternatively,​ the variables are stored in the environment variables with a prefix of `KMVAR_`. 
 + 
 +When accessing a variable, if its name has a space in it, replace it with an underscore. 
 + 
 +Local and Instance variables are available, but Password variables are not. 
 + 
 +By default, all variables are included, but you can select No Variables, or specific variables as desired using the popup menu next to the script. 
 + 
 +==== Using JXA to Access Variables ==== 
 + 
 +You can use JXA’s ability to communicate with the Keyboard Maestro Engine to read and write variables. 
 + 
 +```javascript 
 +// Assumes Modern Syntax 
 +   
 +// --- SET CURRENT APP VARIABLE NEEDED FOR DIALOGS & StandardAdditions.osax --- 
 +var app = Application.currentApplication() 
 +app.includeStandardAdditions = true
  
-  ​// --- SET KME APP VARIABLE NEEDED TO GET/SET KM VARIABLES --- +// --- SET KME APP VARIABLE NEEDED TO GET/SET KM VARIABLES --- 
-  var kme = Application("​Keyboard Maestro Engine"​);​+var kme = Application("​Keyboard Maestro Engine"​);​
   ​   ​
  
-  ​//--- GET A KM VARIABLE ---  +//--- GET A KM VARIABLE ---  
-  //    Returns empty string if it doesn'​t exist +//    Returns empty string if it doesn'​t exist 
-  var someVarNameStr = kme.getvariable("​KMVarNameToGet"​) || '​Default Value if NOT Found';​ +var someVarNameStr = kme.getvariable("​KMVarNameToGet"​) || '​Default Value if NOT Found';​ 
-  console.log("​someVarNameStr:​ " + someVarNameStr)+console.log("​someVarNameStr:​ " + someVarNameStr)
   ​   ​
-  ​var someNewDataStr = "Text to be set to a KM var";+var someNewDataStr = "Text to be set to a KM var";
   ​   ​
-  ​//--- SET A KM VARIABLE --- +//--- SET A KM VARIABLE --- 
-  //      Creates the Variable if it doesn'​t exist +//      Creates the Variable if it doesn'​t exist 
-  //      Verify Variable in the KM App Preferences +//      Verify Variable in the KM App Preferences 
-  kme.setvariable("​KMVarNameToSet",​ { to: someNewDataStr });+kme.setvariable("​KMVarNameToSet",​ { to: someNewDataStr });
  
   ​   ​
-  ​//--- GET TEXT ON CLIPBOARD --- +//--- GET TEXT ON CLIPBOARD --- 
-  var clipboardStr = app.theClipboard() +var clipboardStr = app.theClipboard() 
-  console.log("​clipboardStr:​ " + clipboardStr)+console.log("​clipboardStr:​ " + clipboardStr)
  
-  ​var someDataStr = "​Example text to put on clipboard"​ +var someDataStr = "​Example text to put on clipboard"​
- +
-  //--- COPY TO CLIPBOARD --- +
-  //      Verify using KM Clipboard History Viewer +
-  app.setTheClipboardTo(someDataStr) +
- +
- +
-}  // END of function run() +
-)();+
  
 +//--- COPY TO CLIPBOARD ---
 +//      Verify using KM Clipboard History Viewer
 +app.setTheClipboardTo(someDataStr)
 ``` ```
---- 
  
-==== Get/​Set ​Local & Instance Variables ====+==== 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.+Keyboard Maestro ​has [[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 ```javascript
Line 78: Line 105:
 kmeApp.setvariable("​Local__FromJXA",​ {instance: kmInst, to: "Set in JXA Script"​}) kmeApp.setvariable("​Local__FromJXA",​ {instance: kmInst, to: "Set in JXA Script"​})
 ``` ```
- 
---- 
  
 ===== Displaying User Dialogs ===== ===== Displaying User Dialogs =====
Line 86: Line 111:
  
 ```javascript ```javascript
-'use strict';​ 
-(function run() {      // this will auto-run when script is executed 
- 
 // --- SET CURRENT APP VARIABLE NEEDED FOR DIALOGS & StandardAdditions.osax --- // --- SET CURRENT APP VARIABLE NEEDED FOR DIALOGS & StandardAdditions.osax ---
 var app = Application.currentApplication() var app = Application.currentApplication()
Line 112: Line 134:
  
 return ("Btn: " + BtnStr) return ("Btn: " + BtnStr)
- 
-}  // END of function run() 
-)(); 
 ``` ```
  
 +===== See Also =====
  
-**For more information,​ see:** 
   * [[::​JavaScript for Automation]] Wiki Discussion   * [[::​JavaScript for Automation]] Wiki Discussion
   * [Forum Topics tagged with "​JXA"​](https://​forum.keyboardmaestro.com/​tags/​jxa)   * [Forum Topics tagged with "​JXA"​](https://​forum.keyboardmaestro.com/​tags/​jxa)
   * [Learning & Using AppleScript & JavaScript for Automation (JXA)](http://​forum.keyboardmaestro.com/​t/​learning-using-applescript-javascript-for-automation-jxa/​1545)   * [Learning & Using AppleScript & JavaScript for Automation (JXA)](http://​forum.keyboardmaestro.com/​t/​learning-using-applescript-javascript-for-automation-jxa/​1545)
action/Execute_a_JavaScript_For_Automation.1510880325.txt.gz · Last modified: 2017/11/16 19:58 by JMichaelTX