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.
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
repeat x times or until thing works
So that I'm adding stuff rather than picking holes , 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.
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.
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!
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:
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.
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.)
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.
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.
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
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.