Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JMichaelTX/07829fdf62e0f722a14f to your computer and use it in GitHub Desktop.
Save JMichaelTX/07829fdf62e0f722a14f to your computer and use it in GitHub Desktop.
JXA Functions to Get and Set Keyboard Maestro (KM) Variables using JavaScript for Automation (JXA)
//=====================================================================
function setKMVar(pstrName, pstrValue) {
//=====================================================================
var app = Application.currentApplication()
app.includeStandardAdditions = true
var appKM = Application("Keyboard Maestro Engine")
var oVars = appKM.variables
try {
oVars[pstrName].name();
} catch (e) {
appKM.variables.push(appKM.Variable({'name': pstrName }));
app.displayNotification(
pstrName,
{
withTitle: "Set KM Variable",
subtitle: "Variable was Created",
soundName: "Basso"
});
} // END try/catch
oVars[pstrName].value = pstrValue
return
} // END function setKMVar
//–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
//=====================================================================
function getKMVar(pstrName) {
//=====================================================================
var app = Application.currentApplication()
app.includeStandardAdditions = true
var appKM = Application("Keyboard Maestro Engine")
var oVars = appKM.variables
try {
var strValue = oVars[pstrName].value();
} catch (e) {
strValue = undefined
app.beep()
var oAns = app.displayAlert('KM Variable does NOT exist', {
message: 'Var Name: ' + pstrName,
as: 'critical'
})
} // END try/catch
return strValue
} // END function getKMVar
//–––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––––
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment