Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mathieureguer/1e9f5956052fe6700d42b43cd88a9617 to your computer and use it in GitHub Desktop.
Save mathieureguer/1e9f5956052fe6700d42b43cd88a9617 to your computer and use it in GitHub Desktop.
take screenshot and save it
import Quartz
import AppKit
import os.path
from mojo.UI import CurrentSpaceCenterWindow
# get a path for a png file next to the ufo
path_base = os.path.splitext(CurrentFont().path)[0]
path = path_base + ".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