How to pass multiple variables from Applescript to KM?

Hi,

Currently I am using the ‘user input’ box available in KM listing out names, allowing the user to select the name as a check box. I am now trying to replace this ‘user input’ box with a Applescript, one of the reasons is because the user input box does not have a scroll screen so i can fit all the names (50 names) onto the user input box as the list is then bigger than the screen so the user would not be able to see all the names on the user input box. The user would then select the names, the names selected are Variables and have the assigned the value of ‘1’ to them and the unselected names would have a variable value of ‘0’ assigned to them in KM

Instead I intend to use the following (example, mine would have a selection of 50 different names…) in Applescript;

choose from list {"Peter", "Mark", "John", "Jill", "Joe"} with title "Select Numbers" default items "1" with multiple selections allowed

This option brings up 5 names

Which code would I have to use in this example for the names selected by the user above to have the value 1 assigned to them in KM and the unselected names to have the values 0 assigned to them in KM. We can assume that the selection names above are also the same Variable names used in KM

e.g. in the above example the user only selects Peter, therefore in KM all the names except Peter as above would have a variable value of ‘0’ and Peter would have a KM variable value of ‘1’. If user then (repeated) selected three names such as “John”, “Jill”, “Joe” then these names would have a KM variable of ‘1’ each and the remaining two names which were not selected would now have a KM variable value of ‘0’

I hope that I have not confused, a simple script setting out how to deal with one name would be really helpful.

Thank you

Hey There,

Something like this would work:

set nameList to {"z_Peter", "z_Mark", "z_John", "z_Jill", "z_Joe"}

tell application "Keyboard Maestro Engine"
  repeat with i in nameList
    try
      set value of variable i to 0
    on error
      make new variable with properties {name:i, value:0}
    end try
  end repeat
end tell

set myChoice to (choose from list nameList with title "Select Numbers" default items {item 1 of nameList} with multiple selections allowed)

if myChoice ≠ false then
  tell application "Keyboard Maestro Engine"
    repeat with i in myChoice
      set value of variable i to 1
    end repeat
  end tell
end if

Note: the default items in the choose-from list must be a list.

I have prefixed the variable names to sort them together in the KM variable prefs pane for convenience.

Keep in mind that the names in this list are NOT variables to AppleScript — they are only text.

But that is not an obstacle to using them in Keyboard Maestro.

-Chris

1 Like

Hi, thank you very much for this. It works perfectly. :smile:

My only question now is I too am learning Applescript are you using an App or something to write the above code? I could have never thought of it. Maybe you can direct me to some sources that would really allow me to become advanced in Applescript. I still haven’t learnt how to print a file with a preset as of yet.

Many thanks.

Hey There,

Yes, of course.

The Script Editor comes stock with OSX (called 'Applescript Editor' on 10.7—10.9), but I use Script Debugger — a commercial product that totally eclipses Apple's Script Editor.

I put my References For Learning & Using Applescript up on Gist.

I haven't used AppleScript for printing, so I have no direct advice — but I would ask on the Applescript Users List and MacScripter.net. Someone has most likely fought that battle before.

-Chris