Skip to content

Instantly share code, notes, and snippets.

@theevilbit
Last active April 19, 2024 07:55
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theevilbit/d2580c5d12224f3291dfbb74fd6af29b to your computer and use it in GitHub Desktop.
Save theevilbit/d2580c5d12224f3291dfbb74fd6af29b to your computer and use it in GitHub Desktop.
Make a screenshot on macOS using Objective-C
/*
Compile:
gcc -framework Foundation -framework AppKit screenshot.m -o screenshot
*/
#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>
int main(void) {
//capture window
CGImageRef screenshot = CGWindowListCreateImage(CGRectInfinite, kCGWindowListOptionOnScreenOnly, kCGNullWindowID, kCGWindowImageDefault);
//create bitmap
NSBitmapImageRep *bitmap = [[NSBitmapImageRep alloc] initWithCGImage:screenshot];
//create PNG from bitmap
NSData *data = [bitmap representationUsingType:NSBitmapImageFileTypePNG properties:NULL];
//save file
[data writeToFile: @"screenshot.png" atomically: NO];
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment