Subroutine FTW

I haven't needed to write any new macros for about a year. Today, the need arose, and I discovered to my delight that there is now an automatic subroutine maker at "{Gear Icon} ▹ Engroup ▹ Macro". Bravo! Have wanted this since I started using KM (and even now don't really know how to have asked for it).

There is of course the possibility that this has been there all along, and that I finally understood it. In either case, almost every macro I make repeats groups of Actions; this makes writing them much easier and much less prone to user error.

Thank you.

4 Likes

Thanks for pointing this out! :+1:
I use "Engroup" all the time, but have never noticed or used the "Macro" option at the bottom.

2 Likes

Yeah that would be cool if it actually functions as a subroutine to repeat commands within a macro. Is that what you're doing with it?

We've been able to call a separate macro previously with the Execute Macro action but repeating a set of actions within a macro has been a puzzle.

I'm not sure what you mean, but there are several loop Actions that allow you to repeat a set of Actions: Until, While, Repeat, For Each
If you mean repeat the same set of Actions at different locations in the Macro, then that is exactly what a Execute Macro (call a Sub-Macro) can be used for.

I am also unsure what you want. @JMichaelTX has responded already. In the hope that it helps, here is how I use "{Gear Icon} ▹ Engroup ▹ Macro":

Select a sequence of Actions in a Macro that you want to repeat as a subroutine.
. Execute "{Gear Icon} ▹ Engroup ▹ Macro".
. Keyboard Maestro creates a new Macro and a new action and names that action "Macro-name Subroutine". The newly-created Action simply executes the newly-created Macro.

Then when I want to repeat the subroutine, I just copy the single Action "Marcro-name Subroutine".
When I want to change the subroutine, I edit that Macro. Since my change is to the Macro executed by the Action that replaced it, the edited Macro is executed in every instance of the Action.

For me, this is simple, and helpful in several ways (as already noted, lessens user-errors, makes editing easy and effective), and makes the step-flow of the Macros easier to parse.

I am not a programmer. But to me, this "actually functions as a subroutine to repeat commands within a macro". Tell us more about what it isn't providing that you would like it to.

1 Like

The Macro option for Engroup isn't documented and rather than explaining what I meant, I thought I'd just try it out.

I engrouped three actions: initializing a variable to an empty string, prompting the user to enter a string for that variable and sending a notification displaying that string.

I wanted to repeat these three actions if the user entered a blank in the prompt by simply pressing Return. So I wanted, as I said above, to repeat the same three actions in two places in the same macro.

When I engrouped the three actions Keyboard Maestro made a new macro in the group (not the macro) which it calls with an Execute Macro action.

Which is how I've simulated subroutines in the past. Put the common actions in their own macro and call that macro in your main macro wherever you need it.

I was hoping Engroup > Macro would encapsulate the actions within the main macro, not as a separate macro.

I'd post the sample code but it's incomplete because the engrouped code is off in another macro .

Agreed, being able to call a separate macro from a macro is a big help. I have a group called Functions for my collection of them. But what they go with and where they are used is not obvious. Certainly not as obvious as it would be if they resided within the macro that uses them (like subfunctions in JavaScript or Perl do).

KM has very limited support for what programmers would call subroutines or functions.
I tend to think of Sub-Macros as more like a code library, than I do of internal functions.
But the way I mange that is:

  • If the Sub-Macro is designed for use by only the macros of a Macro Group, then I store the Sub-Macro in that Group.
  • Else I store the Sub-Macro in a "library-like" Group I call, well, "Sub-Macros".
  • I try to give the Sub-Macro a unique name, or at least contain a unique keyword/ID, so that I can find all instances of its use by a KM Editor Global Search.
  • I have a Macro/Script that will open the Sub-Macro used in a selected Execute Macro Action in another Editor window.

As I'm sure you know software design is almost always a matter of balance, or trade-offs. So, if I have a macro that uses the same 2-3 Actions as a set, then I would likely first put them in a Group Action to easily identify and maintain, and just copy the Group Action to a new location within the Macro when I need it.
If it looks like I am going to use this Action set a lot, then I will create a Sub-Macro with those actions. This Action Gear Option to engroup with a "Macro" makes that easy.

One final note on reuse of common Actions: I use MACRO: [KMFAM] Favorite Actions and Macros by @DanThomas extensively. This includes putting multiple Actions, usually as a Group, as one KMFAM item.

Here's a good KMFAM example:

It works like a good function library when you copy (rather than reference) the "function" to you current code (Macro). It is easy to update the KMFAM item, but it does not, of course, change any of the existing copies in my Macros.

1 Like