[KM] DELETE All Variables Except Those on Keep List

MACRO: [KM] DELETE All Variables Except Those on Keep List

DOWNLOAD:

[KM] DELETE All Variables Except Those on Keep List.kmmacros (10 KB)

Use this macro to delete ALL KM variables except those you want to keep.

If you find any bugs or issues, or would like to make suggestions for improvement, please post below.

See Technical Note at the bottom of this post.

==Note==: 2019-08-03 16:49 GMT-5
Now that we have Local and Instance Variables , the best practice (IMO) is to use those rather than Global variables. Thus, this macro should not be needed as often.


You might also be interested in this macro:
MACRO: [KM] DELETE List of KM Variables [SUB-MACRO]


List of KM Variables to Keep

NOTICE: Be sure to edit the KEEP List in the macro before you use to delete your variables. Each KM Variable should be put on a separate line, with just the Variable name. Do NOT use %, space, or any other characters before/after the variable name.

EXAMPLE:

You can run the first time, and CANCEL to get a list of your variables on the clipboard, which you can then edit in a text editor like TextWrangler to build your keep list.

Output of Macro Results

If you click the "Delete" button, then the process will continue and your non-keep variables will be deleted. The results are put on the Clipboard, and shown in a KM window:

My thanks to @ComplexPoint (Rob Trew) for the inspiration for this macro. See his JXA script here.


PURPOSE:
• Delete ALL KM Variables EXCEPT those on the KEEP List
• To just get list of existing variables, CANCEL when prompted to confirm deletion. No variables are deleted if CANCELED.
• Results are copied to Clipboard, and displayed in window.

HOW TO USE:
(1) Edit the KEEP List below ### IMPORTANT ###
(2) Run this macro
(3) CANCEL to stop process and display list of current KM Variables


# TECHNICAL NOTE

Neither JXA nor AppleScript can actually see ALL of the KM Variables that are shown in the KM Preferences. So you may see some KM Variables, other than on your Keep List, remaining AFTER you run the macro.

From Peter (@peternlewis):

The preference pane includes variables that are referenced by any macro or that have a value and that is not %Delete%.

The variables result from AppleScript includes variables that have a value and that is not %Delete%.

5 Likes

Great script,

Is it possible to publish the same script, but without user promt window, so whenever it executes - it deletes all variables “silently” thank you!

1 Like

Hi guys, so what line should I modify so the script would run silently without a pop-up window with delete action?
Thank you!

There's probably a better solution for you than this. At the end of each of your macros, use a "Set Variable to Text" action and set each variable used in the macro to %Delete%. Well, each variable whose value you don't need to keep.

Something like this:

Or you can use this macro:

thank you Dan, thanks for answering so fast.
Its a great macro, I’m trying to implement fully automatic set of scripts so at the end Delete All Variables would clean the clipboard.
with list of KM variables you have to insert them manually but it’s doable

OK, I’ll let the original author respond to your question.

Hey Costa,

Here's the simple way to handle that.

property keepList : items 1 thru -2 of {¬
   "click_X", ¬
   "click_Y", ¬
   "ENV_PATH", ¬
   ""}

tell application "Keyboard Maestro Engine"
   repeat with theVariable in (get variables)
      set varName to theVariable's name
      if varName is not in keepList then
         setvariable varName to "%Delete%"
         try
            delete varName
         end try
      end if
   end repeat
end tell

I've used vertical list notation, because it's easier to sort and maintain than the horizontal. The last null-string item ("") in the list is pruned on compile and is there to make the list that much more readable.

-Chris

[ Edit: 2016/07/12 04:01 CST – minor bug-fix. ]

3 Likes

Thanks,

Can you please post a sample script since Im still new to JXA and other scripts?

If I execute this script on its own, variables still remain in clipboard :frowning:

What do you mean by “still remain in clipboard”? Did you mis-type?

If not, what this should do is remove them from the list in “Preferences->Variables”. It has nothing to do with the clipboard.

Sorry I mistyped.

Somehow I need to run macros couple times in order to clear variables and that's what I'm gonna do.

So thank you!

TEST.kmmacros (3.7 KB)

Also – it seems like [KM] DELETE All Variables Except Those on Keep List.kmmacros does it better, but since I want to do silent execution I will use your script.

Hey Costa,

Not Dan's script – mine.  :wink:

See post #7 for a slightly improved version.

[ Edit: 2016/07/12 04:01 CST – minor bug-fix in Post #7's AppleScript. ]

-Chris

1 Like

THANK YOU!!!

Great script.