Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically get screen size in Mac OS X

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]; } 
like image 393
WrightsCS Avatar asked Feb 13 '11 05:02

WrightsCS


People also ask

How do I find the screen size of my Mac?

On your Mac, choose Apple menu > System Preferences, click Displays , then click Display Settings. Select your display in the sidebar, then do one of the following, depending on your display: Click the Scaled pop-up menu, then choose a scaled resolution for the display.

How do I find the dimensions of my screen?

The size of a desktop computer monitor is determined by physically measuring the screen. Using a measuring tape, start at the top-left corner and pull it diagonally to the bottom-right corner. Be sure to only measure the screen; do not include the bezel (the plastic edge) around the screen.


1 Answers

In Swift 4.0 you can get the screen size of the main screen:

if let screen = NSScreen.main {     let rect = screen.frame     let height = rect.size.height     let width = rect.size.width } 

If you look for the size of the screen with a particular existing window you can get it with:

var window: NSWindow = ... //The Window laying on the desired screen var screen = window.screen! var rect = screen.frame var height = rect.size.height var width = rect.size.width 
like image 77
j.s.com Avatar answered Oct 03 '22 12:10

j.s.com