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

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

I see what you're saying, and that seems to prove it. The issue I have now is that my problem apparently lies somewhere else, which I still need to identify.

1 Like

Then I suggest you post your macros so we can better help you.

That's the plan. I just haven't had a chance yet.

Yes -- that's my bad. I've completely confused the issue by being completely wrong!

So the variable is deleted by setting it to the empty string, but the "label" remains in the Variables pane, and that displays the variable as containing nothing. As @tiffle and @Airy say, functionally there's rarely a difference between "deleted" and "empty" because as soon as you reference a "deleted" variable it springs to life, empty!

An example of when it does matter is in the thread you linked -- if you use AppleScript to access the variable directly:

tell application "Keyboard Maestro Engine"
	set value of variable "Global_a" to 1
	get value of variable "Global_a"
	(*1*)
	set value of variable "Global_a" to 2
	get value of variable "Global_a"
	(*2*)
	set value of variable "Global_a" to ""
	get value of variable "Global_a"
Result:
error "Keyboard Maestro Engine got an error: Can’t get variable \"Global_a\"." number -1728 from variable "Global_a"

...and you can see that you can set and get an existing variable, but as soon as you set it to an empty string it is no longer accessible (deleted) so the script errors. That's why it's suggested to use the getvariable verb -- it handles the empty/deleted variable case without erroring.

Ok. Thanks for the discussion y'all. I've been playing around with my macros this evening and that, plus the discussion in this thread, has convinced me that the issue doesn't lie with the way I'm using Pause Until.

I have some ideas on where I may be having issues, so I'm going to explore that more first before posting any further in this thread or, alternatively, start a new thread further down the road if/when I'm still not able to figure out the roadblock. I did manage to further isolate the problem this evening, so I'm hoping that I'll finally be able to identify the culprit soon.

Yes. I think I'm getting it now on how the subtleties of empty variables behave. Thanks for the example.

1 Like

As has been noted, the concept of whether a variable exists or not is not one which has a single answer, and so is generally not one you should ask.

For most practical purposes, a variable “exists” if it has a non-empty value, which is a question you can legitimately ask.

So going back to the original question, it is perfectly reasonable for a macro to Pause Until a variable is not empty.

2 Likes