How to repeat a type keystroke action multiple times

Hello, I would like to create a macro to type the underscore character multiple, perhaps 100 times (ie create a line) in Pages. I understand how to type a keystroke. It’s the repetition I don’t know how to manage.
thanks very much

You need to use the Repeat action.
Then you can put the other actions in the repeat action.

And if you need then it is possible to ask for user input and then repeat this x number of times.

Repeat text x number of times.

Insert x number of times.kmmacros (2.6 KB)

1 Like

Rather than 100 underscores, have you considered pasting an HTML horizontal rule? I have a KM Named Clipboard, and corresponding macro, just for this purpose.

But if you really want to repeat a character (or a string), then you could use this JavaScript for Automation (JXA) script:

1 Like

There's no way on Earth I'll sit still while 100 characters get typed out (unless I have no other choice).

This macro uses a typed-trigger with a regular expression.

Type:

-
or
–
or
_

And then a number followed by a space.

The string will be expanded to the length of the number and pasted.

This macro can easily be modified to be more general OR more specific.

-Chris


Typed Trigger Repeated #-Times.kmmacros (5.0 KB)

2 Likes

Reading all the comments I can see that my macro could be improved substantial.
But I have to say that I use it mostly for repeating up to 20 items.
And it is normally one character.
Here I do not experience any performance issues.

I can also imagine that seeing a solution using simple and native KM actions could be helpfull for people learning to use KM.

But still thanks for all the optimization tips. They are helpful. :slight_smile:

1 Like

Thank you for your reply.
I am curious to ask : in what context would you want to repeat text multiple times?
I apologize for my badly formulated question: I was proposing a solution (repeat underscore) instead of asking a clear question (I want to draw a line in pages).

it’s all my fault for not asking the question properly. My question should have been flagged as ‘inappropriate’ and below the IQ level of forum members !

1 Like

thank you

thank you very much. Where would I find your clipboard macro ? why is it called clipboard and not ‘draw a line’ macro ?

Sometimes I have to insert x number of 0s to control formatting in Excel.
This is primarily the reason

1 Like

thank you

Probably the best way to create a horizontal line/rule in Apple Pages, is to use Pages' native tools.

See Pages for Mac: Add borders and rules (lines)

Also, I have just posted a macro that should work in most rich text apps (but NOT Pages and MS Word):

1 Like

Agreed.

However, I would suggest first building the string, and then pasting it, rather than pasting N times, or even worse, typing N times.

For example:

2 Likes

thank you. It’s over my head for the moment, but I am working hard to improve.

thank you.

Hey Ronald,

Give yourself time. Keyboard Maestro is a pretty complex beastie.  :smile:

If you want to insert a line of fixed length then simply set the clipboard to the text and then paste it.

-Chris


Generic-Test 01.kmmacros (2.0 KB)

1 Like

great. thank you.

I have an interesting variation of this where I need N left arrows. I can't think of any way other than to Type a Character inside of a repeat statement. It's awfully tedious to watch when the repetition gets up towards 50 or 100 times. Is there a better way to get N characters that move the cursor but don't type any "real" characters?

Does this help?

The only way to improve it (baring the application being especially scriptable) is to move by words if that can be appropriate.

Changing the Simulate Normal Keystroke to 0 is unlikely to do anything except lose some of those left arrows and make the process unreliable.

1 Like