Using this question and answer as a starting point, this is a fully functional command line program. You also need to link to the Cocoa Framework.
#import <Foundation/Foundation.h> #import <Cocoa/Cocoa.h> int main(int argc, const char * argv[]) { @autoreleasepool { // Grab the current mouse location. NSPoint mouseLoc = [NSEvent mouseLocation]; // Grab the display for said mouse location. uint32_t count = 0; CGDirectDisplayID displayForPoint; if (CGGetDisplaysWithPoint(NSPointToCGPoint(mouseLoc), 1, &displayForPoint, &count) != kCGErrorSuccess) { NSLog(@"Oops."); return 0; } // Grab the color on said display at said mouse location. CGImageRef image = CGDisplayCreateImageForRect(displayForPoint, CGRectMake(mouseLoc.x, mouseLoc.y, 1, 1)); NSBitmapImageRep* bitmap = [[NSBitmapImageRep alloc] initWithCGImage:image]; CGImageRelease(image); NSColor* color = [bitmap colorAtX:0 y:0]; NSLog(@"%@", color); [bitmap release]; } return 0; }
If you want to continue working, you need to take additional steps to create and run a run loop.
source share