7
votes

I'm trying to write a Mac OS menu extra application that displays a contextual menu containing the currently active application's menu bar items, when the user presses some hotkey. The displaying of the contextual menu I can do fine, but I can't seem to get the currently active application's menu bar items. At the moment I'm using [[[NSWorkspace sharedWorkspace] runningApplications] filteredArrayUsingPredicate:] to get the active applications' name, but NSRunningApplication seems to contain precious little other information. Is there any way I can get information about application menus from an external application?

UPDATE:

Using the ScriptingBridge framework seems to work fairly well, if you're happy using AppleScript:

    SystemEventsApplication* sevApp = [SBApplication applicationWithBundleIdentifier:@"com.apple.systemevents"];
    SystemEventsProcess* proc = [[sevApp applicationProcesses] objectWithName:appName];

    for (SystemEventsMenuBar* menuBar in proc.menuBars) {
        for (SystemEventsMenuBarItem* menuBaritem in menuBar.menuBarItems) {
            NSLog(@"%@", menuBaritem.name);
        }
    }

will print out a list of menus available from the application's menu bar. Haven't found a way to get the contextual menu, so I won't call this answered just yet...

This was useful too: http://robnapier.net/blog/scripting-bridge-265

1
THANK YOU! I'd love to give you more than a +1, but I wouldn't know how. Compiling /System/Library/CoreServices/System Events.app into a header is probably the strangest thing I've ever done with Xcode but hey, it works! Would you happen to know where the shortcut or keyEquivalent property is hidden in a SystemEventsMenuItem? - epologee

1 Answers

0
votes

You can use AppleScript to simulate clicking a menu item like shown here, but I'm not sure if it's possible to dynamically grab the names of all the menu items, to use that method you need to already have the names hardcoded into the app.