AppleScript Click KM Status Bar Menu Item Too Slow

Hello,
I'm trying to use AppleScript to click the KM status bar menu item.
I don't know how to click the third level menu item yet. But with the code I already have, there is a problem. The first click is quick, but the 2nd click happens quite a few seconds later. I don't know why.

Here is the code:

tell application "System Events" to tell process "Keyboard Maestro Engine"
	click menu bar item 1 of menu bar 2
	tell menu bar item 1 of menu bar 2
		click menu item "Global Macro Group" of menu 1
	end tell
end tell

It's broken.

A certain class of menu widgets exhibit this behavior.

AppleScript is waiting for them to return an event after being clicked (AXPressed), and they don't. Instead they time-out after 6-7 seconds.

I don't know the in-depth details. I only know the problem from the outside – having observed it for years now.

Perhaps Mark Alldritt or Shane Stanley on the Script Debugger Forum would know.

Feel free to file a bug report with Peter.

But give up on trying to make it work.

-Chris

Thanks, Chris.
@peternlewis, do you have any thoughts about it?

I was trying to make it to work for a specific need.
I have customized the palette in the macro group settings and would like to trigger the palette with a script. I don't know any other way except simulating click on status menu entries.

Or, a better solution, is it possible to add a script trigger here?

image

You can use a Show Palette action in your script

image

This does not work for me, because I want a customized palette (just for this palette only):

Why not just give it a hotkey?

If you need to script it you could:

  • Set a hotkey for the palette activation.
  • Create another macro that presses that hotkey.
    • Keyboard Maestro doesn't like stepping on its own hotkeys.
    • Use System Events to press the hotkey.

Execute an AppleScript.kmactions (713 B)

-Chris

I want to avoid hotkey, because i might forget it and when it conflicts with other apps, its hard to debug...

How were you activating the AppleScript to activate the Keyboard Maestro Status Menu?

You could set the hotkey of macro 2 to something really esoteric, and use AppleScript to run that macro.

As far as I know that's the only possible way to use an AppleScript to activate the macro group palette with the custom options you've chosen for it.

-Chris

I was using touchbar trigger in bettertouchtool.

Well, i was trying the status menu bar click. If it works without delay, I won’t need a hotkey.

That’s also why I was asking if a script trigger could be added:

I've found a solution!

I saw it before but did not make it to work. I thought it wasn't the solution I was looking for. But after some attempts, it's working now.
I got the idea from here (including the status menu bar click):

There is a bug where clicking some status menu items causes scripts to be blocked for about 5 seconds. One workaround is to terminate System Events after the click command:

launch application "System Events"
delay 0.2
ignoring application responses
   tell application "System Events" to tell process "Time Tracker"
       click menu bar item 1 of menu bar 2
   end tell
end ignoring
do shell script "killall System\\ Events"
delay 0.1
tell application "System Events" to tell process "Time Tracker"
   tell menu bar item 1 of menu bar 2
       click menu item 2 of menu 1
   end tell
end tell

The only adjustment I needed to make is to switch the delay 0.1 and do shell script ... from

-- this will not work.
do shell script "killall 'System Events'"
delay 0.1

To

-- this works.
delay 0.1
do shell script "killall 'System Events'"

Here is the complete AppleScript code:

ignoring application responses
	tell application "System Events"
		tell process "Keyboard Maestro Engine"
			click menu bar item 1 of menu bar 2
		end tell
	end tell
end ignoring
delay 0.1
do shell script "killall 'System Events'"
tell application "System Events" to tell process "Keyboard Maestro Engine"
	tell menu bar item 1 of menu bar 2
		click menu item "Text Manipulation [Macro Group]" of menu 1
	end tell
end tell

Also, because I don't know how to simulate clicking the status menu bar item at the second level, I can only to click the status menu bar item at the first level, I need to set the items either "By Group" or "Alphabetically".

image

Hey @martin,

This works, but it's a nasty hack and slow – so I don't like to use it.

Try this.

-Chris


Open Keyboard Maestro Status Menu and Select an Item if Desired v1.00.kmmacros (8.5 KB)

1 Like

Thanks, Chris.
It works great! Mine was not only too slow, it sometimes did not click the correct menu item when the cursor was inside the would-dropdown-menu area. Your macro does not have this issue.

1 Like