Skip to content

Instantly share code, notes, and snippets.

@mdippery
Last active March 8, 2025 01:46
Show Gist options
  • Save mdippery/56af797f83e3452996668958e0226306 to your computer and use it in GitHub Desktop.
Save mdippery/56af797f83e3452996668958e0226306 to your computer and use it in GitHub Desktop.
Objective-C program to retrieve screen resolution
#import <stdio.h>
#import <AppKit/AppKit.h>
/* Compilation: clang -framework AppKit -o screen-size -arch x86_64 -arch arm64 main.m */
/* The main issue is that when run from the command line, there is no window
* context, so AppKit doesn't know what the active window is, and thus
* doesn't know what the "main screen" is, so I think it always returns the
* first one. Is there a way to get the window that launched the process
* and return the screen it is on?
*/
int main(int argc, char **argv)
{
int ec = 0;
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSSize size = [[NSScreen mainScreen] frame].size;
printf("%ldx%ld\n", (long) size.width, (long) size.height);
quit:
[pool release];
return ec;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment