How to Trigger a Menu Item Whose Name Changes?

Howdy folks. Ever since I got my Stream Deck I've been adding a lot of shortcuts on it for my most commonly used Keyboard Maestro actions. For instance, I have 10 or so buttons that import my most frequently used actions, and other buttons that trigger certain menu items, like Hide/Show Disabled Macro Groups, Sort Macros by Name, Group Selected Actions and so forth.

However, I am trying to figure out how to trigger the menu items "Enable/Disable Actions(s)". But I'm running into the issue that this particular menu item dynamically changes depending on how many actions are currently selected. If only one is selected, the menu item is titled "Disable Action", but if 2 are selected, it's titled "Disable 2 Actions". I would like to create a macro (that would then be assigned to a button on my Stream Deck) that will trigger that menu item regardless of its title.

I THINK I can do this using RegEX, but my RegEX knowledge is way too basic to know what string to use or if it's even possible. So can somebody either point me in the right direction with RegEX, or perhaps it's possible using an AppleScript to point to the menu item by it's hierarchal position?

I found this topic that indicates I can separate potential menu items by using the RegEX meta character for alternations "|" between them, and mentions that sometimes the caret "^" is also needed... but I can't get the macro to work using that method. Attached is a screenshot of the macro.

Or any other pointers are greatly appreciated. Thanks all!

EDIT: I seem to have gotten it working...I had an extra | at the end (I have up to Disable 10 Actions in the string) that for whatever reason prevented the macro from working. I removed it and now it does work, albeit a little slow. However, it only works when all selected actions have the same status, that is enabled or disabled. If I have a selection of some that are enabled along with others that are disabled it does not enable nor disable them even though in those instances the "Enable X Actions" menu item exists and is enabled.

Hi Chris,
The KM wiki page action:Select or Show a Menu Item [Keyboard Maestro Wiki] explains the use of both the pipe character “|” and regex in this action.

To use a regex you’d need to put a caret “^” at the start of the name and then follow that with the actual regex.

Instead of a regex, you can specify alternative names just as you have done using the pipe character.

The advantage of your approach is that it is easy to understand but it will get unwieldy if you have a lot of alternatives to accommodate. The opposite is true of using a regex.

Thanks for the reply! That must be why when I put the carat in front of the string with the "|" character it stopped working.

Like I mentioned, I got it to mostly work (with the exception of when there is a mixture of enabled and disabled actions in the selection), but it's rather slow. I'm gonna look into how to use RegEX to accomplish the same, but if you have any suggestions I'm all ears.

Chris, I've put together an example macro for you to try that I think does what you're looking for. Let me know how you get on with it.

Test regex menus.kmmacros (4.3 KB)

image

Thanks so much! It seems to work intermittently for me, but I'm unable to figure out why. It disables just fine, but doesn't seem to enable actions. For whatever reason, even when the selected actions are already disabled, it still reads the "Disable" menu item as both existing and being enabled. So it never triggers the enable action.

Well, I’m glad it does the same for you as it does for me!

It was seemingly working, but now restarting the KM engine seems to get it to work again - albeit temporarily.

I’m just finishing now until tomorrow, so I’ll look at it again then unless you manage to fix it in the meantime!

I’ll keep tinkering with it but keep me posted if you come up with something else please :grin:

I certainly will!

This was bugging me so I had to keep looking at it.

Well, I've now got a working macro - at least testing it here gives the right results, but you've got to be the final judge of that!

One thing: if you have several actions selected and some are enabled and some are disabled, then this macro will enable them all because that’s what the KM Actions menu offers.

Also, to work properly, the Failure Aborts Macro and Notify on Failure options are unset on both the Select actions (in the gear menu)!

Here you go:

Test regex menus.kmmacros (4.0 KB)

KM 0 2021-08-23_00-11-14

I am definitely going to bed now, so let me know how you get on.

Cheers!

1 Like

Awesome! I just imported that and so far it works flawlessly, thanks so much! I'll let you know if I run into any issues but I just tested it out in a variety of circumstances and it worked as expected every time. You the man!

1 Like