How to cancel the macro if a loop in it completes x repititions?

The loop is a part of contingency if somehow the button on the webpage is not found. It would try x times if it is still not found, I want to cancel the macro without executing further steps.

Here's an example:

Trying loop example.kmmacros (22.7 KB)

Macro screenshot:

Initialise a variable to 0 before the loop, increment the variable by 1 each turn round the loop, include an "If..." action in the loop that tests if the variable is greater than a certain number and, if so, cancels the macro.

:thinking: Hmmm seems like it is going to be complicated than I imagined

I never created such a workflow before

Adding the check/cancel is easy -- but the whole thing might depend on your "Contingency step" loop. You may not need this at all, just check the result of the "Press Button..." action. If that is "OK" then set a flag variable to "Success!" (or whatever) and exit the loop -- the first action of your "later part of the macro" would then be "If flag is not "Success!" then cancel macro.

So do you want to loop x times in the contingency as well as the 3 repeats you show, or is this just a case where x = 3?

First of all, there could be multiple such buttons I'll need to click in a workflow of a single macro.

Second, I would like to have some sort of a template or a subroutine (that would have press button action along with the contingency steps) that I can use in any macro wherever I want to click a webpage button.

I am using this macro. Where, the instagram reel link is already saved to the variable. If the button click fails and I let the macro cancel, next time i run the same macro, it'll fetch a new instagram reel link from the text file and I'll loose downloading and uploading the previous reel video.

I just want it to try sufficient number of times before the macro gets cancelled. Because the button click may not work first time but it may work the second or third time.

Doesn't matter. Take your head-space up a level and consider the flow of the macro rather than the actions within it.

May not be a good idea. The "control step" itself is relatively simple and you can drop your macro-specific actions into it. If you try and create a sub you'll have to pass parameters (name of button and times to try, at least).

That's up to you -- you don't have to just "Cancel", and any time you do "Cancel" you should consider whether you need to save (or restore) state. So you could have "if this bit fails then write out the variables to a text file then Cancel".

Yes, but it's unclear from your example whether you want to a)

repeat 3 times
   do a thing
   repeat x times or until thing works
   end repeat
end repeat

or b)

repeat x times or until thing works
   do thing

So that I'm adding stuff rather than picking holes :wink: , here's a demo of b) which will give you up to 3 chances to provide an answer. Do nothing first run and you'll see it Cancels after 3 unanswered dialogs, enter an answer to any of them and it will run to completion without showing the rest of the dialogs.

Giving Up Demo.kmmacros (6.0 KB)


1 Like

Before cancelling, just write the content of the variable back to the file and then it won’t be “forgotten”.


That didn't come to my mind

Is it possible to write it back to the same file? i have 8 such files :face_holding_back_tears:

This part seems tricky :sweat_smile: or is it not?

If you’re really using that macro I provided then:

  1. You know the name of the file %Variable%Local__File%
  2. You know the URL - %Variable%Local__LineFromFile% that’ll get “forgotten”

Why not?

Oh yesss.

Thank you.

Let me try that.

See @tiffle's answer -- the caveat being that the "Write to a File" action will overwrite the original file contents, so if you have a 5-line original file and a 1-line variable you'll end up with a 1-line file.

You could read in the entire original file to your KM variable, manipulate it there (taking what you need at each stage of your macro), then write the whole variable back to the file at the end/cancellation of your macro. Or you could use the "Search and Replace" action set to "Search: File" to update small parts of the file as you go.

Let me let that sync in :face_holding_back_tears:

Not if you use the append option. But if the order had to be preserved you’d read the file into another variable and combine the two variables, writing them back to the file.

However, I understood that the files would contain only one URL to start with. In which case a plain write would be fine because the file would already be empty.

Which is the correct approach @fluid_eye ?

All very true.

But @fluid_eye does take an idea and run with it, and I expect this to be a sorted 84-line file including URLs, separator emojis, and time stamps by this time tomorrow -- so let's get the warning in early!

Note to @fluid_eye: The above is not an insult -- I'm loving seeing the things you are trying to do!

1 Like

I could do this successfully.

Thank you @Nige_S and @tiffle for helping and also going ahead and thinking of all the other possibilities and coming up with solutions for the same.

@Nige_S - It’s not possible to :heart: a post twice so here’s a third (for good luck) :heart:

Yes I want b)

I downloaded the macro you shared, it works perfectly.

I failed to understand how do I implement this in my case, because the action in your macro is inside the AppleScript.

The AppleScript action represents "a thing that may or may not happen" -- you could replace it with, for example, a series of actions that reload a web page, try and type in some text, then press a button. I used an AS dialog because it's easy for you to control when that "thing" has happened and see how it works.

In both cases you have a loop that repeatedly tries to do a "thing" until a variable is set, and that same variable is set after the "thing" is successfully done[*].

The wrinkle in your case is that you want the loop to repeat a maximum number of times. There's (at least!) two ways of doing that:

  1. Have an "infinite" loop, count how many times you go through it, each time round test the counter against your max number and the value of your "success" variable -- exit the loop if either are true.
  2. Have a loop that repeats n times -- in this case, 3 -- and each time round test the value of your "success" variable. You exit the loop if the "success" variable is set, or the loop ends naturally after n times through.

In this situation I think the second version makes it clearer what you are trying to do because your maximum repeats is right there at the top of the action.

* We're using a shortcut in KM in that a variable you haven't set yet is empty -- in many languages testing an unset variable would throw an error so you'd have to set it to eg 0 before the loop started and 1 when the "thing" was successful.)

Perhaps this example is more relevant.

It's difficult to find a public web page that "sometimes works, sometimes doesn't", so the first actions writes a a page we can use plus one we load when the button is pressed to your /tmp directory. There's nothing nasty, all the first does is generate a random number from 1 to 6 -- if it's a 1 the page has a "Submit" button, otherwise it reads "No button here...". So we've a 1-in-6 chance that there's a button to press every time the page loads.

(Please don't laugh at the HTML/JavaScript -- this is not in my wheelhouse!)

We open a new, empty window in Brave and then set the counter variable Local_i, purely for reporting purposes.

Then the loop. We're going to have up to 3 tries at submitting the form -- first we increment the counter, then load the page. If the button is there the "Submit Front Browser Form" action will succeed, the %ActionResult% will be "OK" -- so we pop a dialog, set Local_foundFlag to "true", and exit the loop.

Otherwise it's back to the start of the loop and the page reload. And if we fail 3 times the loop completes with Local_foundFlag still empty.

At the end we test Local_foundFlag and either a) report that we failed and cancel the macro, or b) how many tries it took us to succeed.

Web Page Reload Demo.kmmacros (11.0 KB)


Hopefully that makes it a bit clearer how you could use this type of structure in your macro.

I did not fully understand what you said but I downloaded your macro and edited it briefly.

Try 3 times if fails and then abort if still fails.kmmacros (37.2 KB)

Now I have a macro that does exactly what I asked for. But the problem is, when I'll implement it, it'll be too much complicated. Because it is not the just the action of clicking the button that I would want to repeat 3 times but also many actions before that for example starting from opening the webpage etc .

It is doable but complicated.

I reached so far just to realise it is not practical :sweat_smile:

I wish there was a mechanism available where I can point to say, step 5 and ask to start from step 5. Till say, step 12. And repeat 5 to 12 x number of times.
Or jumping directly to step 20 from step 17. Skipping step 18 and 19.
I would may be put up a question specifically about this.

and thank you so much as usual for your help and explaining in detail.