Save an Empty Variable

That confuses a lot of people...

I'd forgotten this bit.

I'd also forgotten that "%Delete%" doesn't really delete but just hides.

manual:Variables [Keyboard Maestro Wiki]


Deleting Variables

  • Global Variables, including Password Variables, continue to exist (remained stored) until their value is set to "", the empty string.

  • Setting a Variable to %Delete% does not technically delete it. It continues to exist. It is just hidden from view in all of the Variable lists and in the Preferences.

  • However, a Variable set to %Delete% will behave as if it does not exist in most Actions, like a If Then Else Action using a Variable Condition comparing to exists.


This means you can't initialize an empty variable.

So ā€“ if you want an initial state you'll have to use NULL or something like it.

-Chris

Can confirm this can be confusing.

For example, I just picked up a new MacBook and have spent the last few days configuring it with my apps and macros.

After setting up Keyboard Maestro and syncing the macros, I was surprised to see that a bunch of variables that I havenā€™t used in a while appeared in the preferences pane. This in spite of the fact that I had deleted them on my iMac either through the %Delete% token or from the preference pane itself.

So even though many of them are empty and ā€œdonā€™t existā€, they still show up in the preferences pane on my MacBook now. Iā€™m sure thereā€™s a good reason for this, it just threw me for a loop, especially since Iā€™ve spent the last week or so purposefully going through and cleaning out a lot of unnecessary variables.

By empty string do you mean literally just leave the field blank?

Because thatā€™s how I used to ā€œdeleteā€ my variablesā€¦but they still showed up in the preferences pane, just with an empty field; no text or anything (so maybe I was doing something wrong). Thatā€™s why I switched to the %Delete% token since it at least hides them from my preferences pane.

Keyboard Maestro has a database of variables. There is no specific way to delete a variable, so setting the variable to the empty string removes it from the database. The alternative would be that there had to be some manual way to do this which is undesirable.

Internally, if you reference a variable that does not exist, it is considered an empty variable.

Keyboard Maestro also shows you all the variables in the database and all the variables you reference in any macro. Otherwise after creating a macro, the variable would not be shown to let you set its value.

People wanted a way to explicitly remove variables from the list. I have no idea why, but they did, hence the ability to explicitly set the variable to ā€œ%Delete%ā€.

AppleScriptā€™s exposure to variables only includes variables in the database. Hence, variables that don't exist in the database result in exceptions.

Could it be different, sure. But each decision stems from other decisions and has other consequences.

Thanks for the explanation, is there somewhere in the Wiki that explains this better. The global variables don't seem to get created until I run them in a macro and I have referenced macros that I haven't set yet until later in a macro that I had to set first or it doesn't show up in the Global Variables. I even made a macro to set all the variables so they get created even if they are empty so other macros don't fail.

I closed Keyboard Maestro and the engine and after reopening Keyboard Maestro none of the global variables are there so I just don't understand when a variable really exists for a macro or AppleScript to reference and when it doesn't if it shows up in Global variables.

So in short I am not quite following this discussion very well.

So what I understand is that setting the value if it is empty (no space) will delete the Global Variable even though it shows in Global preferences but getvariable doesn't delete the variable. It seems like if you run

to value of the variable

and then run

getvariable

there would be nothing to get but it still does work running it in that order. My apologies for not following this very well.

Along the same lines

  • Setting a Variable to %Delete% does not technically delete it. It continues to exist. It is just hidden from view in all of the Variable lists and in the Preferences.

  • However, a Variable set to %Delete% will behave as if it does not exist in most Actions, like a If Then Else Action using a Variable Conditioncomparing to exists.

https://wiki.keyboardmaestro.com/manual/Variables?s[]=delete

getvariable does not create or delete the variable - it just behaves as Keyboard Maestro behaves when the variable does not exist, ie it returns the empty string rather than an error.

1 Like

I know very little about AppleScript, so correct me if Iā€™m wrong.

set asTestVar01 to value of variable "testVar01" has nothing to with Keyboard Maestro, the variable testVar01 in this syntax does not point to any variable in Keyboard Maestro. It refers to the variable we should have set previously in the AppleScript. Only when the getvariable or setvariable commands are used, we are dealing with variables in Keyboard Maestro.

Okay, you're wrong.  :sunglasses:

The first line is old Keyboard Maestro syntax.

The get/set variable verbs are newer.

Try this:

tell application "Keyboard Maestro Engine"
   return {name of variables, value of variables}
end tell

-Chris

Thanks for your explanation Peter!

I can't speak for others, but personally I don't like clutter, even if it's just digital clutter haha. So the same reason that I disable macro groups I don't need (like at the end of my work day) and have them hidden is because it's easier for me to navigate through my other groups. Same thing for variables. If I don't absolutely need it to stick around, but I can't use a local or instance variable, then I use regular variables and then like to "delete" them when I'm done so I don't have to rummage through them in the preferences pane if I'm looking through there for some reason. This is also useful because before I picked up a lot of tricks with Keyboard Maestro, I had the bad habit of giving my variables rather obscure names, generally acronyms. But if I didn't use a particular macro or variable for some time, I would forget what that means, and when going through my variables list couldnā€™t figure out which variable I was looking for. A combination of better naming and using the %Delete% token is saving me a lot of frustration these days.

1 Like

Thanks for the correction. I've never seen the old syntax. :sweat_smile:

It's on the Wiki.

AppleScript [Keyboard Maestro Wiki]

Search for:

ā€œ--- Create & Set KM Variable ---ā€

-Chris

Ah. Thanks.
It's for pre-v7.1.
When I joined the club, it was v7.3 already. I never looked at that section.

Yep.

There's no need to use that syntax anymore, but it still works.

I'd forgotten some of the subtleties though ā€“ and those complexities were actually why the getvariable and setvariable syntax came about.

-Chris

Thats good to know so I could just as well have typed getvariable "blahblahslfkjdslfjsdlfjsdlfkjsdlfkjsdlfkjf" and it would have worked just fine since I wanted nothing returned.

1 Like