Skip to content

Instantly share code, notes, and snippets.

@RobTrew
Last active March 9, 2024 04:37
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save RobTrew/3eb0911dc2c63feb8ae3 to your computer and use it in GitHub Desktop.
Save RobTrew/3eb0911dc2c63feb8ae3 to your computer and use it in GitHub Desktop.
Example: accessing Keyboard Maestro variables from Yosemite JXA Javascript for Applications
function run() {
// KEYBOARD MAESTRO VARIABLES IN YOSEMITE JXA JAVASCRIPT FOR APPLICATIONS
// FINDING, CREATING, READING, UPDATING
// SIMPLE EXAMPLE - TOGGLING A BOOLEAN KM VARIABLE FROM .js
var strKMVarName = "flag";
var oFlag,
blnFlag;
// Get a reference to a Keyboard Maestro variable (existing or new)
function kmVar(k) {
var kme = Application("Keyboard Maestro Engine"),
vs = kme.variables,
vks = vs.where({name:k});
return vks.length ? vks[0] :
vs.push(kme.Variable({'name': k})) && vs[k];
}
function jsBool(strBool) {
return strBool === "true";
}
// GET A REFERENCE TO A KM VARIABLE BY NAME
// ( CREATING THE KM VAR IF NOT FOUND )
oFlag = kmVar(strKMVarName);
// READ THE VALUE OF THE KM VAR (as a .js Boolean)
blnFlag = jsBool(oFlag.value());
// TOGGLE ITS VALUE
oFlag.value = !blnFlag;
// RETURN THE TOGGLED RESULT
return jsBool(oFlag.value());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment