Does 9 loops, stops in the middle of 10th

Hi everyone. First of all thank you for this awesome software. Saved a lot of time.

Here is the question. KM does 9 loops and stops in the middle of the 10th. It always stops on the same step. I thought maybe because this step was pixel color recognition, so I added a random click before that, but now it stops on this random click.

I manually started KM from the beginning like already 20 times and can confirm it is always in the 10th loop and always on the same step.

What can be the possible cause?

I am on the paid version of it is important.

Thank you.

Can you post your macro here? Without it, it's hard to even guess as to why it might be stopping. The easiest way to do that is to select the macro, then click the Share button at the top of the window, and select Keyboard Maestro Forum.

-rob.

Of course. Thank you.

Copy Paste Step 2.kmmacros (115.4 KB)

Thanks!

However ... is there another part of this? I don't see any sort of 10x loop in the macro you posted. Or if it's there, can you tell us where to look? I'm pretty sure I expanded every section, and I don't see any code that's supposed to repeat 10 times.

-rob.

Sorry. I initially thought maybe my macro is too long and I split it on two. Here is the step 1.
Copy Paste Step 1.kmmacros (6.6 KB)

It then goes to the Step 2 (attached before). Step 2 goes to the Step 1 and so on. There are a few if/then checks, so I am not worried about an infinity cycle.

I looked at both, and I saw that each macro calls the other one. That's the only "loop" in the macros. The first macro always calls the second, and the second macro will call the first one if a certain pixels are certain colours. I couldn't find any counter that counts to 10. That is potentially an infinite loop. HOWEVER Keyboard Maestro doesn't support two macros calling each other to a depth of more than 50 in total. So when your macros call each other a total of 25 times each, I think the KM Engine will abort all of your macros.

Therefore I recommend that you stop having each macro call each other, and put them into a single macro, like this:

While "1" (loop forever)
Macro1 actions, removing the final Execute since Macro2 follows
Macro2 actions
End While

Inside Macro2, where you have "If 'condition' call Macro1" replace that with "If 'NOT condition' then Cancel Just This Macro".

If you do what I recommend, then you will not have two macros calling each other, and you can prevent the KM Engine from aborting your macro for calling itself too many times.

However this being said, I'm not sure if this is the cause of your problem. All I know is that this is a potential problem.

My guess is that it's always stopping on the same step because of the fact that you have two macros calling each other, as I said above, and are hitting the Engine's limit. I would have expected it to last 25 rounds, not 10, so maybe my theory is wrong.

You say "loops", but a quick skim suggests it's more like you are doing

macro1
-> launch macro2
   -> launch macro1
      -> launch macro2
         -> launch macro1

...and none of these instances ever complete. So it could be that you are bumping into the "maximum allowed simultaneous macros" limit, which is set to 50 by default.

Check the "Engine log" for errors -- available from the Editor's "Help" menu. You'll probably see lines like:

2024-06-04 18:23:29 More than 50 simultaneous macros - aborting everything

It isn't just "macros" that count towards this limit, nested groups and similar within a macro are also counted -- see this thread for more.

It's not a good idea to increase the limit to avoid the problem for longer! You'll need to rethink your "control flow" and come up with a better way of doing things -- perhaps a "Controller" macro that does loop, calling sub-macros that do their thing then exit and return "control" to the Controller...

But you might -- check carefully! be able to get away with setting your "Execute a macro" actions to be "asynchronous":

...as it looks like they are the final actions in both macros and completion doesn't require any returns from them.

Thank you. I split them because I thought maybe my macro is too long. But when they were one macro, I had the same issue. And it was the same step.

Ahh you are right.
2024-06-04 11:34:00 More than 50 simultaneous macros - aborting everything

Aha! I learn something from you every day. That's probably why he hit the limit at 10 instead of 25.

Sorry, I am just a bit confused how to make a proper loop. I have around 4,000 records I need to copy...

I combined my two macros back in one and inserted put this one in the end, but it stoped again :frowning:

I'm not sure about this -- I'm not seeing anything that actually "Cancel"s your macros under certain conditions (though that may have got lost when you split them).

It looks like you are iterating through a list (of some kind) in a Safari window, processing each item in turn. Better ways to do this include (but aren't limited to):

  1. Gather your entire list first then use a "For Each" action to work through them
  2. Use a "While" loop with a condition, eg "while there are still things I can grab from Safari"
  3. Use an "infinite While" loop, eg "while calculation = 1", but have a "Cancel this macro" within the loop for when you've run out of things to process

It really does depend on what you are doing and, importantly, what sits best in your head when thinking about it!

I'll bet you are still spawning new instances that never complete, rather than actually looping. You'll need to post the whole macro so we can see what's happening.

I only found out when messing with recursion and hit the limit when 25 deep. Thankfully the Forum guru's put me straight...

Thank you. It actually works perfectly. I need to copy data from another machine over remote desktop ... That is why it looks slightly more complicated than it supposed to be :slight_smile:

I am pretty sure I don't understand how to make a loop/repeat action...
ProTrack Copy Paste v2.kmmacros (121.5 KB)

I obviously can't tell exactly what you're doing with the macro, but generally, you should be able to make it do what you want by putting all those actions inside the repeat loop:

That's your entire macro, but inside the Repeat action. It will run 500 times in a row, and then stop. I'd recommend, though, that you first test with the value set to two or three, just to see how it works.

To try on your original, move the Repeat action to the top, then select every other action in the macro except the Repeat, and drag them into the Repeat action.

-rob.

Oh THAT is how it works!!! :slight_smile:

Perhaps the first question to ask yourself is:

What is my "stop" condition?

If you are doing this manually, how would know when to stop doing it again? How can you translate that into terms KM can understand?

Here's an example of "process each line in a variable", where the implicit stop condition is "run out of lines":

For Each Line Example.kmmacros (2.4 KB)

You can do it by testing a value in the loop's conditions:

While Demo.kmmacros (2.7 KB)

Or by running an infinite loop that contains a "Cancel this macro" condition:

While Cancel Demo.kmmacros (3.8 KB)

...and @griffman has shown how to repeat a fixed number of times. And there's more...

They all follow the same pattern -- a "loop" action that contains the operation(s) you want to keep doing along with a stop condition that exits the loop at the appropriate time. Choose whichever method makes sense for the logic of your macro and the way you think about things.

1 Like

I can confirm it is working perfectly now!!!

Now I see that look break you are talking about.

Thank you everyone.