Programmatically get screen size in Mac OS X

WrightsCS picture WrightsCS · Feb 13, 2011 · Viewed 36.5k times · Source

I am able to return the screen size using:

- (void) getScreenResolution {

    NSArray *screenArray = [NSScreen screens];
    NSScreen *mainScreen = [NSScreen mainScreen];
    unsigned screenCount = [screenArray count];
    unsigned index  = 0;

    for (index; index < screenCount; index++)
    {
      NSScreen *screen = [screenArray objectAtIndex: index];
      NSRect screenRect = [screen visibleFrame];
      NSString *mString = ((mainScreen == screen) ? @"Main" : @"not-main");

      NSLog(@"Screen #%d (%@) Frame: %@", index, mString, NSStringFromRect(screenRect));
    }
}

Output:

Screen #0 (Main) Frame: {{0, 4}, {1344, 814}}

Is there a way to format {1344, 814} to 1344x814?


Edit:

This works perfectly:

- (NSString*) screenResolution {

    NSRect screenRect;
    NSArray *screenArray = [NSScreen screens];
    unsigned screenCount = [screenArray count];
    unsigned index  = 0;

    for (index; index < screenCount; index++)
    {
        NSScreen *screen = [screenArray objectAtIndex: index];
        screenRect = [screen visibleFrame];
    }

    return [NSString stringWithFormat:@"%.1fx%.1f",screenRect.size.width, screenRect.size.height];
}

Answer

mamady picture mamady · May 19, 2013

Finding the screen size in Mac OS is very simple:

NSRect e = [[NSScreen mainScreen] frame];
H = (int)e.size.height;
W = (int)e.size.width;