Cleaning Up Variables at the end of sub-macros

Probably not a lot of people will run into this - possibly only me, but I thought I’d post it anyway.

Many of us like to “clean up” variables at the end of our macros, by setting all the variables used in the macro to %Delete%.

Good idea. Highly recommend.

However, setting variables to %Delete% is not free, time-wise. In fact, you might be surprised to know it can be slower than you might think.

However, most if not all of the time, you’ll never notice. Especially if you do the cleanup at the end of a macro, after the macro’s done it’s work. You’d never notice, because you’re not waiting on anything.

But I’m working on an extremely complex set of macros, with multiple sub-macros, some of which get called multiple times, and some of which have a lot of variables. Since the cleanup for the sub-macros occurs before the main macro has finished its work, the time starts to become noticeable.

I’m still only talking tenths of a second. But believe it or not, if you get enough of that going on, under the right circumstances, it becomes noticeable.

So I took all the sub-macros’ cleanup code and put it in another sub-macro, which the main macro calls when it’s done. It cut a couple of tenths of seconds off the macro’s launch time, and when I combined that with some other optimization, it made a big difference (to me).

As I said at the start, this is unlikely to impact most people. In fact, it may only ever impact me, because I have to over-engineer everything. But on the off chance this info might someday help someone else, I decided to post it.

1 Like

I used to do this, too. I don’t do it anymore, because it adds superfluous stuff to the macro.

Instead I prefer to post macros either…

  • with variables with unique prefixes referring to the name of the macro, usually consisting of the initials of he macro, or…
  • in case of draft/temporary macros with prefixes like ω or zzz that sorts the variables at the very end of the list.

This makes it very easy —easy enough— for the users to clean up the variables inventory, when needed.

I’m one of those people. I like to delete as I ago, at the end of a macro, both for myself, and so I don’t pollute the environment of others who download/use my macros.

So I don’t see it as “superfluous” at all, but as good housekeeping.

IMO, for 99.9% of the Keyboard Maestro users, the time this takes will be irrelevant, not at all noticeable.

I would agree. :wink:

But I do thank you for posting about it, so that if I, or anyone, happens to run into this situation, we will know what to look for.

However, you just got my brain thinking again (I know, that’s always dangerous), and it occurs to me that one very easy method of handling variable clean up is to have a special Variable dedicated to keeping list of variables to be deleted. For me, that would be something like “DND__Vars_To_Delete”.

Then each macro, and sub-macro, can simply add to that list when the macro is run.
Then, we can have a cleanup Macro (or script) that runs as often as you’d like (in the background) to simply process the list.

Problem solved/averted. :smile:

That's what it looks like in my macros. I can live with that much superfluousness. :slight_smile:

As we’ve discussed before, I would prefer to do what @Tom mentioned, and use a prefix. But “zz”, not “zzz”, because there’ll be no snoozing on my watch! :stuck_out_tongue:


Strangely enough I’ve been working on a macro to add a variable deletion step(s) this morning. If anyone else is interested in them I’ll post them later today. They are based on the techniques pioneered in Chris Stone’s “Paste Dated Attribution Action v2.0” macro

How do you do it? Based on prefixes, or something else?

I posted one awhile ago, which I’ve never been completely happy with. I’ve also been working on a new one for quite a while now, but since I have to decode every single KM Action available, it’s taking a while and I’m not being diligent enough to get it done in a timely manner.

Curious to know your take on how to do it.

Give me a few min. to post it and you can see for your self

Cool. No rush.

There are two macros to this. The second one is meant to be used as a sub-macro which will place a "Group" action into your macro. Once there just use "⌘d" to duplicate its actions.

If you don't want the individual actions contained in a group simply delete the sub-macro and the second step in the Insert Cleanup macro to do away with it.

Insert Cleanup.kmmacros (36.9 KB)

MyGrup.kmmacros (4.2 KB)

Main Macro:


1 Like

The second image is wrong, it should have been for the sub-macro but is not and I’m hesitant to take up the space in the forum.

So what does this do, exactly?

Look at the last step the last step, that’s what it does, inserts a group then an insert text action into the Group action.

The incorrect image has been replaced with the correct ont.

Here’s some observations:

  1. I’d be uncomfortable using these macros, with your copyright notice in them. I’m not sure what rights you reserve, and what I’m allowed to do with the macros.

  2. The temporary items path you use doesn’t exist on my machine. I recommend using “/tmp/”.

  3. You can export both macros into one .kmmacros file, although it takes a little ingenuity. Select the “All Macros” group, then do a search that at least returns your two macros. Then select your two macros and export them, and voila - both macros in one .kmmacros file.

And just so I’m sure I understand, the purpose of this macro is to insert a group that contains one action for setting a variable to %Delete%?

Feel free to use them or correct them however you choose, The attributation text is place by habit and I should have removed it. Both macros are hereby donated to the public domain.

TemporaryItems folder is easily created. and items stored within it will automatically be deleted IIRC. That said I can change the path to /tmp, it acts the same.

Yes but more are easily added with a “⌘d”

OK, thanks.

These are version 1.1 of the two macros posted above. Changes were made to reflect Dan's suggestions for using the /tmp folder and to relieve his concerns about copyright.

Insert Cleanup.kmmacros (36.8 KB)

MyGrup.kmmacros (4.2 KB)

The Variable naming convention is not the point here. Each of us have our own, and it really doesn’t matter to anyone else.

I’m not sure what you mean by “what @Tom mentioned”, since his approach is to do nothing:

My new approach is very simple:

  1. Each macro simply adds to the Delete List.
  • Items in the list could be the full variable name, or a prefix with an asterisk, like “zz*”
  1. At some point in the future, schedule/run the Macro that deletes all Variables in the list.
  • The macro could be run manually by the user