Does This Variable Exist or Not?

I'm using a variable that supposedly does not exist to create an infinite loop so I can use the 'Break From Loop' action at some point.

I know it's bad design but it works with the macro it's actually in and should be ok.

The thing is, when I go to KM > Preferences > Variables, it appears to exist:


However, when I run a test the loop is indeed infinite because it is not evaluated to be in existence.

Why does the variable show up if it doesn't exist?

Can you post your macro and describe what you're trying to do with it?

It's proprietary stuff and unfortunately rather large so I can't really mock it up.

Mostly I'm just interested in how a variable shows up if it doesn't exist

A variable can be empty. Try using a Set Variable to Text action and leaving the text blank to see for yourself. To delete a variable, set the text to %Delete%.

Hey Greg,

That's just the way Keyboard Maestro works...

Global variables show up in the variables panel of the Keyboard Maestro Editor if they have been created and not set to %Delete%.

The variables pref pane does not show variables that exist – it shows the variables it wants to whether they exist or not. (Somewhere on the forum Peter explains this at some length.)

Set a variable:

Variable Existence Tests.kmmacros (2.1 KB)
Keyboard Maestro Export

See the result in the Keyboard Maestro Editor's Variables Prefs:

image

tell application "Keyboard Maestro Engine"
   set variableNameList to name of variables
end tell

Result:

{
   "BookTitle", 
   "DND__gtmbnWindowPosition", 
   "DND__kmfamResourcesFolderPath", 
   "DND__kmfamWindowPosition", 
   "DND__sspResourcesPackagePath", 
   "DND_CF__TargetFolderPath", 
   "DND_KM_TEST_MENU", 
   "DND_MSI_moveToDestinationFolderPathList", 
   "ENV_PATH"
}

tell application "Keyboard Maestro Engine"
   set variableBookTitleExists to exists of variable "BookTitle"
end tell

Result:

true

Set variable to nothing and thereby destroy it:

Variable Existence Tests.kmmacros (2.1 KB)
Keyboard Maestro Export

The variable is in fact destroyed – despite the fact that it still shows up in the Keyboard Maestro Editor's Variables Prefs:

image

tell application "Keyboard Maestro Engine"
   set variableNameList to name of variables
end tell

Result:

{
   "DND__gtmbnWindowPosition", 
   "DND__kmfamResourcesFolderPath", 
   "DND__kmfamWindowPosition", 
   "DND__sspResourcesPackagePath", 
   "DND_CF__TargetFolderPath", 
   "DND_KM_TEST_MENU", 
   "DND_MSI_moveToDestinationFolderPathList", 
   "ENV_PATH"
}

tell application "Keyboard Maestro Engine"
   set variableBookTitleExists to exists of variable "BookTitle"
end tell

Result:

false

Destroy the variable AND remove it from the Keyboard Maestro Editor's Variables Prefs:

Variable Existence Tests.kmmacros (2.1 KB)
Keyboard Maestro Export

tell application "Keyboard Maestro Engine"
   set variableNameList to name of variables
end tell

Result:

{
   "DND__gtmbnWindowPosition", 
   "DND__kmfamResourcesFolderPath", 
   "DND__kmfamWindowPosition", 
   "DND__sspResourcesPackagePath", 
   "DND_CF__TargetFolderPath", 
   "DND_KM_TEST_MENU", 
   "DND_MSI_moveToDestinationFolderPathList", 
   "ENV_PATH"
}

tell application "Keyboard Maestro Engine"
   set variableBookTitleExists to exists of variable "BookTitle"
end tell

Result:

false

You can also test for this with Keyboard Maestro's IF-THEN action:

Variable Existence Tests.kmmacros (6.0 KB)
Keyboard Maestro Export

Personally I don't like the fact that non-existent variables show up in the Keyboard Maestro Editor's Variables Prefs.

Nor do I like the fact that the variables inspector pane is IN the prefs.

But despite a lot of argument and discussion over the years, this is the way Peter has decided to do things – and it's what we have to live with.

Once you accept that and get used to it it's not that big a deal.

-Chris

3 Likes

Lots of good tests here. Much appreciated!

1 Like