action:Execute_a_JavaScript_For_Automation
Differences
This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| action:Execute_a_JavaScript_For_Automation [2017/04/05 20:42] – Provide better JXA example script JMichaelTX | action:Execute_a_JavaScript_For_Automation [2025/08/18 02:17] (current) – [Local & Instance Variables] peternlewis | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | The //Execute a JavaScript For Automation action// executes | + | ====== Execute JavaScript for Automation Action ====== |
| + | |||
| + | The //Execute a JavaScript For Automation action// executes | ||
| + | |||
| + | ===== Script Results ===== | ||
| The results of the script can be: | The results of the script can be: | ||
| Line 12: | Line 16: | ||
| *Asynchronously ignored — the action runs while the macro continues on. | *Asynchronously ignored — the action runs while the macro continues on. | ||
| - | JavaScript For Automation scripts | + | By default, errors |
| - | You can access Keyboard Maestro variables by using environment variables or by using the Keyboard Maestro Engine application (7.1+). | + | ===== Modern Syntax ===== |
| - | ### Example JXA Script | + | In version 11.0, scripts default |
| - | ** Shows how to Get/Set Keyboard Maestro Variables, and Get/ | + | |
| ```javascript | ```javascript | ||
| - | 'use strict'; | + | (function () { |
| - | (function | + | … |
| + | })() | ||
| + | ``` | ||
| - | // --- 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 | + | |
| - | // --- SET KME APP VARIABLE NEEDED TO GET/SET KM VARIABLES --- | + | ```javascript |
| - | var kme = Application("Keyboard Maestro Engine"); | + | return " |
| + | ``` | ||
| + | |||
| + | You can turn Modern Syntax on or off in the popup menu next to the script. | ||
| + | |||
| + | {{: | ||
| + | ===== Using Keyboard Maestro Variables ===== | ||
| + | |||
| + | In Modern Syntax | ||
| + | |||
| + | ```javascript | ||
| + | var v = kmvar.My_KM_Data | ||
| + | ``` | ||
| + | |||
| + | Alternatively, | ||
| + | |||
| + | 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 | ||
| + | |||
| + | ```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 |
| - | // | + | var kme = Application("Keyboard Maestro Engine"); |
| - | | + | |
| - | console.log(" | + | |
| | | ||
| - | | + | |
| + | //--- GET A KM VARIABLE --- | ||
| + | // Returns empty string if it doesn' | ||
| + | var someVarNameStr = kme.getvariable(" | ||
| + | console.log(" | ||
| + | |||
| + | var someNewDataStr = "Text to be set to a KM var"; | ||
| | | ||
| - | | + | //--- SET A KM VARIABLE --- |
| - | // Creates the Variable if it doesn' | + | // Creates the Variable if it doesn' |
| - | // Verify Variable in the KM App Preferences | + | // Verify Variable in the KM App Preferences |
| - | kme.setvariable(" | + | kme.setvariable(" |
| | | ||
| - | | + | //--- GET TEXT ON CLIPBOARD --- |
| - | var clipboardStr = app.theClipboard() | + | var clipboardStr = app.theClipboard() |
| - | console.log(" | + | console.log(" |
| - | | + | var someDataStr = " |
| - | | + | //--- COPY TO CLIPBOARD --- |
| - | // Verify using KM Clipboard History Viewer | + | // Verify using KM Clipboard History Viewer |
| - | app.setTheClipboardTo(someDataStr) | + | app.setTheClipboardTo(someDataStr) |
| + | ``` | ||
| + | ==== Local & Instance Variables ==== | ||
| - | } // END of function run() | + | Keyboard Maestro has [[manual: |
| - | )(); | + | |
| + | ```javascript | ||
| + | var app = Application.currentApplication() | ||
| + | app.includeStandardAdditions = true | ||
| + | |||
| + | var kmInst = app.systemAttribute(" | ||
| + | var kmeApp = Application(" | ||
| + | |||
| + | var myLocalVar = kmeApp.getvariable(" | ||
| + | kmeApp.setvariable(" | ||
| ``` | ``` | ||
| - | --- | ||
| - | **For more information, | + | 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 |
| + | |||
| + | |||
| + | ===== Displaying User Dialogs ===== | ||
| + | |||
| + | JavaScript For Automation scripts are executed in the background via osascript. In order to have user interaction in the script, you will need to use a reference to the " | ||
| + | |||
| + | ```javascript | ||
| + | // --- SET CURRENT APP VARIABLE NEEDED FOR DIALOGS & StandardAdditions.osax --- | ||
| + | var app = Application.currentApplication() | ||
| + | app.includeStandardAdditions = true | ||
| + | |||
| + | var MsgStr | ||
| + | var TitleStr | ||
| + | var AnswerStr = " | ||
| + | |||
| + | app.beep() | ||
| + | var oAns = app.displayDialog(MsgStr, | ||
| + | { | ||
| + | withTitle: | ||
| + | , | ||
| + | , | ||
| + | , | ||
| + | , | ||
| + | }) | ||
| + | |||
| + | //--- Script is stopped here if user chooses " | ||
| + | |||
| + | var BtnStr = oAns.buttonReturned | ||
| + | |||
| + | return ("Btn: " + BtnStr) | ||
| + | ``` | ||
| + | |||
| + | ===== See Also ===== | ||
| * [[:: | * [[:: | ||
| * [Forum Topics tagged with " | * [Forum Topics tagged with " | ||
| * [Learning & Using AppleScript & JavaScript for Automation (JXA)](http:// | * [Learning & Using AppleScript & JavaScript for Automation (JXA)](http:// | ||
action/Execute_a_JavaScript_For_Automation.1491424927.txt.gz · Last modified: by JMichaelTX
