[SOLVED] Retry Loop

I was trying to learn how the Loop actions work and it seems that's different from what I expected. In the example below I thought that the loop was the IF THEN action, but it seems that when I click Continue on the Alert window, it jumps back to the alert.

Keyboard Maestro Export

Is there a way to make it jump to the IF THEN instead?

Well the retry this loop is inside the If/Then/Else action, so “retrying” goes back to the beginning of the actions inside the If/Then/Else, the first of which is the Alert.

I’m not following what you mean by this, could you explain it differently?

Maybe you could engroup the Alert in an Until Action with the condition being the same as in the If Then Else Action.

If the condition is true, the Alert won't show and the Macro will just move on.

If the condition is not true, the Alert will show and pressing continue will loop until the condition is true.

1 Like

I found the issue: I was using the path, which was (of course) not being recognized. I shouldn't have included the /Volumes/ part. It took me a while to figure it out. I guess I have to go back and retest the Retry Loop and see how that goes.

This is exactly what you described and what I expected it to do. So the issue being the name of the volumes being incorrect, it was always going to the alert, making me think that Retry Loop was not going to the beginning of the IF, so the conditions section. I though it was jumping to number #2 instead of #1:

image

Hope it makes sense

1 Like

Yes, I ended up using the UNTIL action, after I saw the issue with my first approach. I was using the path and it seems that KM is not evaluating the path as a path, but as a name. At least, once I changed it to just the name for the volumes, it started working.

image

But I will try it with the Retry Loop as well, now that I saw the issue was not with the action, but with the name/path.

@cdthomer and @Zabobon
So after testing it with the right names (without /Volumes/), it worked with Retry Loop as well:

image

Always learning something new...

3 Likes

While "If... Then... Else..." isn't a traditional "loop" structure, KM's "Retry" treats it as such -- the condition is re-evaluated each retry. I hadn't realised that, and thought it would only repeat the branch it was in.

It's an extreme method of flow control though, and could introduce side effects -- on a "Retry", variables set within the loop are reset to their start-of-loop values. I haven't played around enough to find out how far this goes -- does it reset Globals? I very much doubt it reverts external actions, undoing a file copy for example, but I honestly don't know!

So with a multi-action branch and a "Retry" you could end up in a very unexpected state...

Here's a demo to show the reset -- notice how the macro enters the "otherwise" branch with Local_i = 1, increments Local_i to 2, but after the "Retry" sends it back Local_i is 1 again so the "If" condition is still false.

Be ready to cancel this somehow -- it's an endless loop...

Loop demo 1.kmmacros (7.6 KB)

Image

That's also an unusual way to do what you want in this case. An "Until" loop will always execute at least once. You're masking that here by putting it in an "If" action so that that once only happens when you already know neither volume is available -- but you're doubling up on your logic (and potential errors).

The more normal way to do this would be with a "While" action, no "If" required:

image

...which will keep putting up the alert "While" either volume is not available.

3 Likes