[Pythonmac-SIG] Best way to grab screenshot?

Dethe Elza delza at livingcode.org
Fri Nov 4 00:33:10 CET 2005


Hi folks,

I'm trying to put together a screencast program using PyObjC.  The  
first step is to be able to take a screenshot, and I've figured out  
one way to do that, based on The Irate Scotsman's Screen Sharing code 
[1].  Since OS X is using OpenGL to compose the window, and since  
even Series 60 cellphones expose and API for taking screenshots from  
Python, I'm thinking there must be a better way than this.  But  
y'know, I've been wrong before.  Here is the function I'm using:

     def screenShot(self):
         rect = NSScreen.mainScreen().frame()
         image = NSImage.alloc().initWithSize_((rect.size.width,  
rect.size.height))
         window = NSWindow.alloc 
().initWithContentRect_styleMask_backing_defer_(
             rect,
             NSBorderlessWindowMask,
             NSBackingStoreNonretained,
             False)
         view = NSView.alloc().initWithFrame_(rect)
         window.setLevel_(NSScreenSaverWindowLevel + 100)
         window.setHasShadow_(False)
         window.setAlphaValue_(0.0)
         window.setContentView_(view)
         window.orderFront_(self)
         view.lockFocus()
         screenRep= NSBitmapImageRep.alloc().initWithFocusedViewRect_ 
(rect)
         image.addRepresentation_(screenRep)
         view.unlockFocus()
         window.orderOut_(self)
         window.close()
         return image


Suggestions for improvement are welcome!

Next step: Figure out how to create a Quicktime movie and insert the  
images.  I had assumed that the QTKit would allow me to do this, ha  
ha ha.  Fool me once, shame on you Apple, but fool me again and again  
and again....

--Dethe

[1] http://iratescotsman.com/products/source/

There are two ways of constructing a software design.  One way is to  
make it so simple there are obviously no deficiencies and the other  
way is to make it so complicated that there are no obvious  
deficiencies.  --C.A.R. Hoare




More information about the Pythonmac-SIG mailing list