Question

---edit---

there is an application called Keycue that performs this function.

---/edit---

---edit edit---

this is a duplicate. Get a list of all shortcuts from another application

---/edit edit---

I am struggling to write an application that takes the frontmost application's menu bar (i.e. If Safari is open, safari menu, if XCode is open, Xcode menu), and parses the shortcuts from it.

Things I have thus far tried, and failed with:

1: Spent a week learning applescript. Toyed with "System Events" to get menu bar, however there is no information I can glean there that will give me shortcut codes.

2: Considered trying to KVO, with NSWorkspace. Tried to get NSRunningApplication, but there is only a ownsMenuBar property, which is a BOOL, not an NSMenu.

3: Tried to get an NSApplication from NSWorkspace, NSBundle, and NSRunningApplication. All to no avail.

4: Tried to get NSMenu from applescript (No success.)

I think the next thing I would like to try is searching for the NSRunningApplication with a YES for ownsMenuBar, and then try to grab the corresponding NSApplication from.. somewhere. no idea where yet though.

So any suggestions??

Was it helpful?

Solution

Try:

tell application "System Events"
    set frontProcess to name of first process whose frontmost = true
    tell process frontProcess
        get every menu item of menu 1 of menu bar item 2 of menu bar 1
    end tell
end tell

EDIT
Once you have this list you can parse each menu item's attributes:

tell application "System Events"
    set frontProcess to name of first process whose frontmost = true
    tell process frontProcess
        set myMenuItems to get every menu item of menu 1 of menu bar item 2 of menu bar 1
        set myList to {}
        repeat with aMenuItem in myMenuItems
            set end of myList to aMenuItem's name
            set end of myList to value of aMenuItem's attribute "AXMenuItemCmdChar"
            set end of myList to value of aMenuItem's attribute "AXMenuItemCmdModifiers"
        end repeat
    end tell
end tell
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top