Save an Empty Variable

Set Variable to Text Action (v9.2)

Set Variable to Text.kmactions (370 B)

Is it possible to save a Variable with nothing in it? I have to put a space in it to get the variable to save which is okay but not my preference. I even tried %20 just to see if that would work and of course it saves %20 to the variable which is what I would expect it to do.

I also tried the %Delete% variable hoping that might work but of course that just removes the variable which is cool (sort of another alternative to Local with unnecessary steps added).

Have a look in the KM Preferences > Variables panel – you'll see your lovely empty variable there, like this:

KM 0 2021-11-01_23-09-23

No need to put anything in it... (even if I can't spell it!)

Haha :smile:

I am actually trying to write nothing from one macro to another so I don't have to write several other macros. I have tried a ton of Token's thanks to you hipping me up to them but nothing seems to work. It has to have at least a space to write to another variable other wise Keyboard Maestro just acts as though it doesn't exist and flashes an error message.

When another macro then tries to reference that variable Keyboard Maestro won't write anything because there is nothing in it.

While I generally use local or instance variables, I often times have info I need to store in a variable for later on, but not indefinitely.

For instance, when a certain app launches on my computer, I have an AppleScript get the screen coordinates for it’s menu bar icon and set about a dozen variables accordingly (because the app is not scriptable directly), that way I can interface with it throughout the day. So I can’t use local or instance variables in that case because they would not be available later on when I needed them. But once that app quits, I set all those variables to %Delete% because I no longer need them and can’t stand clutter hahaha.

Any way you could post the macro you’re working on so we can take a look?

That is cool and helpful to know, I will hopefully remember that in the future when writing macros.

  • Empty - Example For Empty Variable to Delete Macro (v9.2)

- Empty - Example For Empty Variable to Delete .kmmacros (20 KB)

Okay so after further investigation I realized that it has to do with AppleScript not liking it to be empty. Perhaps there is no work around there. Keyboard Maestro displays the nothing text just fine.

For the record I realize what I wrote in this macro can easily be done all in Keyboard Maestro. I simplified this quite a bit because it actually linked to about 8 macros the outputs do different things in three different apps so I figured you didn't need to sort through that mess.

I'm quite the rookie when it comes to AppleScript...but I use a different code to get variables from Keyboard Maestro...

When I run your script in Script Debugger it gives me the following error:

When I modify your script to the following, it runs fine.

tell application "Keyboard Maestro Engine"
	set pomodoroKey to getvariable "pomSesAppKey"
end tell

--set pomodoroAction to pomodoroKey & " ()" & pomodoroLocation
set pomodoroAction to pomodoroKey

Notice that I changed the following line

set pomodoroKey to value of variable "pomSesAppKey"

to this:

set pomodoroKey to getvariable "pomSesAppKey"

And when I use that script in your macro it runs fine. Try this version below.

  • Empty - Example For Empty Variable to Delete Macro (v9.2)

- Empty - Example For Empty Variable to Delete .kmmacros (20 KB)

1 Like

That is great, thanks a bunch. I wonder what the difference is.

set pomodoroKey to value of variable "pomSesAppKey" --No good without space
set pomodoroKey to getvariable "pomSesAppKey" --Works without space

Thanks a bunch!

I couldn't tell you to be honest. I hand't actually seen your way of retrieving a Keyboard Maestro variable in AppleScript before today. Perhaps some of the folks who are more knowledgeable in AppleScript can chime in... @ccstone?

Well I will be using your method from now on unless someone states a reason otherwise. I'll put that one away in the book of AppleScript/Keyboard Maestro code.

Hey @skillet - if you look at the KM wiki Using AppleScript to Get and Set Keyboard Maestro Variables you'll see that @cdthomer has given you the correct way to get the value of a KM variable in an AppleScript.

This forum and the KM wiki are your best friends!!

1 Like

That doesn't happen here - it all works fine for me.

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"
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


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


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.