Variable Repeat

The numeric field in the Repeat loop, like pretty much all numeric fields in Keyboard Maestro, can contain an expression, which can also include variables or functions. Here is an example showing how you can repeat a variable number of times.

Note that "Count" in the Repeat action will show up in red until it has a valid numeric value (usually when you run the macro the first time and enter the value in the Prompt. You can select the Prompt For User Input action, and click the Try button and enter a valid number to ease your mind that all is well.

6 Likes

Thanks Peter for this example.
Eventually Iā€™ve learned how to work with the count variable!
:smile: :thumbsup:

How do I change this so that it says repeat actions count times?
When I add the Repeat action it defaults to this, I want the user to be prompted each time to input a different value at the Prompt for User Input: see attached picture.

Hey Felix,

Type your variable name where the number is.

-Chris

1 Like

I just discovered this post. Very handy.

Instead of a static number in the Default Value field of the prompt, you can add a variable. So adding %Variable%Count% in the Default Value gives you the last value ā€˜Countā€™ again. Super handy when doing really repeated steps.

1 Like

Try this:

2 Likes

Hi there,
I have gotten this macro to work and itā€™s been working really well in general. However recently, it has not been reliable.

What tends to happen is that it does not repeat the full amount of times entered.

How can I troubleshoot this? I donā€™t know where to start exactly.

Are you sure itā€™s not repeating the correct number of times? If you want to be sure, stick a Prompt action inside the loop, and count the number of times it comes up.

What do you do inside the loop? Can you post your macro?

Thank you DanThomas. I expect it is repeating the correct number of times. I will try putting in the Prompt Action.

Here is the macro attached.
Stem Output (Prompt).kmmacros (10.4 KB)

How should I configure the Prompt action here? I must admit, I donā€™t understand how it works! Iā€™d appreciate any tips!
Felix

Here's an example:

Change the Title (#1), and clear the Prompt (#2). It will display a prompt like this:

So each time through the loop, this prompt will display. Click OK to let it continue, and count the number of times the prompt displays.

Also, make sure you add a Prompt to the end of the macro (after the Repeat), so you know when the macro finishes.

Now, as you begin to try and figure out what's really going on, you can add Prompts in various places in your macro, to let you know it got that far, and also to let you look at your application and make sure it's where it's supposed to be, automation-wise. And you can always click "Cancel" to stop the loop, at any time.

Eventually you'll narrow down what's going on.

Hope that helps.

Hi Dan,
Thank you so much for the explanation and guidance. That makes total sense to me. I will set that up and test it right now.

best

Felix

I got to thinking that this could cause a problem, since youā€™re debugging UI automation. Every time the Prompt dialog pops up, the ā€œactiveā€ application will change. Now in theory, when you click OK, it will probably return to the application youā€™re trying to automate, but 1) It might not happen instantly and 2) thereā€™s no guarantee whatever control was focused is still focused.

So just be aware of that. Itā€™s conceivable you could need to add a short Pause after a prompt, to allow the application to get its focus back. I suppose itā€™s even possibly youā€™d need to add an Activate Application action, although I doubt it.

Anyway, just some thoughts.

Oh, and let me know how it goes!

1 Like

Wouldn't it be easier to count repetitions with something like this?

No need for a prompt and risking losing focus. Comes with an auditory cue so you can count along, and then visual confirmation.

2 Likes

Good idea, except Iā€™d use a ā€œSpeak Textā€ action, and have it ā€œspeakā€ the counter.

That would be better yes. I am partial to ā€œtinkā€ for testing and notification, such a good sound. I think it blinded me to better options. :slight_smile:

2 Likes

Keyboard Maestro ā€œRepeatā€ Action

Is it possible to adjust or specifically increase the Loop_Max variable from within the list of repeated actions in order to extend the repeat count beyond its initial value (i.e. to allow more time to obtain a desired result)? The actions above fail to achieve an extended delay, but maybe I'm confusing text and numeric fields.

You might want to try the While Conditions are Met Action as an alternative to your Repeat Action:

Then you can update Loop_Max any time in the loop.

2 Likes

JMichaelTX,

Duh!

Thank you

1 Like

Thanks this helped a lot!:star_struck: