Can I pick a variable dynamically, using a variable?

It it possible to put set variable name fields to the value of other variables?

Not sure whether this is relevant to your broader goal or context, but one way of picking a KM variable name from a list might be:

(function () {
    'use strict';

    var lstNames = Application('Keyboard Maestro Engine')
        .variables()
        .map(function (kmvar) {
            return kmvar.name();
        });

    var su = Application("SystemUIServer"),
        sui = (su.includeStandardAdditions = true, su);

    
    if (lstNames.length > 0) {
		sui.activate();
        var lstChosen = sui.chooseFromList(lstNames, {
            withTitle: 'Choose KM variable',
            withPrompt: 'Choose:',
            defaultItems: lstNames[0]
        })

        return lstChosen ? lstChosen[0] : undefined;
    }
})();

2 Likes

Thanks for sharing this script, Rob.

It is very useful in it’s own right, but also serves to illustrate:

  • Getting an array of property names from objects using JXA
  • Display list of choices using JXA
1 Like

Hey Jack,

I’m not clear on what you mean.

Your action shows you placing a text-token for a variable name in a variable name field.

No can do.

You cannot use a variable to set the variable portion of the Prompt for User Input action.

If you really need to do this you could:

  • Export the action to disk.
  • Open it in a text editor and extract the text between the outermost <dict>…</dict> tags.

You can find/replace on the myVariable string and change it to anything you want.

set myUserInputAction to text 2 thru -1 of "
<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>IsActive</key>
  <true/>
  <key>IsDisclosed</key>
  <true/>
  <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>Test Do Script</string>
  <key>Variables</key>
  <array>
    <dict>
      <key>Default</key>
      <string></string>
      <key>Variable</key>
      <string>myVariable</string>
    </dict>
  </array>
</dict>
"

tell application "Keyboard Maestro Engine"
  do script myUserInputAction
end tell

This is a bit cumbersome, but it’s very powerful.

-Chris

2 Likes

Thanks, all. This gives me an idea of what’s required. Seems like a bit of a rabbit hole.

I’m not clear on what you mean.

Your action shows you placing a text-token for a variable name in a variable name field.

No can do.

@ccstone, that was indeed the goal—I wondered if KM could expand the variable text token to its value and then look up that variable name. So, for example, if my variable Count was set to a value of 1, the prompt would display 1 as the variable label in the prompt.

No big deal—after the weekend I have thought of a smoother way to do what I want!