7

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: https://robnapier.net/scripting-bridge

Dimitar Nestorov
  • 2,411
  • 24
  • 29
alaroldai
  • 336
  • 2
  • 11
  • **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 Sep 03 '12 at 22:30

1 Answers1

0

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.

stonesam92
  • 4,307
  • 2
  • 19
  • 24
  • Hey, thanks for the applescript tip! I did some digging around, found [this article](http://hints.macworld.com/article.php?story=20111208191312748) which has an applescript way of doing pretty much exactly what I want... I'll keep looking for an Objective-c method though. Bundling an applescript into my app could get very messy. – alaroldai Jul 18 '12 at 00:52
  • You can use appleScript from cocoa applications without too much trouble, see [NSAppleScript](https://developer.apple.com/library/mac/#documentation/Cocoa/Reference/Foundation/Classes/NSAppleScript_Class/Reference/Reference.html). Obviously it's not as nice as straight objc but i don't know if you'll find a simple built-in solution in pure objc. Good luck though – stonesam92 Jul 18 '12 at 11:43