Then run the AppleScript from the Script Editor or Script Debugger.
tell application "Keyboard Maestro Engine"
set asTestVar01 to value of variable "testVar01" -- No good without space
set asTestVar02 to getvariable "testVar01" -- Works without space
end tell
I'd expect the value of method to work and return an empty string just as the getvariable method does, but it will throw an error.
On the other hand this works with either of the variable creation methods even though the make new command creates it without a value.
tell application "Keyboard Maestro Engine"
# Create Variable with:
make new variable with properties {name:"testVar01"}
# OR
setvariable "testVar01" to ""
set asTestVar01 to value of variable "testVar01" -- No good without space
set asTestVar02 to getvariable "testVar01" -- Works without space
end tell
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.
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.
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.
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.
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.
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.