"Select or Show a Menu Item" selects wrong menu item

I'm using "Select or Show a Menu Item" to run a Service in Finder, but it picks the menu item directly above it around 20-30% of the time.

Menu Selection

My Macro

Hmm, that’s odd. My only guess is that the menu is being updated out from under Keyboard Maestro after it gets the menu and before its executed.

The menu is looked for by name, and has to match the name exactly, and if not it will be reported as not found (which you can easily see happen if you just change the menu item text a little).

So Keyboard Maestro is finding the right menu by name and then telling it to be pressed (using the AXUIElementPerformAction API). So there is not a lot of scope for it selecting the wrong menu item.

But the Services menu is a dynamic menu, and what I suspect is happening is that the menu is built when Keyboard Maestro asks for it, and then while it is processing the menu, it is dynamically updated to add in the other item. And then when the menu is pressed, the system is not keeping the Accessibility menu item that it gave to Keyboard Maestro in synced up with the relocated menu item in the updated menu, and so is pressing the wrong menu item.

The best I can suggest is to add an unused command key for the menu item (like Control-Shift-Option-M or something), and then use Keyboard Maestro to type that keystroke.