MACRO: “Follow Menu Choice with Return” (v0.5)

How to “immediately” choose a "Prompt With List" menu option

[UPDATE v0.5] I initially published my v0.2 version of the macro here. Other people, particularly @Sleepy, made suggestions, some of which I incorporated. So I have updated this long description of the macro and how I built it with notes about the updates. The conversation that lead to those updates thus follows this post in this thread and time is seemingly warped. (See the movie "Predestination" for more warping of cause and effect.)

Just about a month ago, mid-January 2022, @ALYB asked if there was a way to avoid having to press Return to select an entry when using a "Prompt With List" menu.

The consensus of the responses was either no, it couldn't be done or was way more trouble than it would be worth.

I'm very glad that I didn't see that thread earlier or I might not have gone ahead and done what @ALYB was asking for.

The path I took is fairly simple.

I created a macro that has a separate trigger for each of the 48 individual keys that I might use as a menu choice in a "Prompt With List" (PWL) action. The macro spits out the keyboard letter that triggered it, followed by a Return, so I don’t have to type the Return manually.

The v0.2 version of the macro disables itself after it does this, so that single characters are not reinterpreted elsewhere. The macro that initiates the PWL action enables this macro and all of its triggers right before it calls the PWL.

[UPDATE v0.5] In the v0.5 version, the macro is in a separate group and gets activated from the calling macro using the "Activate Menu Group" action with the "Activate for one action" option, immediately before the Prompt With List action displays its menu. Thanks to @Sleepy for this idea.

The “Follow Menu Choice with Return” macro

I could have generated this list of 48 different triggers for the macro by hand. But instead I used Dan’s “[KMET] Edit Selected Objects as JSON Text” macro to generate a template and used the VIM editor to produced the full list, as JSON, which I then reloaded back into KBM.

I made up a test macro, “Follow Menu Choice with Return” and added just a few single character triggers, just to be able to see the form that each trigger would take. I then used Dan’s “[KMET] Edit Selected Objects as JSON Text” macro to get the JSON code for that initial version into an editor so that I could repeatedly copy the block of code for the triggers.

I’m not facile with Dan’s default editor, BBEdit, I’m much better with VIM, but when I tried placing VIM in Dan’s macro as the editor app, it didn’t work. I don’t think VIM is integrated with AppleScript the way BBEdit is. So I let KMET put the code in BBEdit and then I copied the whole thing with ⌘A, ⌘C and pasted it into an empty VIM buffer.

Inside the short JSON version of the macro, as exported by Dan's KMET macro, the triggers look like this:

		"Triggers": [
				"FireType": "Pressed",
				"KeyCode": 0,
				"Modifiers": 0,
				"MacroTriggerType": "HotKey"
				"FireType": "Pressed",
				"KeyCode": 1,
				"Modifiers": 0,
				"MacroTriggerType": "HotKey"

I found a useful reference for the keyboard keycodes at Complete list of AppleScript key codes.

As a first approximation, I generated the complete list from keycodes 0 to 50. When I pasted that new macro back into KM using Dan's "[KMET] Paste Objects into KM" macro, with that initial list of triggers, I found that KeyCode 10 is the Section Sign, 36 is Return, and 48 is Tab, none of which could be entered as a menu choice in response to the Prompt WIth List action, so I deleted those from the list. Here's the resulting list, with the three deletions noted:

 0,  1,  2,  3 , 4,  5 , 6,  7,  8,  9,
     11, 12, 13, 14, 15, 16, 17, 18, 19,
 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,
 30, 31, 32, 33, 34, 35,     37, 38, 39,
 40, 41, 42, 43, 44, 45, 46, 47,     49,

 10 - Section Sign
 36 - Return
 48 - Tab

Initially I removed Space too, but I put it back when I decided that in my larger macro, the one that uses PWL for switching between named desktops, I wanted to use Space for "the previous desktop". That turned out to not be as simple as I had hoped, but it was doable. More on that later.

Earlier Versions of the Macro

My first version (unpublished v0.1) had each trigger defined as a "long press". I thought that I might not need to enable and disable the macro that way, but there were two problems for me with the long press. One was comfort and intuitive use, I just didn't like the feel of having to do a long press on the menu choice key. Sometimes it wasn't long enough and I got the keystroke and not the Return, and then doing it immediately again doubled the keystroke, which was not a menu match. The other was that for some characters, e.g., "o", a long press pops up a menu of the accented forms of the character. That also got in the way of the menu choice being quick and easy to use.

Using a regular keypress and doing the enabling and disabling of the macro seemed to work well, except that sometimes the macro got enabled and not disabled, which made typing regular text difficult — every single character typed got followed automatically by a Return, until I could kill the macro.

[UPDATE v0.5] In the new version, instead of enabling the macro right before Prompt With List and having the macro disable itself, I use "Activate Macro Group" for one action, as suggested by @Sleepy. In the new version, the macro which adds the Return is always enabled, but it is inside a separate group, the "Menu Key as Single Action" group, which is enabled but not active.

[UPDATE v0.5] Here's the updated, v0.5 version of the "Menu Key as Single Action" group, containing "Follow Menu Choice with Return". (The v0.2 upload was just the macro, without the containing group.)

Menu Key as Single Action group v0.5.kmmacros (13.4 KB)

[UPDATE v0.5] When you install this in the KM groups, it should look like this:

Figure 1. Details of of the "Menu Key as Single Action" group.

It should be enabled and available where you want it, but it should not be activated by anything.

[UPDATE v0.5] Here is an example of how to activate the group right before displaying the Prompt With List menu. (In v0.2 the action here was to enable the macro.) The action here is in the Keyboard Maestro action category.

Figure 2. Adding the "Activate Menu Group for One
Action" action.

Figure 3. Locating the "Activate Macro Group" action before Prompt
With List.

Using Space as a menu choice

I mentioned above that there was a slight problem with using the Space key as a menu choice with a Prompt With List menu. The glitch is that, if the space key is the trigger, the token TriggerValue is not a single space character, instead it is the string "Space".

I could have created logic to handle that as a special case, but I opted for a simpler solution. In my usage each desktop workspace has a name and my PWL menu is a list of those names. The PWL action chooses which desktop I want to work in and the returned value is used to specify which desktop workspace I want to switch to. I also have "⤺ Previous Desktop" in the list as a separate entry. Each PWL entry starts with a single letter that is the meniu choice, except for that one. To fix the Space key glitch, I just made that PWL entry read "⤺ Previous Desktop [space]".

When the PWL menu is displayed and I press the spacebar, the macro gets a %TriggerValue% of "Space" and the \l operator converts that to lower case, "space". The macro then spits out those five lowercase letters and the Return so I briefly see the string "space" entered at the top of the menu. It matches the entry I want, the Return selects it, and the desktop-switching macro runs the AppleScript code to pop me over to my previous desktop. Use the same technique for any menu option that makes sense as the Space choice in your menu.

One risk of using "Follow Menu Choice with Return" (fixed in v0.5)

Because of how the original scheme used enabling and disabling the macro for the "duration" of the PWL menu, if you were to leave that menu by other means, such as pressing ESC, changing desktops, invoking another macro with a hotkey, etc., you could leave yourself in the state where every letter you type gets followed by a Return and space characters get replaced by "space". That could be annoying, at the very least.

[UPDATE v0.5] The newer version does not rely on separate enable and disable actions. Instead the "Follow Menu Key with Return" macro is in a separate group, "Menu Key as Single Action", that is initially enabled but not activated (Figure 1). The macro group is activated right before the Prompt With List action is run, using "Activate Macro Group for One Action" (Figure 3). If you accidentally deactivate the group before making the menu choice, the only problem is that you don't get a Return typed automatically for you. There are no leftover effects on anything else you might be doing.

Future enhancements

I had originally created this for a single purpose, for one particular macro, to avoid having to define over 20 triggers that would always be active. I simply couldn't find an "intuitive" set that large that didn't step on hotkeys in active applications, the OS, or something.

[As a side note, one of my inspirations was how Jim S has created a set of macros in MACROS: Desktop Spaces • Macros for Navigation and Window Management, v1.1 (Superseded) which allow for 16 different desktop workspaces and have separate hotkeys for each, plus another set of hotkeys to move the active window to any of those workspaces. That uses up a lot of possible hotkeys and when I tried using it, I ran into the problem of stepping on other hotkeys that I needed, or didn't want to accidentally activate.]

With the “Follow Menu Choice with Return” installed in the "Menu Key as Single Action" group, it's now available for me to use in any future macros where I want to have a hotkey pop up a menu that is then selected from by a single keypress. When I start using it in other ways, I may need to update it. If you have ideas for how you would like to use it that may require an update, please let me know. (I make no promises other than to be interested.)

1 Like

I have a few questions. I hope you don't mind.

Why did you place 0.5 seconds pauses and the Type Return key as separate actions? And did you try placing the Return Key inside the Insert Text action? Was there a problem?

Did you consider using a single trigger like the following?... (note that it uses the regex match and that you can get the value of the match using a token.)

Did you consider the action "Enable Macro Group for one action" which (I think, but I'm not sure) would save you from having to use the "Disable Macro" action at the end of your macro? This might solve the problems in your last two paragraphs, but I'm not sure.

Thanks @Sleepy.
No, I don't mind at all. That's what I hoped I was inviting.

To answer your questions:

0.5 second pauses? I thought it wasn't working well with no pause at all, this seemed to help, I don't remember the symptoms, I recall trying 0.2 sec pauses and not being comfortable with the results, but I don't remember why. It could just be that I liked the slower rhythm of execution, but I'm not really sure. Maybe in conjunction with your other suggestions, they won't be necessary. I'll see what happens.

No, I didn't try including the Return with the other text. I just didn't think about it that way, and your idea makes a lot of sense.

I have never used a RegEx trigger. I didn't even consider it. It's a really good idea and would simplify the macro a LOT, one trigger definition instead of 48. Do you know how that single-character regex expression works in matching Tab, Space, Return, etc.?

I was not aware of "Enable Macro Group for one action" until just a few minutes ago, when I was looking at a post about Key Chords. I immediately thought that such a setting could be used in this macro. Then I read your note. I agree, it could very likely solve the issue of disabling this macro.

So, as a "work in progress", I'm numbering this version as V0.2. V0.1 is described variously above as "Initially" and "At first" but it was not shared on the forum. I'll see how your suggestions work and update the macro as appropriate.

Thanks for the ideas!

If my memory is correct, the regex trigger only works with "normal characters" not things like Tab or Return. So you can reject it for that reason, or you could simply add a handful of hotkeys for those special keys and handle them a little differently in your code. I've done this before.

That is actually my preferred behavior. Anything else is a bit less than intuitive. How does Space work, if you recall at all?

I'm only 90% sure, but space should work just fine.

Thanks for all the suggestions, @Sleepy.

You were right, the pauses were not needed and I could add %Return% to the \l%TriggerValue% as a single Insert Text action. That means that after the list of 48 trigger values, there is only that single Insert Text.

(I have not been able to get a RegEx trigger specified because that only seems to work in the context of a text entry field which is not the case when a Prompt With List list is displayed.)


Your idea of using Activate Macro Group was perfect. As you suggested, it completely eliminates the glitch of accidentally not disabling the macro for unexpected reasons.

The key to making it work is putting Follow Menu Key with Return into its own group that is enabled but not activated. Follow Menu Key with Return is enabled all the time, the group activation controls whether or not it intercepts keyboard key presses as hotkeys, so I've deleted the self-disabling action from the end of it.


Here's how I use it right before the Prompt With List action. It gets activated only for one action.


I'll get the uploaded macro and the previous instructions updated shortly.

Thanks again.

1 Like

You're welcome.

I see you used the string "%Return%" which works perfectly, and works even better than my idea of just entering the Return key. I say "better" because you can actually see the Return key the way you are doing it. So from now on, I'm going to adopt that technique in my macros.

Macro Updated to v0.5, 2022-02-21

All update notes have been incorporated into the first post of this thread, updated comments, screen shots, and kmmacros file.

Hi @August. You might know this already, but with the second generation of my Desktop Spaces macros, you can set change any of the hotkeys. See: Desktop Spaces • Macros to Improve Navigation and Window Management

1 Like

Usage Note

I discovered that if I have more than one entry in the List that start with the same letter (in my case it was "Music" and "Memories"), this system will send a Return immediately following the "M" that I type, and so it automatically selected the first entry and I didn't ever get to see that there were two possibilities.

I don't know how to fix that, or whether or not it is worth the effort. Simplest is to pick a different key to use to select the entry. There are 48 choices.

1 Like