Delete All Variables Except…

Thanks to the introduction of Local Variables with KM8 you might feel the need to clean up your variables inventory.

The macro deletes all variables from KM’s inventory except the ones excluded.
It makes use of the “Variables” collection, introduced with KM8.

The macro does the same as @ccstone’s AppleScript, but I found it to be considerably faster:

Deleting 1600 variables (as created e.g. by this macro) takes 5 minutes with the AppleScript, but just 8 seconds with this macro, on my MBP 2017. [1]


Clean-up Variables.kmmacros (27.5 KB)

You should configure the macro by customizing the Regular Expression for the exclusions.

Out of the box (as shown in the screenshot) any variable whose name starts with ENV_ or Persistent is excluded from deletion.

The “Persistent” keyword is an example. Replace it with your personal pattern for variables you want to keep. However, do not remove the “ENV_” exclusion, since this prefix is used for environment variables, for example ENV_PATH, ENV_PERL5LIB.

Examples for exclusions:

# default:
^(?:ENV_|Persistent)
# a different prefix:
^(?:ENV_|MyMacro)
# an additional prefix:
^(?:ENV_|Persistent|MyMacro)
# only the prefix for environment variables:
^ENV_

To make the regex case insensitive prefix it with (?i), for example

(?i)^(?:ENV_|Persistent|MyMacro)

See the Regular Expressions reference for more variants.



If you don’t want to use Regular Expressions you can also use string conditions like this:

Note that this is always case _in_sensitive.



[1]: For an optimized version of the mentioned AppleScript see @ccstone’s post below. It runs as fast as this macro.

5 Likes

Thanks for a great, and very fast macro, Tom.

One suggestion, change your default RegEx to:
(?i)^(?:ENV_|DND_|kmfam|Local|Instance)

Reason for Changes:

  1. Make case insensitive since use of KM vars is the same
  2. Many of the macros that I have published use a prefix of "DND_"
  • and later, I believe @DanThomas started using this convention.
  • These are global variables that you "Do Not Delete", because they are used in later executions of the same macro, or in companion macros.
  1. The KMFAM set of macros published by @DanThomas all use a prefix of "kmfam".
  • I'm not sure which ones must be global, but to be safe, I don't delete them.
  • When I have some time, I'll do more research to confirm/correct this.
  1. Include "Local" and "Instance" as a prefix to NOT delete because if someone, like me, makes a mod to your macro and uses some "Local" variables they will get deleted before they should be.

I don't think there is any harm in making these changes, and you can obviously note to the user that they might not need them.

FYI, my mods to your macro were simple.

  • Change RegEx that EXCLUDES Variables
  • Display How many Variables were deleted
  • Display Which Variables were NOT deleted.

In case you're interested, here's my version:

####MACRO:   Clean-up Delete Variables @KM8 @JMichaelTX

~~~ VER: 1.0    2017-10-28 ~~~

####DOWNLOAD:
Clean-up Delete Variables @KM8 @JMichaelTX.kmmacros (29 KB)
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.


Hey Tom,

The current form of that AppleScript is not very different than the one you mention, and it runs in a bit less than 8 seconds with your ~1600 variable data set if the Keyboard Maestro preferences window is closed.

(If the prefs window is open it takes right at 8 seconds.)

This is on my venerable Mid-2010 MacBook Pro.

-Chris

--------------------------------------------------------------------------------
# Auth: Christopher Stone
# dCre: 2016/07/28 14:42
# dMod: 2017/02/06 15:17
# Appl: Keyboard Maestro Engine
# Task: Delete Variables Whose Name is not in the Exclusion List.
# Libs: None
# Osax: None
# Tags: @Applescript, @Script, @Keyboard_Maestro_Engine, @Delete, @Variables, @Exclusion_List
--------------------------------------------------------------------------------

set pinnedVariableList to items 1 thru -2 of {¬
   "ENV_PATH", ¬
   ""}

tell application "Keyboard Maestro Engine"
   set varNameList to name of variables
   
   repeat with varName in varNameList
      if varName is not in pinnedVariableList and varName does not start with "DND_" then
         try
            setvariable varName to "%Delete%"
         end try
      end if
   end repeat
   
end tell

--------------------------------------------------------------------------------
2 Likes

Hi Chris,

thanks for the note and the script. I’ve added a footnote to the OP.