Skip to content

Instantly share code, notes, and snippets.

@typemytype
Last active June 15, 2017 20:41
Show Gist options
  • Save typemytype/6c4ee724f61e782c127079c95afbdfa2 to your computer and use it in GitHub Desktop.
Save typemytype/6c4ee724f61e782c127079c95afbdfa2 to your computer and use it in GitHub Desktop.
take screenshot and save it
import Quartz
import AppKit
from mojo.UI import CurrentSpaceCenterWindow
# a path where to save the screenshot
path = u"~/Documents/test.png"
# get the space center window
sp = CurrentSpaceCenterWindow()
# get the cocoa window object
w = sp.window().getNSWindow()
# get the window number
windowNumber = w.windowNumber()
# get the screenshot
screenshot = Quartz.CGWindowListCreateImage(
Quartz.CGRectNull, # use Quartz.CGRectInfinite for a full screenshot
Quartz.kCGWindowListOptionIncludingWindow,
windowNumber,
Quartz.kCGWindowImageShouldBeOpaque)
# convert it to a CIIimage
result = AppKit.CIImage.imageWithCGImage_(screenshot)
# create a represenation
rep = AppKit.NSBitmapImageRep.alloc().initWithCIImage_(result)
# get the bitmap data
PNGData = rep.representationUsingType_properties_(AppKit.NSPNGFileType, None)
# save it to disk
PNGData.writeToFile_atomically_(path, False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment