Is It Possible to Use a For( ) Loop in Keyboard Maestro?

I would like to go through a list like this... (line separated by carriage return)

  • apple
  • orange
  • banana
  • pear

And convert to...

This is an apple.
This is an orange.
This is a banana.
This is a pear

With the logic being able to assign the right "an" or "a"

I believe I will need to

  1. Loop through the list
  2. Target each fruit individually
  3. Assign the right "an" or "a" with an IF statement
  4. Combine the full line of text into the result variable

I am wondering how do I do step 1 and step 2 inside keyboard maestro? Or is it even possible at all?

Oh wait never mind I found the solution

Using the "For Each" in KM

Yep, "For Each" is one repeat loop that KM offers. The name of that Action caused a lot of us to not find it when we were first learning KM.
For other types of loop Actions, see: Looping Actions.

If you have any other questions, please feel free to post (in a new Topic).


@sunnychu, welcome to Keyboard Maestro (KM) and its Forum.
KM is one of the best Mac automation tools available, its Forum is one of the best and friendliest forums on the Internet. Whenever you reach a tough stumbling block trying to use KM, please feel free to post your question/problem here for help.

You will also find this helpful:
Tip: How Do I Get The Best Answer in the Shortest Time?


Hey Sunny,

I thought I'd have a go at doing this with a regular expression instead of a loop.


Prefix Words Using Proper Grammar v1.00.kmmacros (5.9 KB)

I don't know if this is your actual, real-world use case, but if it is then I'd suggest that you use a grammar checker rather than KM.
For example, it is much more complicated to handle:
"This is a very red apple"

But, just for grins, I did write a macro that addresses your specific request:

It offers the following:

  1. KM Variables at the top where you can set the prefix and suffix for each string.
  2. Uses the RegEx metacharacter to match all characters other than vowels.

Below is just an example written in response to your request. You will need to use as an example and/or change to meet your workflow automation needs.

Please let us know if it meets your needs.


MACRO:   Adding Grammar to Text [Example]

-~~~ VER: 1.0    2020-12-05 ~~~
Requires: KM 8.2.4+   macOS 10.11 (El Capitan)+
(Macro was written & tested using KM 9.0+ on macOS 10.14.5 (Mojave))

DOWNLOAD Macro File:

Adding Grammar to Text [Example].kmmacros
Note: This Macro was uploaded in a DISABLED state. You must enable before it can be triggered.

Example Output





  • Adding Grammar to Text [Example]
    • I recommend using a Grammar Checker instead of KM for this task.


  1. First, make sure you have followed instructions in the Macro Setup below.
  2. Trigger this macro.


  • Carefully review the Release Notes and the Macro Actions
    • Make sure you understand what the Macro will do.
    • You are responsible for running the Macro, not me. ??
      Make These Changes to this Macro
  1. Assign a Trigger to this macro.
  2. Move this macro to a Macro Group that is only Active when you need this Macro.
  3. ENABLE this Macro, and the Macro Group it is in.
    (all shown in the magenta color)
    • SET Source String
    • SET String PREFIX
    • SET String SUFFIX


  1. KM 9.0+ (may work in KM 8.2+ in some cases)
  2. macOS 10.11.6 (El Capitan)+

TAGS: @RegEx @Strings @Example


  • Any Action in magenta color is designed to be changed by end-user