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.
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:
• 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.
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.
###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.
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.
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?
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.
####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
```
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).
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.
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!