How to Loop actions inserting different set of 6 character text each time?

I have a macro loop I want to run but I need to insert a set of text characters into a text box which is selected in the loop each time. The the sets of characters are in a text file separated by a space between each set. I understand text can be inserted through the clipboard command, but how would I complete the rest of this? Thank you for any help!


Hey Simon,

Here's the general method.

  • Read the file into a variable.
  • Break it into lines with a regular expression.
  • Loop through the lines.

You'll have to copy to the clipboard instead of displaying the text.

You may need a slight pause before pasting.

The regular expression:

[ ] == A space enclosed brackets (as a set) for clarity.
\n  == Newline metacharacter


Generic-Test 01.kmmacros (3.3 KB)

1 Like

Looks good thank you, I will try that very soon!

I tried that ccstone and it works perfectly thank you! Although I need a single character string on each loop on the macro inserted into the text box going sequentially. So if there is 20 different text character strings in a file separated by a space, on the first loop the first text character string is inserted and on the second loop the second text character string is inserted etc.

From a programming point of view I have thought about making a variable with the number 0 in it and each loop the number is increased by 1 and this variable is used to select which item in an array is selected and put into a new variable which is put in the text box. How would I go about this?

Thank you!

Hey Simon,

That’s exactly how my macro works now.

Instead of the Display Text action you would emplace your job sequence.

The For Each action in the macro iterates over the lines in the variable one by one.

So, you have what you need (unless I’m failing to understand something).


I put the rest of my job sequence into the for each loop last night and ran the macro multiple (multiple) times and it started having problems. I put a one second delay at the start of the execution of the instructions on each loop and I found the text box that the character set is inserted into likes to immediately jump to a new text box without finishing the job sequence and will put a new character into the new text box (no matter what that text box is). It is an action very similar to a tab key press but I assure that is not in the job sequence although a return key press is the first action within the loop. I am aware sometimes a pause is required or the macro will try to carry out an action before the last one is finished, I am unsure if this could be the problem. Is no pauses the route of many problems?

I have tried to be as clear as possible sorry if that is difficult to read! Does anyone know what could be causing tab key problem? All ideas are very appreciated! Thank you

Hey Simon,

My first thought is tabs embedded in the source text, but if you’re pasting the text instead of typing it that shouldn’t be an issue.


Post your macro to the forum, so we can see what you’re doing.

If you’re unclear on how to do that see this:


Hi Chris here it is.

I added pasting on another macro which did a simplified sequence and it was more accurate. Adding it on this, the entire file is pasted and not a single six digit character set.

When I select the text box it is meant to paste but the mouse movements are not recorded properly either so it pastes randomly on the screen. I have recorded and rerecorded and it still does not work, yet on a small macro mouse movements work fine.

The tabbing has seemed to have stopped after using pasting. There are three pasting options which is best for my purpose?

The mouse movements and pasting the entire file instead of the single character set are my main problems I think.


What you are missing is some Pause actions to allow the actions to complete before continuing on. For some actions (eg Activate Application), Keyboard Maestro can wait until the action is performed, but for others, there is no way for Keyboard Maestro to know the consequences and how long they will take (eg Click actions, or menu actions that cause animations or other time consuming behaviour).

In this case, the prime locations that likely need a Pause would be after the click (to allow whatever consequences the click has to happen); after the menu selection to allow the dialog to appear; after the first activate application (to ensure Illustrator is ready to accept the keystroke) (the second activate adobe illustrator seems redundant); after the click (as before); at the end of the loop to ensure the loop does not repeat too fast.

Also, you activate Illustrator, but never anything else, so its not clear what application you expect to be in at the start of the loop.

Thats great thank you so much I got it working with pauses, but now after the sixth loop it starts inserting the same text repeatedly into the text box and the mac makes a beep noise like it can’t do something. I checked the file it is reading from and there are no errors in that, I am very confused.

I have managed to work that problem out! Is there a a way so I can run a macro on a program in the background and still use the computer to do other things? Currently because the mouse and keyboard is used by my macro it renders the computer useless until its finished its task!


Ok I’ve worked out how to make a macro group available to only one application but still when I try to swap to my browser I am pulling my mouse away from the macro. Why is this?

I doubt that is possible. The User Interface, by definition, is pretty much one thing at a time. As long as your macro uses any part of the UI, then you will need to wait until it completes.

The only other option I see is a script, but it also would NOT be able to use any UI commands. IOW, the script would have to interact with the app through its object model, not through its UI.

Good luck!

So what is the option I talk about above good for? The one which makes a macro available only for one application?

IMO, the main purpose of that is to allow the same hotkey (or other trigger) to be used in multiple applications, but actually trigger the macro specific for the app.

Here’s an example:

I use many different apps that support highlighting (background color) of text, but each of them do it differently, and have different app shortcuts (or none at all) to apply the highlight.
Using KM, I have one hotkey, CMD-CTRL-H, that applies highlight in every app I use, but with a different macro for each.

Make sense?

I see but it means my laptop is pretty much rendered useless until my macro is finished

That’s a funny way of thinking about it. :astonished:

The purpose of any macro is to automate what you would otherwise be doing manually.
Macros can, and do, save us many hours of time, at least on a weekly basis, maybe more.

So, yeah, you have to wait for the macro to complete. But really, how long is that, one minute? One minute that probably saves your 10-15 minutes.

It has been long established that, by far, most of the time the computer is waiting on the user to do something. Very rarely are we waiting on the computer.

If it really bugs you, bring up something you can read before you start the macro.
Or bring up something on your tablet or phone to read.

Hey Simon,

That’s right.

When you’re driving the UI with automation software you can’t mess with the UI.

Now then. I see you’re using Illustrator CS6. It is quite scriptable:

A scripted solution can probably run in the background.

I haven’t scripted Illustrator, so I cannot advise you what is and is not possible with it.


yes it is very scriptable, but its objects don’t access what I need unfortunately. I am wondering if I partition my hard drive with two operating systems (whilst extreme) would get round this?

No. You can run only one OS partition at a time.