Pause Until Variable Is Not Empty (when variable is previously set to empty string)?

This is a bit of a conceptual question, but I'm trying to wrap my head around why a Pause Until action might fail when checking for the "is not empty" condition of a variable which was previously set to an empty string.

Is there anything "wrong" with setting a variable to an empty string at the beginning of a macro, where that macro then subsequently does actions to populate said empty variable with some value (it no longer is empty) and then the Pause Until action is used to verify that said variable is no longer empty (is not empty), before moving on to other actions in the macro?

No - not at all.

However, if you're using anything other than a local variable then that variable might be cleared by another macro, or even by the same macro if it gets triggered more than once successively (either by accident or deliberately).

Can you post an example of your macro - simplified if necessary?

I can try to post an example later this afternoon. I'm not in front of my computer at the moment. I was just sitting here thinking about some roadblocks I had been hitting, and so I decided to start the thread on my phone.

They are global variables, not local, but that's because I need to access these variables in a different macro. That said, I don't believe they're getting set to an empty string elsewhere, and very little time elapses between when the variable is set to empty and when the variable is then populated again with the trigger value for that macro, so I doubt this is an issue.

Basically, what I'm doing is that I'm clearing out the old values (to ensure that I'm starting from a blank slate, and not using old values) at the beginning of the macro, and then using either the trigger value of that same macro or a variable set from the trigger value of a different macro to populate my current variable of interest. This is then followed up by a Pause Until action which checks to make sure that the variable is no longer empty, because it must contain a value of some kind in order to not cause problems later on in my macros. The set to empty action is just to create a fresh start each time the macro is run.

I similarly am using this approach elsewhere in this same macro to use variables and look up tables to populate subsequent variables, also using the Pause Until is not empty approach, but its still the same concept. I'm resetting a variable to empty, then populating it with something new, and then asking the Pause Until action to verify that it has been populated (is not empty) before moving on to the rest of the macro.

Is it failing to pause, or failing to continue?

There's nothing "conceptually" wrong with what you want to do although, as @tiffle pointed out, global variables come with their own problems. Here's an example of using a global to pause/continue -- "Run Me!" will fire off "Random Timer", put up the first text window, then pause until "Random Timer" sets Global_requiredVar:

Run Me!.kmmacros (4.6 KB)

Image

Random Timer.kmmacros (3.7 KB)

Image

It's pausing, but it's failing to recognize that the variable is no longer empty, so it just pauses until it times out and then it continues onward, because I have it set not to abort on timeout. But it's not recognizing that the variable(s) is not empty.

And depending on how the macro is triggered, the timing of the variable getting filled can vary quite a bit, hence why I'm trying to use Pause Until instead of Pause with a set pause length.

Check your variable names carefully. Check that whatever is setting the value is actually setting it to something and not eg an empty string. You can check the "Pause Until" itself by waiting until it is stuck and then going to KM's Settings->Variables -- find the global, check it is indeed empty, enter even a single character by hand and your macro should continue.

That's the funny thing. The trigger value is what is populating the variable, and that is coming from a MIDI message. So it's definitely not setting it to an empty string. And I can see the variable value populated in KM.

That's why I was wondering if Pause Until isn't meant to work this way, with empty strings. I've read some on how the variable basically doesn't exist if it's been set to an empty string. In other words, when I'm setting the variable to an empty string at the beginning of the macro, is the fact that the variable then currently doesn't exist at that point in time somehow or another short circuiting the Pause Until action? Maybe because the variable doesn't currently exist (because it was set to empty string), Pause Until can't "see" the variable to check if it's empty or not?

You might be thinking of Dictionaries -- you can delete those by setting them to an empty JSON structure.

Globals, even empty globals, persist. Better yet, your "Pause Until... is not empty" test will create the global if it doesn't exist (rather than error out) -- and that global is created empty. You can try this for yourself with my macros above -- open Settings->Variables and find Global_requiredVar (or scroll to where it would be if you haven't run the macro yet). Open "Run Me!" in the Editor and add an extra "r" to only the "Pause" action:

image

...and when you run the macro you'll see the new global appear in the Variables pane. But the macro will pause "infinitely" because "Random Timer" sets the correct variable while "Run Me!" is looking at the incorrect one.

So you need to check your variable names carefully! If you've got it right everywhere except the "Pause" action then your macro won't error out -- but it will never complete, either.

After the Pause Until statement, you could insert this statement:

If it displays anything between the two parentheses, then your problem is that the variable is NOT empty.

I will go and check my variable names, however I'm still confused about the subtleties of how Pause Until works.

Peternlewis states that the variable IS deleted (does NOT persist). See post #17 in the thread below (the whole thread is a good.discussion on this topic though).

I've been basically doing the same thing. I have lots of display text in window actions all over.the place right now, in an effort to figure out where the hang up is. Each instance of this action is displaying different variable values I'm interested in at that specific point in the macro.

Okay, but the code I'm recommending will display the value ONLY when there is a timeout, and you can see what the value is then. My suspicion is that it contains a newline character, which you think is empty but is not empty.

Ok. Good point. I'll try that.

Yes, but a little bit later on he also says:

which should allow your Pause Until to function as desired.

Can you elaborate on this? The Variable Wiki even says that a Global variable doesn't continue to persist (exist) once it's set to an empty string. This all seems contradicting and confusing, so I must not be understanding something.

https://wiki.keyboardmaestro.com/manual/Variables

What do you suppose is meant when he says "internally"?

Also, does any of this stuff change if you reference an empty variable in a different macro versus in the same macro? Maybe that's what "internally" is in reference to?

No - I'm just repeating like a parrot!

If it's a global variable it is the same one wherever it is referenced - there are not multiple copies.

Alright, let me post my macros here in a while, and try a few of the suggestions in this thread to see where that gets me. I'll get back to you once I post the macros. Thanks

I get that global means global, and therefore only once copy. I was just wondering what Peter meant by "internal".

Consider the fact that KM variables are stored/tracked in at least five different places. 1. The KM Engine's memory; 2. The KM permanent storage area which contains macros and variables; 3. The debugger's list of recently accessed variables; 4. The KM Editor's list of variables in the Preferences Pane; 5. Another place I can't remember at this moment.

A common question people ask is whether a variable is empty or truly deleted, and how does one test that? That question is ambiguous because it isn't clear which storage area is being specified. It is entirely normal for one of those storage areas to have an empty variable while in another area the variable doesn't exist. In my opinion there is no functional value to the KM programmer as to whether a variable is empty or non-existent.

1 Like

Well, I can't read his mind but my mind tells me that "internal" just means "within the KM environment". So that anywhere in your macros that you reference a global variable that doesn't yet exist in the KM variables database, KM will treat it initially as an empty variable.

OK - here's a simple test.

This example macro sets a global variable TestNotEmpty to nothing and than executes a Pause Until TestNotEmpty is not empty; then it makes a sound.

Download Macro(s): Test Pause Until Not Empty.kmmacros (2.4 KB)

Macro-Image

Keyboard Maestro Export

Macro-Notes
  • Macros are always disabled when imported into the Keyboard Maestro Editor.
    • The user must ensure the macro is enabled.
    • The user must also ensure the macro's parent macro-group is enabled.
System Information
  • macOS 13.6.1
  • Keyboard Maestro v11.0.2

Running this macro seems to do nothing until I use the KM variables inspector to change the value of TestNotEmpty to something like "x", when the running macro promptly makes a sound and terminates. Thus showing what you expect to happen does actually happen.

Here's the KM variables inspector:

image