_fileRepresentationMapping = { '.png': NSPNGFileType, '.gif': NSGIFFileType, '.jpg': NSJPEGFileType, '.jpeg': NSJPEGFileType, '.bmp': NSBMPFileType, '.tif': NSTIFFFileType, '.tiff': NSTIFFFileType, } def _getFileRepresentationType(filepath): base, ext = os.path.splitext(filepath) return _fileRepresentationMapping[ext.lower()] def screenshot(self, filepath): # Take screenshot rect = NSScreen.mainScreen().frame() image = NSImage.alloc().initWithSize_((rect.size.width, rect.size.height)) window = NSWindow.alloc().initWithContentRect_styleMask_backing_defer_( rect, NSBorderlessWindowMask, NSBackingStoreNonretained, False) view = NSView.alloc().initWithFrame_(rect) window.setLevel_(NSScreenSaverWindowLevel + 100) window.setHasShadow_(False) window.setAlphaValue_(0.0) window.setContentView_(view) window.orderFront_(self) view.lockFocus() screenRep= NSBitmapImageRep.alloc().initWithFocusedViewRect_(rect) image.addRepresentation_(screenRep) view.unlockFocus() window.orderOut_(self) window.close() rect = NSScreen.mainScreen().frame() image = NSImage.alloc().initWithSize_((rect.size.width, rect.size.height)) screenRep = NSBitmapImageRep.alloc().initWithFocusedViewRect_(rect) image.addRepresentation_(screenRep) # Save representation = _getFileRepresentationType(filepath) data = image_rep.representationUsingType_properties_(representation, None) data.writeToFile_atomically_(filepath, False)