Execute next macro in group

Is it possible to run a macro and at the end add an action that runs the next macro in that same group?

You can add an Execute a Macro action to the end of your macro. You will have to explicitly state which macro you want to run, but that seems safer and less nebulous to me than thinking in terms of "the next" macro.

If your plan is to chain macros, so that, say, macro A runs macro B which runs macro C, consider instead implementing a macro that calls those macros in the order you require. Perhaps also A, B and C may be better implemented as subroutines – depending on your needs, of course.

Yes, that's what I am doing, but wanted to see if I could just pick "Next Macro in Group".

For this particular case, there are no "safety" issues, because I created a group just for that.

I just want to have an IF THEN action at the end and if the condition is true, then it will run the next macro. This would be useful for example if you have 20 identical macros that you want to run one after another, and having "Next Macro" you could just duplicate the macro and not worry about modifying 20 actions to run specific macros, which is my case.

I know that I could put everything in a single macro and have it cancel or continue running the macro, but sometimes I like to have macros separated from each other to make it clear what I'm doing.

To be honest, I still haven't quite understand the concept of routines vs macros. I think I need to be in a situation where I actually need something and routines will be my ah-ha moment. So far, I haven't really understand their purpose and potential, at least not for the 700+ macros I created.

I am sceptical that having so many identical macros is the best approach, but I did duplicate a macro once today for one of my own macro groups, so I certainly won't prejudge your requirements! :smile:

A subroutine can be a convenient way of having a sequence of actions that can (if required) be passed a parameter, and which will return a result to the calling macro.

One can instead still take the traditional route of having a macro call another ("sub-")macro, but passing values from one to the other is messier.

Just one example of how subroutines can be useful, taken from real life: have a macro that formats the URL and page title from various Web browsers, a couple of which require some hacky workarounds; rather than have a macro that is cluttered with unrelated sets of actions for each browser, call subroutines that have the steps needed for each kind of browser, and then handle the results they pass back.

Yes indeed, you can take them on then.

It's always a bad idea, until it's the best (or only) approach haha

So basically using a (sub) macro is more like a "read-only" version. It runs and that's it. The subroutine can then output something that the main macro can use, maybe a value, a variable, etc, something like that?

So basically it's almost like a "variable" where you can change it and it will affect everything that used that "variable", in this case the subroutine, at the same time since each browser (using your example) has some unique "characteristics" each subroutine for each browser would output something different to the main macro. Something like that?

But yeah, I think I will have to really need it one day to fully see it in action on a more practical example for my own case.

But thanks for your detailed info. That is already a great help and I will save this on my KM Tips doc :wink:

1 Like

Before V. 10 of KM, there were no subroutines, but you could call one macro from another macro, using the Execute a Macro action. And of course you can still do this, and it may often be all you need.

So what does a subroutine offer that you can't do just by having one macro call another?

The KM Wiki explains that when one macro calls (executes) another macro, there is a limitation:

The Execute Macro action allows you to execute another macro... though without any ability to pass parameters or receive a result other than through variables...

So if you had a macro called "Main macro" and it called "Helper macro", and you wanted to pass values between them, it could be fiddly, or at least inelegant.

And that is the problem that subroutines solve.

Imagine that "Helper macro" could be replaced with something that was exactly like a macro (a sequence of actions) but actually could receive and return values easily and directly. Well...

That's a Keyboard Maestro "subroutine"!


  1. A subroutine in the general computing sense is defined by Wikipedia (and I think it will do!) as "a sequence of program instructions that performs a specific task, packaged as a unit. This unit can then be used in programs wherever that particular task should be performed".

  2. When Keyboard Maestro documentation talks of "subroutines" it is nearly always referring to KM's special callable units that can receive and return values – those neat things I have described above. However, if you read the page for the Execute a Macro action, note that the opening sentence uses the word subroutine in the general computing sense when it says that a macro can be executed by another macro "like a subroutine"! :exploding_head:

  3. Since the idea of subroutines in the broadest sense is not unique to KM, you can find plenty of examples of where you might want to think in terms of using subroutines. Here is one.

1 Like