Is there a "Clear all variables" command for KM?

I've been using KM for many years and only recently have I learned how incredibly powerful it can be to script and program all sorts of things.

I have a question— Is there a command to "clear all variables"?

I'm using various jumps to other macros, and I use a variable as a one time "flag" for a specific condition or state so another macro loop running asynchronously is aware of that state.

Do I need to explicitly null or re-zero these specific variables used as flags at the end of every macro? Or is there some hidden command to clear all local or global variables at the start or end or every macro? (I don't want/need to "delete" these variables. I just want to clear them or null them.) Thanks in advance.

Yes. These is no "clear all variables" KM Action.

Thank you for the response and the information. It would be nice to have such a command. I guess Peter has to balance power and being judicious about the number of commands KM offers.

There is no command built in to KM but you can roll your own, like this:

To clarify - this doesn’t delete variables, it just clears their contents.

If you don't need to "delete" variables then you really don't need a macro like this and, arguably, if you need to reset variables it might be best to create a macro that is specific to those variables alone and not, potentially, every variable in KM which is the danger with this macro if used carelessly.

Thank you for your response. Interesting approach. But I think I'm going to take your second suggestion- create a separate macro that I can call at the end of any macro that needs to reset the specific flag variables I use. I'm learning to think a bit more like a programmer.

1 Like

I only skimmed this thread, but do you know about Local and Instance variables? (See the Scope section.) This eliminates the need to clear out variables at the end of the macro.

Dan, I'm glad you brought that up. I think you my have hit the nail on the head. I need to use local variables for flags. I've just become aware that they exist and I need to look at KM's documentation to change my flag variables (which I think are global) to local variables. Thank you.

Speaking of "local variables", having looked over the documentation, I'm wondering if the local variable would do it...I'm using the variable %Variable%flag% as a "flag" for an asynchronous second macro that does a repeat loop with a notification that it's running every 30 seconds. Would the "local variable" %Variable%local flag% be seen by the other macro running asynchronously??

No it wouldn’t and neither would an Instance variable unless the second macro is a sub-macro of the first.

OK, let's try this again, this time without accidentally posting the message before I finish. :roll_eyes:

@tiffle is right. For that situation, you definitely need normal variables. And kudos for using some complex logic - not everyone "gets" that kind of logic.

One thing you can do is use a naming convention with a specific prefix. Then you can delete variables with that prefix, using applescript, like this:


Here's the Applescript:

tell application "Keyboard Maestro Engine"
	set value of variables whose name starts with "gsmap" to "%Delete%"
end tell
1 Like