About Resetting Variables

Hi, @igh_033. @noisneil, @tiffle, and @cdthomer have already shared lots of great information, but I thought I'd weigh in too...

You specifically asked about:

tell application "Keyboard Maestro Engine"
  set value of variables whose name starts with "gsmap" to "%Delete%"
end tell

I've not seen the issue the @cdthomer mentioned, but there's a lot I've not seen :grinning:. So based on his experience, the above should probably be rewritten:

tell application "Keyboard Maestro Engine"
  set value of variables whose name starts with "gsmap" to "blank"
  set value of variables whose name starts with "gsmap" to "%Delete%"
end tell

If you had two variables named gsmap_1 and gsmap_2 (global variables because they do not have a local or instance prefix) you could use the above AppleScript to delete them or you could use the Set Variable to Text action twice:

Keyboard Maestro Export

I've placed them in a Group action and renamed the group (Delete global variables) to provide some simple documentation.

So back to the AppleScript. If I had many global variables with common characteristics*, then I'd probably use the AppleScript approach (as @cdthomer does routinely). Otherwise I use the Set Variable to Text (%Delete%) method.

*Note that the AppleScript is not restricted to starts with. One could delete all of the variables that contain a certain string.

tell application "Keyboard Maestro Engine"
  set value of variables whose name contains "gsmap" to "blank"
  set value of variables whose name contains "gsmap" to "%Delete%"
end tell

As @tiffle stated, there's no requirement to delete global variables, but my personal preference is to do so unless I want to:

  1. retain the value for a subsequent run*, or
  2. pass a value to another macro (I do this rarely).

If you don't delete them, then the Keyboard Maestro Preferences>Variables pane (or Settings>Variables with macOS Ventura) will get cluttered and it will be more difficult to find the global variables that are likely of more interest, i.e., the ones that fall into the two categories mentioned above.

*It's a bit off-topic, but I generally prefer to retain values in Dictionaries, not global variables. My rationale is here.


Also as stated above by others, you generally don't need to delete local or instance variables, but there are times when it becomes useful. For example, suppose you have a loop and you are using a Search using Regular Expression capture group to extract a color from a string. If the color is found, you set local_DetectedColor to the value. In this situation you'd want to place the following before the Search using Regular Expression:

Keyboard Maestro Export

Yes, you wouldn't necessarily need to use the %Delete% token, but I like to use it because it helps reinforces the purpose of the action.


With local and instance variable, my personal preference is to use this convention:

local_SomeDescriptionInCamelCase
instance_SomeOtherDescriptionInCamelCase

Legal variations of local above are:

local SomeDescriptionInCamelCase
LOCAL SomeDescriptionInCamelCase
local__SomeDescriptionInCamelCase
Local__SomeDescriptionInCamelCase
local Some Description

There are other legal permutations, but regardless, I prefer the format I mentioned above. Here's my explanation and a few caveats:

  1. I only use two underscores for situations where they are needed, i.e., 1) with Prompt For User Input when variable names double as dialog labels; and 2) as parameters within Subroutines. That way, when I see a variable that includes two underscores e.g., local__Color I know that the variable is used in one of the special contexts.

  2. I only include spaces in variable names if the variable doubles as a label in a Prompt for User Input dialog as mentioned above. For subroutine parameters, I use the format local__SomeSubroutineParameter. Notwithstanding the dialog labels, I avoid spaces because there's the huge advantage of being able to select a variable name using โŒฅโ‡งโ† or double-clicking if the variable name is devoid of spaces.

  3. It's trivial, but I prefer lowercase local and instance because with my eye, I'm able to more quickly identify the unique portion of a variable name. There's the bonus that lowercase letters are slighly narrower in the KM editor.

5 Likes