Lately I got curious to know how could I grab the entire desktop of my computer, and save it into a file, or display it into an NSImageView component. I started to look around on the web and discovered that:
- There’s no direct support for that in Cocoa
- There’s a lot of different ways to do it, both supported and unsupported, cross-processor and not, easy and complicated
I have found several useful resources in my quest, like this one, this other one, and finally this one. But what I wanted most was a complete application to play with, so what I did is to put all the different implementations I’ve found in one single application, called “ScreenshotDemo”:
Amazingly, the approach that seems the ugliest turned to be the most appropriate, that is, using an NSTask instance wrapping the /usr/sbin/screencapture utility. With it, the application feels lighter, easier to maintain, in the true, purest Unix style: using a collection of small utilities, all chained one to the other, is better than having an overbloated tool that does everything, but just bad.
You can just download the (universal) binaries and the source code from the ScreenshotDemo Project page (sources).
By the way, for those that would like to do the same in C#, like me :) just check out this code. It isn’t much easier in .NET, as you can see ;)
And last but not least, here’s how to do it in wxWidgets, explained by Julian Smart, the creator of this incredible library.