Skip to content

Instantly share code, notes, and snippets.

@scriptingosx
Created January 9, 2018 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scriptingosx/ac6bcfe5f88591607db4d299296520ae to your computer and use it in GitHub Desktop.
Save scriptingosx/ac6bcfe5f88591607db4d299296520ae to your computer and use it in GitHub Desktop.
Cocoa Python code to generate an image file for the current Mac model
#!/usr/bin/python
from Cocoa import NSImage, NSImageNameComputer, NSSize, NSMakeSize, NSMakeRect, NSBitmapImageRep, NSPNGFileType, NSData
from CoreGraphics import CGImage
from objc import NULL
size = NSMakeSize(512, 512)
image = NSImage.imageNamed_(NSImageNameComputer)
image.setSize_(size)
cgRef, rect = image.CGImageForProposedRect_context_hints_(NULL, None, None)
imageRep = NSBitmapImageRep.alloc().initWithCGImage_(cgRef)
imageRep.setSize_(image.size())
pngData = imageRep.representationUsingType_properties_(NSPNGFileType, None)
pngData.writeToFile_atomically_("computer.png", True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment