Thanks to KM 10's new %AccessedVariables% token, we now have a much easier way to erase any global variables our macros may use. Just add this action to the end of any macro with global variables you would like to clean up when the macro is done:
That's a great idea. I wish to add that if you put that code inside an IF statement that looks like this, then it will execute the cleanup operation only if the CAPS LOCK button is activated. This will give the user a little control over if/when the cleanup happens. The user can clear variables at the end of any macro when the CAPS LOCK button is depressed. Nobody uses the CAPS LOCK for anything anyway. It's the most useless key that ever existed.
Just came across this and I like it. I just downloaded it to try it out.
Currently I use an AppleScript to delete all my debug_ variables, but occasionally it only deletes about %90 of them. @DanThomas said he experiences the same thing whether he uses AppleScript or JXA so it's not just me apparently.
This might be a more reliable way to cleanup variables. Thanks again for sharing!
I'm curious, though, if it ever ends up having the same issue of some variables not going away, even though you set them to %Delete%?
If it does solve the problem, I want to know why. Because the other way of doing it (deleting variables with a prefix) does actually set all the variables to %Delete% - it's just that sometimes, some of the variables refuse to go away. So like I said, if this new method never has that problem, I want to know why. Because, you know, curiosity and/or WTF?!
Setting a variable's value to nothing or to %Delete% will cause it to be unavailable to AppleScript:
tell application "Keyboard Maestro Engine"
exists of variable "winList"
end tell
However – setting to nothing will leave the variable visible in the Keyboard Maestro variables preference panel.
The AppleScript above will return false in both cases.
I just ran my delete variables script.
set pinnedVariableList to {}
tell application "Keyboard Maestro Engine"
set varNameList to name of variables
# set varValueList to value of variables
repeat with varName in varNameList
if varName is not in pinnedVariableList and varName does not start with ¬
"DND_" and varName does not start with "ENV_" then
try
setvariable varName to "%Delete%"
end try
end if
end repeat
end tell
Here's the the seeming result in the KME's variables panel:
Just for giggles I'm going to set the variables to something like "NULL", before I set them to "%Delete%" and see if it make a difference to the display.
I rate this as a bug, but I'm not absolutely certain it is one...
I've known about this for a long time. I know I "spoke" with Peter about it, maybe 5 years ago, but I don't recall what he said, other than ever since then, I just shrug my shoulders and say "whatever".
I’m interested because I have certain global variables that I use during work or other things but don’t need them once I’m done. So I don’t necessarily need them to be DND_ prefixed variables since I do eventually delete them.
Ok so I just tried this in an extremely simple macro that consisted of setting three variables, and then deleting them.
It was a disaster.
I have no idea what happened but it wiped out a LOT of my global variables... including some of my DND variables. I have NO idea how it happened... I can post the macro here if you want to see it...
It just dawned on me that using %AccessedVariables% to delete "unwanted" variables might be a horrible idea, at least until @peternlewis changes something.
This may not explain @cdthomer's situation, but it's important anyway.
I'm gonna start off talking about a Custom HTML Prompt, but stick with me here, because I think this may extend beyond just the prompt.
A week or two ago, I discovered that if I ran my Variable Inspector Prompt (custom HTML prompt) twice in one macro, the second time %AccessedVariables% returned ALL KM variables.
When you run a Custom HTML Prompt, KM puts ALL KM variables in "document.kmvar". I believe that when KM does this, it is inadvertently marking every variable as having been accessed by the macro. I think Peter agrees with me here, but I won't speak for him.
So if you were to use the "Easy Global Variable Clean-Up" macro after running a Custom HTML Prompt, you'd probably delete all your variables.
So that's bad as it is. But I'm wondering if other things may inadvertently affect %AccessedVariables% that we don't know about.
Thanks for this info Dan. It sounds like what happened to me because initially I had run your VIP just before the "delete" action... which might explain why it nuked dang near my entire variable list
So now I'm thinking I should add a warning to the post for VIP. What do you think?
@gglick I don't know if you've been following along, but if not, read my post above. How do you think we should handle this? (And if you need more explanation, let me know.)