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 revisionPrevious revision
Next revision
Previous revision
action:Execute_a_JavaScript_For_Automation [2023/09/13 02:49] – [Script Results] peternlewisaction:Execute_a_JavaScript_For_Automation [2025/08/18 02:17] (current) – [Local & Instance Variables] peternlewis
Line 36: Line 36:
 You can turn Modern Syntax on or off in the popup menu next to the script. You can turn Modern Syntax on or off in the popup menu next to the script.
  
-===== Get/Set Keyboard Maestro Variables =====+{{:action:modern-syntax.png?nolink|}} 
 +===== Using Keyboard Maestro Variables =====
  
-==== Global Variables ==== +In Modern Syntax (v11.0+)reference the variable like this:
- +
-  +
-** Example JXA Script shows how to Get/Set Keyboard Maestro //Global// Variablesand Get/Set the System Clipboard.**+
  
 ```javascript ```javascript
-'use strict'; +var v = kmvar.My_KM_Data 
-(function run() {      // this will auto-run when script is executed+```
  
-  // --- SET CURRENT APP VARIABLE NEEDED FOR DIALOGS & StandardAdditions.osax --- +Alternatively, the variables are stored in the environment variables with a prefix of `KMVAR_`.
-  var app = Application.currentApplication() +
-  app.includeStandardAdditions = true+
  
-  // --- SET KME APP VARIABLE NEEDED TO GET/SET KM VARIABLES --- +When accessing a variable, if its name has a space in it, replace it with an underscore. 
-  var kme Application("Keyboard Maestro Engine");+ 
 +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
  
-  //--- GET KM VARIABLE ---  +// --- SET KME APP VARIABLE NEEDED TO GET/SET KM VARIABLES --- 
-  //    Returns empty string if it doesn't exist +var kme Application("Keyboard Maestro Engine");
-  var someVarNameStr kme.getvariable("KMVarNameToGet"|| 'Default Value if NOT Found'; +
-  console.log("someVarNameStr: " + someVarNameStr)+
      
-  var someNewDataStr = "Text to be set to a KM var";+ 
 +//--- GET A KM VARIABLE ---  
 +//    Returns empty string if it doesn't exist 
 +var someVarNameStr kme.getvariable("KMVarNameToGet") || 'Default Value if NOT Found'; 
 +console.log("someVarNameStr: " + someVarNameStr)
      
-  //--- SET A KM VARIABLE --- +var someNewDataStr = "Text to be set to a KM var"; 
-  //      Creates the Variable if it doesn't exist +   
-  //      Verify Variable in the KM App Preferences +//--- SET A KM VARIABLE --- 
-  kme.setvariable("KMVarNameToSet", { to: someNewDataStr });+//      Creates the Variable if it doesn't exist 
 +//      Verify Variable in the KM App Preferences 
 +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" +
- +
-  //--- COPY TO CLIPBOARD --- +
-  //      Verify using KM Clipboard History Viewer +
-  app.setTheClipboardTo(someDataStr) +
  
-}  // END of function run() +var someDataStr = "Example text to put on clipboard"
-)();+
  
 +//--- COPY TO CLIPBOARD ---
 +//      Verify using KM Clipboard History Viewer
 +app.setTheClipboardTo(someDataStr)
 ``` ```
- 
-Local and Instance variables are available (as described below), but Password variables are not. 
  
 ==== Local & Instance Variables ==== ==== Local & Instance Variables ====
  
-**How To Get/Set Keyboard Maestro Local & Instance Variables** +Keyboard Maestro has [[manual:Variables#Instance_Variables_v8|Local and Instance Variables]].  In order to get or set these, you need to have the instance information of the macro and use different parameters in the JXA get/set methods.  
- +
-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.+
  
 ```javascript ```javascript
Line 102: Line 106:
 kmeApp.setvariable("Local__FromJXA", {instance: kmInst, to: "Set in JXA Script"}) kmeApp.setvariable("Local__FromJXA", {instance: kmInst, to: "Set in JXA Script"})
 ``` ```
 +
 +Note that this method is only useful if you are not using Modern Syntax (since you can access the local variables directly through the `kmvar` hash), and you need the instance information by some means (passed as an environment variable in the case of the `Execute a JavaScript For Automation` action, but in other cases external to the Keyboard Maestro Engine you would need some other method of getting it).
 +
  
 ===== Displaying User Dialogs ===== ===== Displaying User Dialogs =====
Line 108: Line 115:
  
 ```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 134: Line 138:
  
 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.1694573355.txt.gz · Last modified: by peternlewis

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki