Search code examples
cocoamacosscreenshotmacos-carbon

How do I take a screengrab of a single window using Cocoa or Carbon on OS X?


I need to be able to identify a single window on the user's screen and take a screen capture of it. The screen data is to be stored in memory and not written to disk.

This is already supported through the commandline tool /usr/sbin/screencapture or through the Grab utility (though their functionality is not extensive enough to justify me launching them as a subprocess).

References / Hints

  • nm /usr/sbin/screencapture returns private Cocoa interfaces including _CGSGetSharedWindow that appear to do this.
  • Third party application Snapzpro does this (but does not provide source code)

Solution

  • Mac OS X 10.5 introduced the Quartz Window Services API to do just this.