Quickly Expand The Selection To A Given Letter

After reading this: How Do I Get the Position of a Character in a String using KM Actions? - #3 by JMichaelTX I came up with this macro:



The macro:
Expand selection to user-defined character.kmmacros (7.3 KB)

I have two questions:

  1. How can I replace the red action with an action that avoids the input box, but directly takes any character that can be inserted via the US keyboard?
  2. How can I make this macro repetitive? So first the selection is expanded to the first 'r', then further to the second 'r' in the input box, etc.

For the first question (task) I can perhaps use The “Follow Menu Choice with Return” Macro created by @August Mohr? I’ll have to download and study it, once I’m back at my Mac.

I'd also like to know if it's possible to do that. But as I understand it right now, with KM you don't ask a user for a character of input, rather you let the key be the trigger for a macro. The macro I screenshotted in the post that you linked contains an image of the trigger that you would need to use. If you do that, you can access the value of the keypresses using the %TriggerValue% token. It's a different way of thinking, and may require you to rewrite your code.

A loop requires to know when to stop. When are you planning to stop the repetition you speak of?


@Sleepy made some suggestions for how to improve my macro that may make it faster and simpler and also may help avoid the glitch/error that I described at the end. I'll let you know when I update it, which should be in just a day or two.

Hey @peternlewis,

How tough would it be to add a key-down event to the Pause-Until action?

And then allow the user to collect the key via the %ActionResult% token or a new token?

That would open up a whole new horizon of trigger options...


The answer may depend on whether we're asking about ascii keys or USB key codes. I'm not sure which one is more desirable to have, nor am I sure which one is easier to implement. I'm inclined to choose the latter, but then if we want to know which character was pressed (via ActionResult?), then the result would likely end up being strings like "CTRL-CMD-F10". If we chose ascii keys then the result could be a single character.

Virtually impossible.

Keyboard Maestro cannot detect all keys without either setting hot keys, or them producing key events, neither of which is practical or sufficiently generic.

@Sleepy I've come up with a solution without a loop:


I've posted the macro here: Quickly select up to a given character (repeatable)

1 Like

@peternlewis Nor is it possible to create a hotkey+letter trigger? CMD+/,r for instance?

Or create 26 shortcuts: CMD+/+letter and 26 case conditions?

It may depend what your question means.

  1. If you mean using built-in KM triggers, without a complicated KM macro to make it work, the answer is no.
  2. If you mean could KM be modified to support this: while it seems to go against KM's design philosophy, I'd say the answer is maybe.
  3. If you mean can you write a macro to emulate multi-key triggers, then I think yes. I could probably write a macro to emulate multi-key triggers. But I don't understand your question because CMD is a modifier, not a hotkey-capable key or an ASCII character. I think you would need to clarify by saying what kinds of keys (and how many) you want to be able to use in your multi-key shortcuts. If you tell me the exact, complete set of multi-key shortcuts you want to support, I'll tell you if I can emulate this in a set of macros. I'm pretty good at coming up with solutions to difficult problems like this one. But you probably won't like my solution because it will require work.

Thanks for your clarification! I thought that it would be option 3 and after some thinking I remembered a macro of @cdthomer where several hot keys can be used, and depending on the one that is used, a different action will be taken. (The Aspell macro)

So I tried this:

And it works. I can use this to avoid the Prompt dialogue box in my Quick selection macro.

The disadvantage would be that I have to reserve an awful lot of keyboard shortcuts just for this single macro…

Your code illustrates some of the things we would have to do to get multi-key hotkeys to work. If you're happy with this solution, we're all happy.

Actually, this sort of problem comes up so often, I've been thinking of a fancy new macro that handles all hotkeys including multi-key hotkeys. It would basically be a macro that intercepts all possible hotkeys, and using a dataset that the user prepares (probably a dictionary) it triggers the appropriate macro using a level of indirection. A user would probably set up hotkeys like this:

The main downside to this is that it would be the ONLY macro a user could use to set up a hotkey. If the user tried to set up any other macro to use a hotkey, then the KM palette would pop up, which cancels the automation. Basically I would be replacing KM's ability to process hotkeys with a single macro that emulates it. I'm not sure if people would want this sort of thing.


Okay, what about a means to report what key is down in the following case?


As far as I know there is no available method at present.

This would require quite a bit of set-up by the user when covering many keys, but it would offer a lot of functionality and could be reused once created.


No, there is no reporting on which condition of an “any” caused the condition to succeed. It doesn't really work in general, since for all/none variants there would be no specific matching condition, and for the others it could be one or more than match, and so the results would be indeterminate since they are all tested in parallel.

If you want to know this you have to expand it out to a While Loop and multiple If Then Else conditions.

I was hoping it'd be relatively easy to create a means to avoid that level of complexity. Most user's won't go there.


Hey Hans,

You can read Peter's answer above for one quite complex method.

The other more simple method that I can think of is to create a conflict palette.

So your main macro when activated pops up a conflict palette with as many letter choices as you want.

The letter is returned to the main macro when you press the key, and the macro continues.


1 Like

Hello Chris,

The idea of a conflict palette had indeed popped my mind. However:

  • wouldn't that imply a large and ugly list with at least 26 letters?
  • how to create one without creating 26 macros?

Currently I'm musing over another approach, of which I'm not sure that it's possible:

  • create a trigger macro which only function is to enable and launch the quick expansion macro;
  • add a final action to the quick expansion macro where it disables itself (to free all the hot keys used).

How about that?


I’ve updated my macro and I think it does what you want. When I use it I type shift + command + control + S and that pops up a Pick With List menu. The list is organized, as you have previously suggested, with a unique single letter at the beginning of every line.

Immediately before the PWL menu, I run Activate Macro Group for one action which activates a dedicated group. The group contains one macro, which has 48 triggers for the single action which outputs the trigger value followed by Return.

1 Like

I tried that approach in my earlier version of "Follow Menu Choice With Return". It worked OK, but every once in a while the macro got interrupted, didn't run properly, and so didn't disable itself. That was awkward when then triggers were normal text characters.

My newer version puts FMCWR into a dedicated group that gets activated for one action only. That avoids the problem of how to disable the hotkey set.