Variable Repeat

variables

#1

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.


Repeat a Macro
Looping actions
Ask User and Repeat key/s or key sequences
Use a variable in a „Prompt for user input“ for the number of loops in a "Repeat Macro"
Scroll down selection
Use a variable in a „Prompt for user input“ for the number of loops in a "Repeat Macro"
Triggering a Macro X amount of times?
Stopping an action at the end of the list
#2

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


#3

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.


#4

Hey Felix,

Type your variable name where the number is.

-Chris


#5

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.


#6

Try this:


#7

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.


#8

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?


#9

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)


#10

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


#11

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.


#12

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


#13

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!


#14

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.


#15

Good idea, except I’d use a “Speak Text” action, and have it “speak” the counter.


#16

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:


#17

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.


#18

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.


#19

JMichaelTX,

Duh!

Thank you


#20

Thanks this helped a lot!:star_struck: