Search code examples
objective-cmacoscocoaxcode4

Get MainMenu of other Running Application other then the Main Application


I know the question is kinda confusing, but let me explain...

I want to be able to get the Menu Item Titles of all running applications, not just the main application. I know how to get the main application menu items by using the code below:

NSMenu *mainMenu = [[NSApplication sharedApplication] mainMenu];

for (NSMenuItem *item in [mainMenu itemArray]){
    NSLog(@"%@", [item title]);
}

And I also know how to get a list of all running applications using the code below:

[[NSWorkspace sharedWorkspace] runningApplications]

Now I want to be able to be able to get the mainMenu of a certain application in the sharedWorkspace.

Is this possible.. If so, please share :) Thanks!


Solution

  • You need to use the Accessibility API to get access to other application's GUI elements. Keep in mind that you (or your app's customers) will need to turn on the "Enable access for assistive devices" in the Universal Access system preference.

    To get started see The Accessibility Hierarchy.