views:

45

answers:

1

In Cocoa, how can I determine which screen holds the menubar when the computer has multiple screens?

Here's what I have so far:

NSArray * screens = [NSScreen screens];
NSScreen * mainScreen = [screens objectAtIndex:0];
if ([screens count] > 1)
{
    for (NSScreen * screen in screens)
    {
        if (/* screen == the screen that holds the menubar */)
        { mainScreen = screen; break; }
    }
}

NSLog(@"the main screen is: %@", mainScreen);

Note: I tried using [NSScreen mainScreen], but that simply returns the screen on which the currently active window resides. It only returns the screen that has the menubar if there are no other windows open in my app.

+5  A: 

From the NSScreen documentation:

The screen containing the menu bar is always the first object (index 0) in the array returned by the screens method.

so your variable mainScreen already contains it.

Joshua Weinberg
Sweet! Thank you
e.James