[KM] DELETE List of KM Variables [SUB-MACRO]

MACRO: [KM] DELETE List of KM Variables [SUB-MACRO]

— VERSION: 2.0 ⇢ 2016-05-28 —
==Updated==: 2017-05-04 5:09 PM CT

DOWNLOAD: Has been Removed Due to Bug in Macro or KM.

UPDATE: 2017-05-04 5:09 PM CT


The primary use case for this macro is to "clean-up", aka "delete" the variables used in another macro. So you would typically call ("Execute Macro") this sub-macro as the LAST Action in your calling macro.

An example, test, macro that demonstrates use of this sub-macro is provided at the bottom of this post.

7f22859fab1fa4216aebd8e01cd86725df31c085

Thanks to @DanThomas for suggesting use of the Execute Macro Parameter. This new version (2.0) now provides 3 ways of transmitting the variable list, including via the Parameter:

5d1d385a2ab7b81a0b47c9f899de7b5b0393adea

Author: @JMichaelTX

PURPOSE:

• Delete All KM Variables in a list of KM Variables
• The main use for this is at the END of the calling Macro to "clean" (delete) the Variables used in that Macro
• To be used ONLY if you have two or more variables to delete.

HOW TO USE:

FROM THE CALLING MACRO:
• Add a "Execute Macro" Action, calling this sub-macro
• Choose the method of transmitting the variables to be deleted.

THREE WAYS TO TRANSMIT THE VARIABLES TO BE DELETED

[1] Set the Variable "KMVars_To_Delete" to list of variables
(Do NOT use the Execute Macro Parameter)

This assumes the Calling Macro has set the Variable "KMVars_To_Delete" to a list of variables, one per line, that are to be deleted.

[2] Set the Execute Macro Parameter to a CSV list

IF there is a COMMA in the Parameter of the Execute Macro action, it is assumed that the Parameter contains a list of comma separated values (CSV) that is a list of Variables to be deleted.

[3] Set the Execute Macro Parameter to the Name of a Variable Name that contains list to delete
and create the Variable with the list of variable names.

It is assumed that IF the Parameter contains just ONE Variable Name that this Variable contains the list (one per line) of Variables to be deleted.


You might also be interested in this macro:
MACRO: [KM] DELETE All Variables Except Those on Keep List


43acf5c0f31725077c30286c78d0be9c32226010


EXAMPLE OF USING THE ABOVE SUB-MACRO

MACRO: DELETE KM Vars TEST

DELETE KM Vars TEST.kmmacros (4.5 KB)

4704e07dc150c970f8a903d97f39b5ac55efd2eb

OR, just use the Execute Macro Parameter:

5d1d385a2ab7b81a0b47c9f899de7b5b0393adea

Tip: Deleting Just ONE Variable

If for some reason you want to use this macro to delete ONLY one variable, just put a comma after the variable name in the Parameter field:

41bb9c70a5c4ce4ccdec03fffb42ce6165a8dc7a

5 Likes

I was just going to write a macro like this, but thought I’d check the forum first. Awesome!

Is there a reason you didn’t pass the list of variables as a parameter in Execute Macro?

Thanks.

Yep. I could not get the Execute Macro parameter field to show multi-lines.
I want to enter and view each Variable on a separate line.
I suppose you could use a CSV list, but that's harder to read.
So, I chose readability over compactness.

@peternlewis: Request # 1,001+ -- make the Parameter field support multilines.

1 Like

###Just updated this macro in the original post (OP).

Main Change:

THREE WAYS TO TRANSMIT THE VARIABLES TO BE DELETED

(1) Set the Variable "KMVars_To_Delete" to list of variables
(Do NOT use the Execute Macro Parameter)

This assumes the Calling Macro has set the Variable "KMVars_To_Delete" to a list of variables, one per line, that are to be deleted.

(2) Set the Execute Macro Parameter to a CSV list

IF there is a COMMA in the Parameter of the Execute Macro action, it is assumed that the Parameter contains a list of comma separated values (CSV) that is a list of Variables to be deleted.

(3) Set the Execute Macro Parameter to the Name of a Variable Name that contains list to delete
and create the Variable with the list of variable names.

It is assumed that IF the Parameter contains just ONE Variable Name that this Variable contains the list (one per line) of Variables to be deleted.

Dan, you're causing me a lot of work! :laughing:

Just kidding. That's actually a good idea, so I implemented it, just for you (and a few others, like me.)

Macro updated in OP.

You sound just like me. You think"It's fine the way it is," then you just can't help yourself.

To be honest, I think you were right the first time. I mean, we're going to set a variable to the list of variable names, right? So why not have it be the one the macro uses? Meh, either way. LOL.

Actually not. You can just enter the variable list in the Parameter field without having to create another variable that contains the list.

I now actually prefer using ONLY the Parameter field for a short list of short variable names. Since I allow you to use COMMA SPACE between variables, it is very readable.

OK, let me give it a shot. I’m about to build some macros that will help gather the variables to delete…

Hmmm, I just had a thought. I’ll be back with more in a while.

Oh, yeah - thanks!! :slight_smile:

I have a situation where I’d like to be able to save and recall a selection of variables like presets. Seems like the what you are doing here could be adapted for such a purpose. How would something like that look?

Thanks,
K

It's not clear what you are asking for.

Please post a new topic in the "general" category, and provide details/example.

New to the discussions here, and happy to find this. I think I’ve found a bug with it, however. I can’t get this macro to work with the KMVars_To_Delete field. It works if I send the variables as parameters in the Execute Macro call. Has anyone else seen this behavior?

This has been working a long time for me.
If you could please post your actual Macro (or the Actions involved in the Sub-Macro), I'd be glad to take a look.

I'm testing it with a modified version of your DELETE KM Vars TEST macro. I modified the macro to add an Alert prior to and after the call to Execute Macro so that the variables can be tested as a pre- and post- condition.

I'm running this against an unmodified version of the [KM] DELETE List of KM Variables [SUB-MACRO], and when I use the KMVars_To_Delete, the Postconditions alert shows the variables still have the original values. If I call using execution parameters it works as expected.

Here is the test macro:
DELETE KM Vars TEST.kmmacros (3.5 KB)

And the delete macro (I just downloaded this again today to verify)
[KM] DELETE List of KM Variables [SUB-MACRO].kmmacros (14.0 KB)

Okay, I just uncovered something that I think is a contributing factor to this.

If you test the macro by clicking on the “Try” button in KM, the test will work.

If you test the macro by Ctrl+Opt+Cmd and typing in the name of the test macro, it will fail.

I believe this has to do with the values of the myTrigger variable being set by triggering the test via macro name.

The best/easiest way to do that is to open the KM Preferences Variable window and watch the variables of interest:

Here I have entered "TEST__" in the Variable Search to watch those variables.

OK, I'll test your test macro. Give me a while -- it might be tomorrow before I can get back to you.

Bingo! You hit the nail on the head!
####Effective Immediately -- Quit Using the Macro at the top of this topic.

This seems like a bug in KM to me, because the sub-macro is reporting the Trigger of the main, calling Macro, instead of the Trigger for itself.

IAC, I now have a better method of cleanup that uses a script to accomplish the same thing.

Please see if this will work for you. Let me know if you have any questions.

##Macro Library   @KM Delete Variables with Script


####DOWNLOAD:
<a class="attachment" href="/uploads/default/original/2X/7/7f40779ad47a975ca75b5078d52608635b201e6b.kmmacros">@KM Delete Variables with Script.kmmacros</a> (4.2 KB)

---

###ReleaseNotes

**How To Use**

1. Enter the KM Variable Prefix (like "TEST__") used with the variables in your macro that you want  to delete into the  "Set variable KMVarPrefix" box.
.
<img src="/uploads/default/original/2X/a/a32a047ec29b3edb132109b32721c68ad1cd3d1a.png" width="291" height="61">
.
2. Enter a list (one per line) of KM Variable names into the "Set variable KMVars_To_Delete" box.
.
<img src="/uploads/default/original/2X/f/f865d8b190e27b01cd6b90c1ac7a44a338bf92a9.png" width="301" height="60">

You can leave either/both empty if you wish.

---

<img src="/uploads/default/original/2X/5/51542799de34c2dec016c34b744372433d32bb6b.jpg" width="461" height="1162">

---

###AppleScript to Delete KM Variables

```applescript
tell application "Keyboard Maestro Engine"
  
  --- DELETE ALL KM VARIABLES WITH THIS PREFIX ---
  
  set prefixStr to getvariable "KMVarPrefix"
  
  if (prefixStr ≠ "") then
    set value of variables whose name starts with prefixStr to "%Delete%"
    --display notification "All KM Variables with PREFIX = '" & prefixStr & ¬
    --  "' have been deleted." with title "Keyboard Maestro Script"
  end if
  
  --- DELETE KM VARS IN THE VARIABLE "KMVars_To_Delete" ---
  
  set varsToDeleteList to paragraphs of (getvariable "KMVars_To_Delete")
  
  if (varsToDeleteList ≠ {}) then
    
    set varsToDeleteList to varsToDeleteList & {"KMVars_To_Delete"}
    
    repeat with oVar in varsToDeleteList
      setvariable (oVar as text) to "%Delete%"
    end repeat
    
    --display notification "All KM Variables in 'KMVars_To_Delete' have been deleted." with title "Keyboard Maestro Script"
    
  end if
  
end tell
```
3 Likes

I’ll give it a try later. For some reason, when I try and import that macro I’m getting the spinning beach ball and must Force Quit KM. I’m trying to import in safe mode (https://wiki.keyboardmaestro.com/manual/Macros).

Does this go for everyone, or just phookz?

I would prefer that everyone quit using it.
There is a bug in either the Macro or KM that is causing the TriggerValue token to be not handled properly. I'm investigating.
My apologies to all who are uses of this macro.

See

2 Likes

Not sure I'm supposed to use this macro (based on 2017-05-04 updated note).
I find it very helpful (using it as a sub-macro).

One note: When I first tested it I wrongly assumed I should "Disable" the "Set Variable" action I had left blank. After re-enabling it the macro worked like a charm!

1 Like