Reset global variable

I know that Local and Instance variables get deleted after the macro runs, whereas Global don't.
I was reading this topic and I understand what happens when a global variable is empty (it gets deleted) or when we use %Delete%.
It seems that using %Delete% keeps the variable, but it's empty?
Can someone explain what is the logic behind this, if we can't see it in the list?
Why does an empty variable get deleted, but using %Delete% keeps the variable, but hidden? Shouldn't this be the opposite?

Now my question is:
I have a global variable where I will be adding new lines over time. At some point, I want to grab all that info and send it to the clipboard. When I do that, I want to clear the variable, because next time I add new lines, I don't want to append, I want to add new lines from scratch.

I could use a prompt to decide what to do such as replace the current info or append (for example the first time I would pick Replace, then after that I would pick Append), but I would like it to be simpler and avoid me thinking what I need to do, clicking more buttons, etc.

Here's how the current behavior can be tricky:
Let's say I have Macro 1 and it has an action that creates a global macro "test" (let's assume that a local variable isn't that the macro needs).
If I then empty it or use %Delete% that variable will not be visible on the list.
Now if I'm working on Macro 2 and I want to know if a particular global variable is available by going to the list of variables and I forgot about Macro 1, I could make the mistake of using "test" again on Macro 2, so if I'm appending lines using Macro 2, when I run Macro 1 it will break things.

I think empty global variables should stay visible and %Delete% should do what it says: delete the variable from the list. I honestly don't understand the reasoning behind the current behavior and why it needs to be confusing?

I might be missunderstanding what you need or point towards, but if you empty and/or delete your variable after having dumped the content to your clipboard, you wouldn’t need to create the variable first for you to append to it. The ‘Append variable with’ action will create a new variable (if it does not already exist) just as well as the ‘Set variable to’ would.

1 Like

There are two "lists of variables," and they behave differently. Which one of these are you talking about:

  1. The list in the KM / Settings / Variables pane?, or
  2. The list in the KM Debugger when you click on the Variables button?

The issue here is not about creating variables. The issue here is:
Why does KM delete a global variable, if it's empty?
According to this reply:

Empty variables are completely deleted
But variables set to %Delete% are not deleted
Wait... what? So empty = deleted, but %Delete% = not deleted :hushed:

Peter's words, not mine:
"In Keyboard Maestro, if you set a variable to the empty string, then the variable is removed and no longer exists.
If you set it to “%Delete%” then the variable exists, but its display in variable lists is suppressed, and its value is functionally empty."

My issue with this, as I mentioned, is that global variables can cause big issues if they are not available on the list, but are being used in a macro.

For example, let's say I set a global variable called "list".
And every day of the week I like to add book titles to that variable. Then Sunday night I run a macro that grabs all that info and creates a .txt file with all those titles. After that, I want the variable to be empty again, because I do that every week.

Now, of course, after a while I don't remember what the name of the global variable is, because I just run the macro, I'm not looking at the variable name.
Then one Monday (when the variable is supposed to be empty, but because it's empty, it also disappeared from the variables list) I decide to create a new macro and I also need a global variable. I go to my variables list and I see that there's no "list" variable variable. Great. I create the macro, add an action to create a "list" variable.

Then Tuesday, Wednesday, etc, I keep running the 1st macro to add book titles, but I'm also running the second macro everyday, also using a variable called "list". I guess you can see where that goes, right? They are both using the same variable. Mess!
If I were able to have an empty variable that's visible on my list of variables, I would know that some macro is using the variable "list" and I would pick a different name.

The first one

That's good to hear. I didn't want to start thinking about it only to realize you were talking about a different list.

My issue with this is the way %Delete% doesn't delete a variable, hides it (which I have no idea what the resining is behind it). What's the difference between deleting a variable when it's empty vs hiding it?
What's the goal and the benefit?

I've been researching it for the last 15 minutes before I start to read your post.

This was a tough topic to understand. I had misunderstandings myself. And there's a chance I'm wrong in something below. I'm just understanding some of this for the first time now.

What you are asking for there is not possible. Not at all. There is NO WAY for the KM Editor to determine "if a variable is used in a macro." Not even the Engine can determine that. (I suspect Godel's Theorem makes it impossible, but we should save that discussion for another thread.) Variables can be used in macros in a variety of different ways, not simply by name in a visible/obvious token like %Variable%X%. The list that you are asking for would be inaccurate. Do you want to rely on inaccurate lists? It is actually impossible for the KM Editor (or Engine) to know the names of the variables that a macro can create. You specifically wanted to know if a global variable was "being used in a macro", rather than "does the global variable exist." Those are different things, and the first one is impossible to know.

Peter alluded to this when he wrote:

For both of the lists of variables that have been mentioned above, (the one in the Debugger and the one in the KM Preferences Pane) neither of them correspond to what's happening inside KM. Both lists are inaccurate and by the nature of the great power of KM's >>flexible<< design, must always be inaccurate. The Debugger only lists the ones that have been dynamically changed, and the Preferences one only lists the ones that the KM Editor knows about using the incomplete list of techniques Peter mentioned above. But I think I know what you are thinking... you are asking "Why isn't the KM Editor Preferences list of global variables made identical to the dynamic KM Engine as it runs?" That's a fair question. I don't think Peter answered that. I suspect the answer is that it would slow down the Editor a lot because the Editor would either have to read a huge amount of data from the storage files on a continual basis, or Peter would have to create some sort of protocol for the Engine to update the Editor every single time a variable changes. >>>>And even that wouldn't give you a list of all variables that the Macro changes, only the ones that are currently in use!!!!!!!!<<<< This might be possible, but it's a new layer of complexity and burden on the CPU that Peter can't probably see merits very many people. Are you sure this is important to you?

Externally, for our purposes, a variable can either exist or not exist, and those that exist can be empty or not empty. I think it would be possible for KM to remove our access to the "exists" test. All we need is the "is empty" test. I can't think of a single reason I need "exists", when "is empty" will suffice for everything I do.

I won't blame you if you are angry or frustrated by my answer. And I stand ready to be corrected.

I believe you are misunderstanding my words.

I'm not expecting KM to determine anything.
What I expect is that when I go to my variables list, I see that I have a variable called "myBooks" and it's empty. That's all.
So when I'm building a new macro and I know I need a global variable and I want to use the name "myBooks", I can simply go to the variables list pane, scroll, and confirm that it's there so when I add the New Variable action, I know I can't use the name "myBooks" as my new variable.

Now if macro 1 has the action to create a new global variable "myBooks" and by the end of the week I have to empty that variable, it's now gone for that list, right?

If I then create a new macro (macro 2) and I have an action to create a variable "myBooks", since I can't see it on the list, I assume the variable doesn't exist at all and none of my macros is using that name. If the variable was there, empty, at least I would assume that 1 or more of my macros is using it, because that's why I created it. So I can either create a variable with a different name or I can search for that variable in my other macros.

No matter what, I think it doesn't make sense that %Delete% doesn't actually delete, but an empty variable, actually deletes it. Why can't an empty variable just stay visible? We are allowed to have empty variables, right? When we use Local variables, they can be empty when they are created. So my question is: why deleting an empty variable, instead of just leaving it there on the list? Then if we manually delete it or use the %Delete% token, only then it would delete it from the list.
It seems to be a very straight forward approach.

It's almost like:
Me: Hey let me wash your car!
You: awesome. It really needs it. It's so dirty.
Me: Well, by "wash" I mean, throw a lot of garbage to make it even dirtier.

So %Delete% doesn't delete, but "empty variable" deletes it.
Imho, makes no sense...

What you want is just that? I already have that. I have variables in that list that are empty. So I have what you said you wanted.

There are lots of empty variables in my list.

Kapture 2023-11-06 at 13.51.22

Look how I created the variable, removed the Default Value, closed the window and when I went back, it's gone

That list is not equal to the REAL list of global variables in the KM engine. Are you aware of that? My big message to you was mostly about why that is not possible.

That list in that window is just an approximation of the real list of global variables. If you want to know whether a global variable exists, you must ask the Engine to check by running a macro.

On an interview I read online with Peter, I remember him saying that he wishes more people, even people with no knowledge of coding and all that, would use the app.
Well maybe this is one of the reasons why "regular people" may not use it. If you need to know what's happening under the hood or you need to be a bit more "geeky", then it makes people feel that the app is not for them.

As a normal user, I shouldn't have to know that variables exist somewhere else, but are not visible to me.

If I create a variable, I expect it to be available to me, visually speaking. I don't think that's too complex to understand from the user perspective. It's like "yeah the variables exist, but you can't see it. If you want to see them, you have to go to another app and know what to search for". No, that's not how it should work if this app is supposed to be used by people with "basic" skills. At least not this kind of scenario.

As I said, %Delete% should do what it says: Delete the variable completely.
Deleting the "Default Value" should keep the variable, but empty.
I just tested and if I set the variable to empty using an action, the variable remains there on the list and maybe that's why you have empty global variables.
So when Peter says that setting the variable to empty, deletes it, it's not true. It's partialy true:
If you make it empty via the variables pane, it deletes it.
If you use an action to set the variable to "empty", it doesn't.
See the inconsistency in all of this?

We can always find workarounds for everything, but I think it's not how an app should work.

The editor is further away from the real global variables than the Engine. I tried to support your claim when I told you to ask the question, "Why isn't the KM Editor Preferences list of global variables made identical to the dynamic KM Engine as it runs?"

And then I answered that question.

Actually, editing the variables in the KM Editor window does nothing to the Engine's real database of variables. The Engine doesn't even have to be running when you delete entries in the KM Editor's list of variables. How would you expect KM to deal with that?

You are looking at it almost like from a "developer" perspective.
If an app has a "variables" pane and I create a variable, I expect the variable to be there.
As an average user I shouldn't be required to know "why" it's not there.

Let me give you a silly example:
You sell cell phones and you have the emojis, right?
Now let's say it's an iPhone and Apple decided that all emojis that you didn't use in 30 days, are hidden, but Apple doesn't tell anyone about this, only the "geeks" know it.
So the average user, after a while, will see that their emojis are gone, one by one.
And when you try to ask someone, a "geek" they tell you, yeah, now you have to go to another app, to see all the available emojis.

Can you see how this is confusing?

I have a preferences pane called Variables.
I can create variables.
I expect the variables to be there until I delete them myself.
That's all.

I shouldn't be required to even know about the Engine, how or why the variables are not there. If you as a developer offer a feature, it should work as expected.
This is basic UX design.

Again, I can create workarounds, but it doesn't make sense to me, no matter how many many people explain "why" it works this way.
If too many variables have an impact on the CPU, it should be up to the user to make decisions to manage their variables, not the app itself.

The more I think about your arguments, the more I think that the only way to tell the user the whole truth, without any error, is to remove the list of variables in the Editor and force the user to go through the Engine only. Maybe a menu from the Engine's system tray icon. What do you think? What method do you think is best for a user to query the Engine. The Engine has the ONLY AUTHORITATIVE list of global variables, for reasons I explained earlier.

Indeed, I do agree with you that imperfect data can be confusing. So now it's up to you to explain how you want to get the true data to the user. I explained in my long post why the Editor cannot do that. So now it's your turn to explain how the data can get to the user without any error.

If the average user who wants super simple macros is able to create global variables, then I don't think adding an extra layer of complexity such as using the Engine (which to be honest I don't even know how to access that or what to look for) is going to help at all.

You have to understand that this app is not just for people with advanced skills or advanced macros. This is not promoted as "an app for people with complex demands and people with understanding of how computer work under the hood".
That being said, you are expecting the average user to understand what the Engine is, what "ONLY AUTHORITATIVE list of global variables" means, etc. I don't consider myself a beginner anymore, even though I'm not an expert, but I don't even know what that means. Do you expect a beginner to know that info?
Do you expect someone to understand that they added a variable to a pane, removed the "Default Value" and when they came back, the variable is gone?

We have to put ourselves in the shoes of those people as well, unless you are creating an app that only "experts" and people with more advanced skills will use. That's not the case here.

I think you are making a big deal out of this, to be honest.
Let me ask you:
what's the difference between having a global variable with a value of 1, or having no value at all? Does that have a real impact on the CPU or the performance of KM?
So having an empty variable is supposed to have MORE impact than a variable with the value of "1"?
Because that's what I don't understand. Variables with values are visible, where variables that are empty, are hidden...
Make it make sense, because to be honest, I don't understand the logic behind it...

Moving the path from the Editor to the Engine is not adding a layer of complexity, it's removing a layer of complexity. That faulty layer is the Editor. This is the cause of your problems. So you should be begging for KM to remove that faulty layer and go straight to the source. The source is the Engine.

User > Editor > Engine > Files

Removing the Editor removes a layer of indirection and complexity. If you do this:

User > Engine > Files will get to see the true, unadulterated global variables. There will be no layer of indirection that's resulting in user confusion.