MacOS – Create hotkey to open a menubar app

macosmenu bar

Is it possible to create a hotkey (or other trigger) that opens a menubar app? I don't mean launch. I mean, cause the menubar app to display its contents.

Let's say, for example, I wanted to create a hotkey to open a menubar weather app. Can this be done?

The app I'm trying to open is open source on Github, so I'm open to coding the trigger into the app if need be, but I have no clue how to do that, which means I'd prefer a simpler solution.

I'm using Mountain Lion.

Best Answer

You can assign a shortcut to a script like this:

ignoring application responses
    tell application "System Events" to tell process "QuickHue"
        click menu bar item 1 of menu bar 1
    end tell
end ignoring

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 first two lines are needed when System Events is not already running. System Events supports sudden termination, so it should be safe to even send it a KILL signal.