_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)) screenRep = NSBitmapImageRep.alloc().initWithFocusedViewRect_(rect) image.addRepresentation_(screenRep) # Save representation = _getFileRepresentationType(filepath) data = image_rep.representationUsingType_properties_(representation, None) data.writeToFile_atomically_(filepath, False)