Set variables with name starting with "" to "" value

Is there are way to use the For Each Action with ‘the variables’ collection to set all variables with name starting with “MM__” to a value of “0”? So, MM__Variable1, MM__Variable2, MM__Variable3 etc. are set to a value of 0.


There is indeed:

For Each MM__ Variable.kmmacros (2.5 KB)

The trick is to use %LocalVariable% (or whatever name you choose for each variable in the For Each action) with the token syntax, %Variable%LocalVariable%, in the "Set Variable to Calculation" action, instead of just LocalVariable like what we would usually do with a V variable field. Without the token syntax, the "Set Variable to Calculation" action would only set the LocalVariable variable to 0, rather than the variable whose name starts with "MM__". With the syntax, KM knows to process the correct variable, i.e. the one whose name starts with "MM__". Feel free to ask if you have any follow-up questions!

1 Like

Haha, how did I know it was definitely something simple?

That makes sense to me. Works now! Thanks so much!

1 Like

Hmm, strange. If the MM__ variables in question are empty, this macro does not work; it will not set them to 1 / any value.

That’s probably because it’s using a “Set Variable to Calculation” action. Try using “Set Variable to Text” instead and see if that works; I think it should.

Hah, I was actually using the “Set Variable to Text” action originally and then double-checked your macro and saw that you were using the Calculation Action and tried that instead. Neither seem to work. I wonder if this is by design for some reason?

Variables that are empty do not exist and are not listed.

The Keyboard Maestro editor will show you (in popup menus and the preferences), variables that exist as well as any variables that are used in any action. But variables without values do not exist otherwise, and don’t exist specifically in the Keyboard Maestro Engine.


Ah, makes sense. I wasn’t aware of that but I figured there was a good reason for it. Thanks for the clarification Peter.