I'm looking for some advice on how to structure a macro that needs to re-run itself with updated parameters, while avoiding runaway nesting or multiple instances running at once.
Background:
I have a macro that processes a batch of data step-by-step.
At certain points, when it encounters specific kinds of data, it needs to update some variables dynamically according to defined rules — and then re-run the same macro again, using the updated variables as its new input context.
The Problem:
If I use Execute a Macro to call the same macro recursively, the nesting grows very deep very quickly, and eventually causes side effects or makes the flow difficult to maintain and debug.
What I really want is:
To re-run the macro (starting fresh) with the updated variables;
But also to cancel or exit the previous instance, so that only the latest instance continues to run;
And ideally, avoid using Pause or Delay-based workarounds, which feel fragile.
My Question:
Is there a reliable pattern or best practice in Keyboard Maestro for this use case?
Something like:
A macro that can "restart itself cleanly";
Or a way to transfer control to a new instance, while canceling the current one;
Without breaking global variables or getting into orphaned executions?
Any suggestions or macro structure tips would be greatly appreciated!
One way to do it is to have the Execute a Macro action set to run asynchronously, and then put a Cancel this Macro action after that. Note that you'll also need some logic at the beginning of the macro to insure the globals aren't overwritten with default values—something like "if empty, set to value 'Foobar', otherwise leave alone."
When I need a macro that "calls itself" a large number of times, I create a single macro (see example below) which contains a variable (it could also be a local variable) called "State" which contains the name of the state of my macro, and in the macro is a while loop that runs forever but also contains a Switch statement that executes a different block of code depending upon the value of the State variable. For example, there's a puzzle called the Collatz Conjecture which you might want to implement with a recursive function like this:
f(x)=[if (X MOD 2)=0 then RETURN (X/2) else RETURN (X*3+1)]
This is a function that is recursive, but if you implemented it like this in KM, you would end up with more than 50 recursions for many starting values, (before you hit the number 1) therefore you could use my state table approach to implement it like this:
Using this approach you can essentially have the macro call itself with new values (using local variables) an unlimited number of times. This may not be the solution you want, but it's the solution I use to solve this problem.
Thanks! I actually thought of this approach at first, but it didn’t work as I expected.
However, I just tested it again with a small, simplified macro, and it seems to be working now.
I probably need to go back and double-check my main macro.
Your suggestion really helped me revisit this idea—much appreciated!
Thank you! That's a brilliant idea!
However, my current macro has become a bit too complex (at least for me) — it involves things like automatic web page switching and checking for local file existence.
So this approach might not be the best fit in my case.
But I truly appreciate your input — it definitely gives me some inspiration for future work!