Save an Empty Variable

Thanks, I appreciate the link, that was my bad and just some code I had been using for over five years to get Variables in AppleScript and had not run into an issue until now. I should have looked there when I found that AppleScript was having an error. It is still curious to me but very glad it is working without the space.

Very strange that my earlier code worked fine for you, @cdthomer code made it work for me.

1 Like

Thanks for verifying! I'm fairly new to AppleScript so I was wondering if @skillet's method was a deprecated method or something.

1 Like

Understandable. I do things that are technically out-of-date and not the "right" way because it's what I picked up years ago and never saw the need to change since they still work haha.

1 Like

For what it is worth I had t to change a few of these back in some of the AppleScripts since I started getting different errors. I will post a link to all the macros when completed for anyone that is interested.

tell application "Keyboard Maestro Engine"
	--set pomodoroEvent to getvariable variable "pomSesCatAppKeyIntDurLoc" -Errors out.
	set pomodoroEvent to value of variable "pomSesCatAppKeyIntDurLoc"
	-- set pomodoroCatKeyInt to getvariable variable "pomSesAppCatKeyInt" --Errors out.
	set pomodoroCatKeyInt to value of variable "pomSesAppCatKeyInt" --
	set pomodoroDuration to getvariable "pomSesApp__Duration" --The reason why it has the two underscores is so we can display the name "Duration" only but that unfortunately has to save to a globlal variable with the two underscores as well in Keyboard Maestro. See "How to Hide a Variable Prefix in the Prompt" https://wiki.keyboardmaestro.com/action/Prompt_for_User_Input
end tell

tell front document of application "OmniFocus"
	set theTask to make new inbox task with properties {name:pomodoroEvent, note:pomodoroCatKeyInt, estimated minutes:pomodoroDuration}
end tell

@peternlewis


Hey Guys,

This looks like a bug to me.

Run the macro.

Make Empty Variable.kmmacros (1.6 KB)
Keyboard Maestro Export

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

-Chris

1 Like

Thanks Chris great to get your input and expertise!

Here's a link to what you all helped me create, thank you so very much!

1 Like

If you set a variable to the empty string, the variable is deleted and no longer exists.

So the variable "Whatever" will fail, because it is referencing a variable that does not exist.

I don’t know why you wouldn’t expect it to throw an exception if you refer to a variable that does not exist.

This is a primary purpose of the getvariable command, which does not have this issue.

What is confusing to me is I can see the variable there in the Global Variables so it exists but it just doesn't have a space in it.

Global Variables preferences show variables that exist or are used in any macro.

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: