Modern JavaScript Syntax and Variable Inclusion in Scripts

With the release of Keyboard Maestro 11, there are two new facilities related to scripts that are important to know about. Both are controlled in the popup menu to the left of the script.

image

For pretty much all script execution, you can now explicitly control which variables are passed to the script execution. Limiting the variables sent can reduce any risks associated with leaking data to an environment you don't entirely control, as well as slightly reduce execution time (probably imperceptibly).

Note that for Shell scripts, ENV_ environment setting variables will always be set.

Existing actions will be set to include all variables, but for new actions they may default to not including variables.

Also in that menu for JavaScript execution, you can select Modern Syntax which will be the default for newly created actions (existing actions remain as the traditional syntax). Modern Syntax essentially wraps your script inside a function call, like this:

(function () {
…
})()

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 such as return "Hello";.

Also, when using Modern Syntax, Keyboard Maestro will pass variables in a private local dictionary kmvar, so you can access a variable named My Data with kmvar.My_Data instead of document.kmvar.My_Data. This also means that Execute a JavaScript For Automation scripts can receive variables.

Note that the Execute a JavaScript in Custom Prompt action does not pass variables or use the Modern Syntax since you entirely control the environment and there are other ways to read and write variables.

6 Likes