Assign a string to a variable whose name is the current value stored in another variable?

That’s a mouthful. :smile:

When I press option-J, I want to assign the currently selected text to variable “j”. I’m able to create an interim variable called “newVar” which I substring and filter from %TriggerValue% to have the value “j”.

Now I would like to programmatically store the selected text to “j” in a helper macro, rather than “Set variable ‘j’ to text '%CurrentClipboard%”, because I’m making a lot of these. Is this possible? (is it clear?)

Thanks! Excited to see the forum.

You cannot "indirectly" access or set a variable within Keyboard Maestro.

You would have to use AppleScript, which would then be relatively easy.

2 Likes

Thanks a ton for that. If I wanted to be all 21st century and use JS for the same purpose, is it possible?

Then, a related question: If I wanted to paste in the value of a variable, using the letter/number used in the trigger hotkey to refer to the variable name (e.g. ⌃F --> %Variable%f%), how can I do that?

There are various trigger text tokens. See the Edit ➤ Insert Token menu. Probably %TriggerValue% will do what you want, although you will need to remove the modifiers yourself with a search & replace.

Yes, you can use JavaScript for Automation to control Keyboard Maestro Engine. Use an Execute Shell Script action like this:

osascript -l JavaScript <<'EOM'

var app = Application('Keyboard Maestro Engine')
var n = app.variables['A'].value()
app.variables[n].value = "Hello"

EOM

1 Like

Yes, thank you. I’m clear about how to use TriggerValue, but less clear on how I might trigger pasting in, for example, variable “a” from having pressed command-A – programmatically (with a “helper” macro" that can take input of any trigger).

Something like:

Trigger Command A
Actions
Set Variable T to text %TriggerValue%
Search Variable T for regular expression ([A-Za-z]) into variable V
Filter Variable V by Lowercase
Insert Text by Typing "%Variable%V%"

If that is not what you’re after then I think perhaps you need to actually stop and define what it is you are trying to do and clearly explain that.

2 Likes