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,
50,
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 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.)