Repeat Macro X Number of Times, and for Each Unique Time Type Something Different

Lets say I have a text file like this:

Pro-Q 3

How would I go about to making a macro with a prompt asking how many times the macro should repeat itself.

Lets say I type 6.

For each time the macro repeats itself it should type something different as its action.

First time it should type: Pro-Q 3
2nd: Pro-MB
3rd: CLA-76

It knows what to type because it takes the information from the text file, but I have no idea how to make a macro like this.

I don't think there's any repetition necessary. The easiest way to get a particular line from a text file is a shell script that runs sed. For example

sed -n 10p file.txt

will print the 10th line of the file file.txt. (sed's default behavior is to process and print each line in the file. The -n option tells it not to print by default, and the 10p tells it to print the tenth line.)

So your macro just needs to prompt the user for the line number and then use that variable in a shell script that calls sed. Like this:

Put the full path to the file of interest between single quotes so the script doesn't get confused by spaces or other "special" characters in the file name.

To understand how to use KM variables in shell scripts, look at this page in the wiki.

1 Like

Hey Maximilian,

Perhaps you should tell us more about what you're trying to accomplish...

Without more information I would probably use a Prompt With List action for this task, so I could easily select one or more items of the list in the prompt.


Prompt With List Example v1.00.kmmacros (6.2 KB)
Keyboard Maestro Export

I have a text file with all my favorite plugins in Logic Pro, and I have a separate program called PlugSearch – where I can search for a plugin and add it to my favorites for quick loading.

The list is 50 lines of plugins, so 50 plugins.

Recently this PlugSearch has behaved a little bit buggy for me so I've had to delete all my 50 plugins (carefully ordered) and do it all over again.

I'm thinking I'll do this once and for all and then a script can fix it back up for me if something happens =) repetitive work.

Chris your setup looks like what I need.

Email with any bugs. He's very responsive and you will probably find a more convenient solution than redoing all your favourites.

Keep in mind that the prompt-with-list plug-in automatically sorts the list, and you'll probably want to turn that off.

Make sure to look through the gear menu at the top right of the action – or use its contextual menu – as there are even more options.


How do I make the action "type text by pasting" type one of the lines from the list?

Note that I saved the prompt selection to a variable:

Prompt With List Example v1.01.kmmacros (6.8 KB)
Keyboard Maestro Export

The texts bracketed by percent signs are Text Tokens user manual section.

Between the three of them you have a great deal of power and flexibility.

OK, so I know the original question has changed somewhat -- but here's a couple of ways of "If I have a text file and the user picks a number n, how do I do something with each of the first n lines". There's a couple of ways to do this -- in both examples change the first action's path to a text file to a file on your machine.

Traditional way is to iterate through an array. KM can treat text as an array by using delimiters -- unfortunately your can't use Return/Linefeed as a delimiter so you have to search'n'replace with your own custom delims:

Lines From File -- Array.kmmacros (4.5 KB)


But KM has "Collections" which you can run through with a "For Each" action. That looks like it will always do all of the items in the collection, but you can maintain a counter and exit the "For Each" loop when you've processed the correct number of items:

Lines From File -- For Each.kmmacros (6.2 KB)


Since the discussion has moved on I'll omit my usual rambling explanations :wink: But ask if anything's unclear.

Ok I'll try to understand here, or be more specific.

would it help to have the list of plugins like this:

Screenshot 2022-11-23 at 10.18.45

instead of this:

Screenshot 2022-11-23 at 10.16.23

If we could get the list of plugins to a variable then I need the macro to type %Variable%PluginName%

On first pass its Pro-Q 3 second pass its Pro-MB
third time its CLA-76

The Macro will repeat itself 49 times

Exactly what steps would you do in this task if you were doing it completely manually? You have only mentioned one step - typing a list of items one after another.

Would you type "Pro-Q 3" and then hit return, followed by "Pro-MB" and return? Or is there more to it than that?

It is easy enough to make a Macro that types your list of items one after the other (see below). Maybe you could use this as a start and add in any other steps to the loop, along with the typing. The loop could even execute another Macro on each loop, or invoke a menu item.

Assuming you want to go through the complete list there is no need to specify "49 times" if there are 49 items in the list - but if you did want to limit the loop to a certain number that could be done too.

Without more details of exactly what steps you need to do it is hard to advise.

Anyway, this is how you can type a list of items one after the other and use that loop as the basis of doing more things.

Type list of items, one at a time.kmmacros (5.1 KB)

Click to Show Image of Macro

1 Like

Thanks Zabobon!

this is what my actions would be like

Click at found image
Insert text by typing One

Click at found image,
drag, release

thats one cycle

Click at found image
Insert text by typing Two

Click at found image,
drag, release

and so on and so on.

I tried your macro and it types the whole list at once.
I understand now why you need to know what my other actions are gonna be like!
Could this be done with your macro?

Or... right click at static coordinates, right-arrow, Enter, to add as a favourite.

How so? If you mean that PS would occasionally 'forget' favourites, I reported it a while back and it's been fixed.

It doesn't matter to KM -- you can (nearly!) always transform text to something usable with a "Search and Replace" action. A good rule is to keep the text in the format most useful to you when you look at it next week/month/year.

In this case I would keep it as one plug-in per line -- it will make it much easier for you to maintain when your plug-ins change and removes the chance that there's confusion when someone calls their plug-in "Flange, Crazy Frog Version" (there's very little chance they'll use a linefeed character in a name).

When making a macro, you need a plan. When asking on the Forum, it's helpful to show your plan in a way that people unfamiliar with what you are doing can understand. It also helps you answer questions before they are asked -- for example:

Why 49 times? Is that the number of pig-ins? If so, wouldn't it be better if it repeated "number of plug-ins" times so it didn't matter if you added/removed some? Or is 49 a hard limit on what you can add, so even a list of 100 items should stop at 49?

The plan can be as simple as:

  1. For each item in my list
    a. Open a dialog
    b. Type in the item
    c. Close the dialog

...and you'll see that @Zabobon's macro above does all that except the "Open a dialog..." step.

1 Like

Yes. You would include those steps in the loop. And be a good idea to at least initially add some pauses so, the Macro doesn't trip over itself. You could remove or reduce the pauses between actions later if they are not needed. This kind of thing:

Yeah thanks man :slight_smile:

Yes the purpose of the repeat 49 times is to have my whole list of 49 plugins added to my favorites.
If the list was longer then I would want the macro to repeat longer.
I see now the best way would have been to repeat until complete.

Oh - and initially test your Macro with just one item in the list, until it works as expected. You don't want a Macro repeating something 49 times if it is creating mayhem...

And with that in mind also good idea to first setup a "Cancel All" Macro.

1 Like

For me deleting selection deletes a random one instead of the selected, in turn messing up the whole order of my list.
Two times the list has been empty after restart.

I'm going to report this soon as I can.

Re-add PS Faves.kmmacros (38 KB)

Macro screenshot


thanks! I was fiddling around with Zabobon and got it to a point where it was listing in order just like I want it! I see Neil had the same solution. Hats off to you guys! Much appreciated

1 Like