Easiest way to clear variables


I have a macro that loops, and inside it, I say like If you see this variable do this, if you see this one do this. It works great during the first run, but theeeeen, when it loops, it remembers the 2 past variables so my If actions don't work anymore....is there something I could put at the end of my macro, before it loops, to clear variables? Thanks!!

Example.kmmacros (13.4 KB)

I have to over simplify my long macro to illustrate what I'm trying to achieve, here is a made up example.

I have a number of repeats based on user input, then I search for specific terms in the filenames, and based on the variables that I get from that, I have diff actions. So the goal is not to find another way to do it, but how to make this work (which I guess would be to add something before it repeats that clears the variables) so that I can learn from your trick and tweak it to work in my bigger macro...thank you so much!!!

So we start with this:

Screen Shot 2022-09-30 at 9.12.37 PM

An the expected result is this:

Screen Shot 2022-09-30 at 9.09.32 PM

Well, I found it! Posting it just in case it can help someone else! Just need to do Set Variable to %Delete%

While that sort of does what you want, it's rather the nuclear option and has side effects. It works here because you are testing for the existence of your variables and using the %Delete% token "hides" them from the "If" action.

The more usual way is to test the contents of the variable rather than for it's existence. And the way to "blank" a variable is to set it to nothing -- eg use an empty box if it's a text variable or 0 if it's a number. (The distinction isn't generally important in KM, but it helps you see what the variable is meant to do.)

So for testing:

And to blank a variable:

Also -- and I know it's only a demo macro, but this might apply to your real one -- you only need to blank the variables you've set. In this case you know they're set if you execute the appropriate "If" condition -- blank that variable there and you won't need to nuke still unset variables at the end of each loop (obviously, don't do that if you need those values later!).

You may not even need those variables at all! If you simply want to "do a thing if the clipboard contains" then:

The options you choose will depend on the test you want. You can see how the various conditions work on this page.

1 Like

Jeez! I was so obsessed with all the RegEx that I was doing to get different variables that I forgot that they were all coming from the place >> my clipboard!!.... Thanks for opening my eyes! Using your reply as the solution, thank you!!