[SOLVED] Reseting list of variables not working

This is one of those cases where I have no clue what's wrong...
I'm trying to reset a list of variables to the same value, 0
This approach seems like it would work, but their values don't change.
Am I missing something here?

Reset all global variables to 0 copy.kmmacros (23 KB)

Keyboard Maestro Export

This is a tricky one.
See Dynamic Determination in Variable Name Block in the wiki.

The Set Variable action can process tokens, so we can use the %Variable%x% syntax directly:

Reset all global variables to 0 copy.kmmacros (23 KB)

Macro image

Keyboard Maestro Export

1 Like

I was looking at the macro, before you replied, and I think I understand what's happening.
Basically when I'm using just Local__vars on the last action, it's targeting the value itself, in this case it was set to "Default Value", so basically I was saying: Set "Default Value" to 0
Instead of "Set resetTest01 to 0"

So by adding %Variable% in the beginning, now I'm targeting the variable itself, not the value. Is that it?

In the original macro, you were setting a variable called Local__vars to 0, and due to the loop you were just doing that same action 3 times.

Then, since it's a local variable, it is destroyed as the macro finishes. Remove the Local__ prefix and you should see what is happening in Preferences → Variables.

I'm not sure I understand this... Let me explain:
I created the 3 global variables manually in the Preferences

They are all, by default, set to "Default Value".

Now if I'm using the For Each and I'm reading each line from the first variable I set (Local__listOfVariables), wouldn't it mean I would be targeting the global variables one at a time? So the first pass would target resetTest01, then on the second one wold be resetTest02, then resetTest03? That's the normal behavior of the For Each. Reads the first line, applies the actions to that line, then moves on to the next one, etc.

I think the issue here (and it was solved when you changed the variable at the top), is that when it sets the variable "Local__variables" (so in this case, each line of the "Local__listOfVariables"), it was doing this:
Set "Default Value" to 0
instead of:
Set the value of the variable "resetTest01" to 0

That's why your approach works, because it's processing normal text "resetTest01", and then adding %Variable% in the beginning and % at the end, making it now target the variable %Variable%resetTest01% instead of targeting "Default Value"

Does it make sense?

So basically, my macro was doing something like this:

And yours is doing this:

I was surprised to learn that tokens for variables (combined with text and more tokens) were allowed in the Set Variable field.

In theory, your original expectation of how the Set Variable to Text action could work is viable, because the Engine could evaluate the variable and discern from the variable's contents whether it contains any tokens or not. But that seems like a very risky thing to define in a language. The language already has a mechanism to do what you want, as @avtraino said, so I guess that's the answer.

1 Like