tags:

views:

2457

answers:

7

How to take screenshot programmically of desktop area in Mac OS X ?

+6  A: 

There's some OpenGLScreenSnapshot sample code that shows how to do this using OpenGL. It was just updated a couple weeks ago.

Chris Hanson
+2  A: 

Qt includes an example screenshot app in examples\desktop\screenshot. Qt works on a range of platforms, including MacOSX.

http://trolltech.com/products/qt/

Andy Brice
A: 

If you consider REALbasic, this is extremely easy to do with RB and the MBS Plugins. I've just written an application that does timed screenshots using RB and the MBS Plugins. You can read about it here: http://tektalkin.blogspot.com/2008/08/screenaudit-for-mac-osx.html

stevechol
+4  A: 

Two interesting options I have seen, but yet to use professionally, are the screencapture utility and a MacFuse demo.

The screencapture utility has been around since 10.2, according to the man page, and could be linked to a Cocoa application by use of NSTask.

The MacFuse demo worked by creating a new screenshot each time a folder was opened, or something like that. The idea being you could write a quick script to access the image when you needed it, without having to have the script actually run on that machine.

But seriously, Apple has some other sample code called "Son of Grab" which uses the new CGWindow API which is pretty awesome.

http://developer.apple.com/samplecode/SonOfGrab/

Peter
A: 

The following might be helpful if you are attempting to accomplish this with either C++ or python. Also, this would be even more helpful in the case that you want your programmatic method to be cross-platform portable. (Windows, Linux, Mac osx, and even beyond)

My response involves a similar approach to one of the earlier responses, but the difference is in which cross-platform framework one uses.

In almost the same way that QT will allow you to capture and save a screenshot, so does another "competing" framework, namely the wxWidgets framework. wxWidgets is a C++ framework, but it also provides python bindings via a framework called wxPython.

To read more, follow the following link and choose "Page 139" from the list of pages that match the search:

http://books.google.com/books?id=CyMsvtgnq0QC&vq="accessing+the+screen+with+wxScreendc"

que que
+1  A: 

One way of going about doing this would be to use NSTask in conjuction with the 'screencapture' command line command.

For example:

NSTask *theProcess;
theProcess = [[NSTask alloc] init];

[theProcess setLaunchPath:@"/usr/sbin/screencapture"];
// use arguments to set save location
[theProcess setArguments:@"blahblah"];
[theProcess launch];

The you could open up the file wherever you told it to be saved, process it, and then delete it as needed. Obviously stopgap, but it would work.

+2  A: 

If you're fine with Leopard compatibility, there's a very powerful new CGWindow API that will let you grab screen shots, window shots, or composites of any range of window layers.

http://developer.apple.com/samplecode/SonOfGrab/

Michael Rondinelli