Variable apparently doesn't exist after `Set Variable To`, but can be shown in Alerts

Hi there,

I've only ever built some simple macros and I'm sure my issue is caused by a noob mistake, but I can't for the life of me figure it out or find useful help online. I would appreciate it if someone could provide some insight.

What I'm trying to do

It's programming 101 stuff: I want to create a variable that acts as an index, and check its value against a constant so that a while loop will continue as long as the index is less than the constant.

How I did it

The screenshot below is taken after I pressed the Run button (equivalent to ⌃⌘R) at the top of KM's interface. You can see that

  • Under the Set Variable action, the current value of the variable localInboxIndex is still empty instead of 0
  • In the Alert window, the value of localInboxIndex is 0 (the value 25 belongs to the aforementioned constant)
  • In the conditions section of the while loop, I deliberately changed the condition from "index < constant" to "index exists", to demonstrate in the screenshot that as far as the while loop Action is concerned, the index variable doesn't even exist

My questions

  • What on earth is happening here with these apparently contradictory information? Does the variable exist or not?
  • If my approach is fundamentally wrong, then what are the best practices for creating a while loop that goes on as long as the index variable is less than a constant?

Thank you again for helping out someone who's building a complex macro for the first time!

Could you explain how it demonstrates that?

I recreated your macro and in the while section I put in an Alert action. Running the macro displayed that alert, which indicates that the variable exists. What do you get when you try something like that?

In your example, did you perhaps mean to put the alert inside the while loop?

2 Likes

As @kevinb has said, you need to have your Actions inside the loop for the loop to do anything. Here is an example of a loop where the variable is incremented by I on each loop, until the variable is more than 5. (The reason for the pause Action in the example, is that otherwise the loop would run too quickly for you to see the numbers counting up.)

You could also use an "Until" Loop. A difference between the "While" loop and the "Until" loop is that the "While" loop will exit the instant the condition is met and the "Until" loop will still complete all its Actions after the condition is met and then exit the loop.

EXAMPLE While Loop with Changing Variable.kmmacros (3.8 KB)

Click to Show Image of Macro

2 Likes

Just an FYI, if you’re basing a local variable’s existence on the real-time “evaluate conditions” feature, that’s where the problem lies. Local variables do not exist outside of the macro’s execution... which means that the real-time evaluate conditions feature of many actions (as shown in the in your screenshot) will never show local variables, since they are created only when the macro executes, and no longer exist once it’s done executing.

1 Like

Hi @kevinb, @Zabobon and @cdthomer , thank you so much for your helpful replies! I didn't realize that's how local variables work, and now my questions are answered (and then some!)

Thank you :smiley:

Edit: Only now did I accidentally find this snippet (from KM wiki's Variables page) while searching for other problems:

Macros can create or read existing variables, which persist and are permanently stored (except Local and Instance variables which are transient, and Password variables which are never saved to disk).

3 Likes