Skip to content

Instantly share code, notes, and snippets.

@kjaymiller
Created September 6, 2018 17:23
Show Gist options
  • Save kjaymiller/40d5e99ec3561b49d5b8f9a3db9a6ebf to your computer and use it in GitHub Desktop.
Save kjaymiller/40d5e99ec3561b49d5b8f9a3db9a6ebf to your computer and use it in GitHub Desktop.
Keyboard Maestro Script to Loop Through Array
function createButton(variable)
{
return `<dict>
<key>Default</key>
<string>1|0</string>
<key>Variable</key>
<string>a`+variable+`</string>
</dict>
`
};
function run(){
var app = Application.currentApplication()
app.includeStandardAdditions = true
var kme = Application("Keyboard Maestro Engine")
var variables = kme.getvariable("Items").split('\n')
var buttonVariables = [];
for (var variable of variables)
{
buttonVariables.push(createButton(variable))
}
kme.doScript(`<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<dict>
<key>Buttons</key>
<array>
<dict>
<key>Button</key>
<string>OK</string>
</dict>
<dict>
<key>Button</key>
<string>Cancel</string>
<key>Cancel</key>
<true/>
</dict>
</array>
<key>MacroActionType</key>
<string>PromptForUserInput</string>
<key>Prompt</key>
<string>Please enter the details for these variables.</string>
<key>TimeOutAbortsMacro</key>
<true/>
<key>Title</key>
<string>Untitled</string>
<key>Variables</key>
<array>`+buttonVariables.join('\n')+`
</array>
</dict>
</array>
</plist>
`)
selectedVariables = []
for (let variable of variables) {
if (kme.getvariable('a'+variable) == 1)
selectedVariables.push(variable)
}
return selectedVariables.join('\n')
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment