Repeating Macro

Is there a way to have the entire macro to run a specified nummer of times

Yes, wrap everything in a repeat action.

1 Like

Great, that worked. Thanks!

And here's another take:

Repeat Macro N Times v1.1_asynchronously.kmmacros (5.4 KB) (5.4 KB)
(v11.0.2)

EDIT: After information given by @peternlewis I've uploaded a v1.1 version here that runs the Execute This Macro-action asynchronously. Read Peter's information in in post #8.

2 Likes

Hey @Alexander

A very interesting way of solving… didn’t know about doing it this way.

Thanks for sharing this.

Greetings from Germany

Tobias

1 Like

I also find this aproach interesting, for some reason. As someone kind of new to programming logic I find it stimulating finding different approaches to reach similar end goals, also when it could otherwise be solved within a single KM-action, like here.

I did however realise here now that this approach could be simplified and improved further, as the content of the first group could just as well be placed within the second one. And by adding a Cancel This Macro-action after the Execute This Macro-action it could also move on to a second part of the macro after the N-th run of the first part. Like this:

Run First Part of Macro- Up Till a Defined Point- N-times — Then Continuing On To Rest of Macro v1.1_asynchronously.kmmacros (5.8 KB)
(v11.0.2)

By moving what was contained within the first group, into the second one, the display text demonstration does not display a correct count on the first run, but that is of no problem for real world use cases. (A correct countdown display could of course anyways be achieved by placing the Display Text-action after the first Switch within the green group.)

EDIT: After information given by @peternlewis I've uploaded a v1.1 version here that runs the Execute This Macro-action asynchronously. Read Peter's information in in post #8.

While on the subject of repeating, and counting.

I was working on something else here now when I realised that we can also basically have a Repeat-action, with a built in counter, by setting a For Each-action up like this:

For Each-action, Set up as "Repeat" With Built In Counter (v11.0.2)

For Each.kmactions (2.0 KB)

The desired number of repeats needs to be set in the to-field in the For Each (now set to 6). It can also be set up giving a countdown counter, by changing the counting upwards selection to counting downwards, and setting the repeat number in the from-field (setting to to 1).

This sort of tail-recursion will not work well in general as Keyboard Maestro has a safety net that restricts recursion to 50 levels. If the Execute Macro executed the macro asynchronously, that would be ok as the first macro would then immediately exit.

But really, it is better to use the Repeat action or one of the other control flow actions like While or Until or For.

2 Likes

Thank you for adding this information and advice!

Just to aid my understanding, what does recursion restricted to 50 levels mean in this context? Am I right in thinking that it means that it will (or could) stop consecutively executed macros after the 50th repeat?

(EDIT: My testing here now indeed shows that both my two alternative approaches to repeat (in post #4 and #6), wont repeat for long. Each and every time i run them – be it if I run each macro with a 1 second pause, 1 hundredths, or no pause at all – they both seem to be killed after 16–17 consecutive runs. (So I still do not understand what levels mean then, it seems, all though a clue seems to be that my number of consecutive runs is close to 50/3, suggesting that each of my consecutive runs of these macros go 3 levels deep, or something like that.) And, as you said it would @peternlewis, when running the Execute Macro-action asynchronously it runs every kinds of numbers of repeats I am asking of it.)

EDIT2: Now updated my uploaded macros, in post #4 and #6, to run the Execute This Macro-action asynchronously

Keyboard Maestro restricts the number of simultaneous macros to 50. Every macro instance that is running counts (so including each level of local macros, executed macros, sub macros, and I think even nested actions count as a different level).

In normal practice this does not happen and is to stop runaway macros that simply execute themselves forever for example.

1 Like