Screencast in Cocoa (Python)

I noticed that jwz is trying to take a screenshot in Cocoa and having trouble. I’m happy to see that he’s porting his awesome collection of screensavers to OS X, that’s great news. As for taking screen shots, I’m amazed it is as hard as it seems to be–heck even the new Nokia Series 60 give you the ability to take screeenshots from Python now. In any case, here is a method that works for me under PyObjC. It assumes it is part of a Cocoa object and that you’ve done a from AppKit import * already. Since I can’t comment on jwz’s blog (whether it’s because I don’t use LiveJournal or because I do use Safari, I don’t know), I’ll post it here instead.

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

I cribbed this several months ago from some example Cocoa code, but forgot to make a note of where I got it. If anyone recognizes this pattern, please let me know so I can attribute it.

[Update 2006-03-10]

Nils Soffer pointed out:

A simpler solution:
try:
    screenshot = 'screenshort.pdf'
    os.system('screencapture -i %s' % screenshot)
    image = NSImage.alloc().initWithContentsOfFile_(screenshot)
finally:
    os.remove(screenshot)

:-)

Which reminds me, you can also use the command-line screencapture -c to go to the clipboard instead of a file.  This might be faster, but I haven’t tested it yet.  I still think there should be a Cocoa method to grab the underlying framebuffer of the screen. Besides the -c flag to send the image to the clipboard there is also a -C flag to capture the cursor as well.

[Update 2008-01-22]

Digging through old mail as I move this entry from my custom blog setup to Wordpress, I found where I got this code from originally. It’s from the Irate Scotsman’s Screen Sharing code.  Thanks, Matt!

Silent Blog

Yes, I’ve been quiet for awhile. No, I’m not going to apologize, and I wish more bloggers would stop apologizing when they take time off. One of the great things about Atom/RSS feeds is that I can keep up with blogs that are posted to infrequently, which includes some of my favorites. Don’t feel like you need to post all the time–we’ve all got plenty of other things to read!

My quiet time is going to continue for a bit longer, but while I won’t apologize, I will at least explain. Here’s what I’m doing instead with the couple of hours I have between putting the kids to bed and going to bed myself.

  • Writing the OS X version of the file uploader for The Conversation Network. At some point this will be made public, and the idea is to open source it. I’ll be sure to point that out when it happens. It’s been really great to have this opportunity to work with Doug Kaye and the rest of the gang from ITConversations, after being an admirer (and consumer) of their work for some time.
  • Attended the Northern Voice and Moose Camp conference, where I was fortunate enough to co-moderate a session on community and blogs with Nancy White
  • Writing my own weblog software. Blogger has a better interface than manilasites, but that’s not saying much. I’m also trying to port my old content from manilasites, plus my pre-blog-era work, plus my paper journal. All of this will be hosted at livingcode.org in the near future.
  • Writing more articles for IBM developerWorks, I hope to have more to say about that Real Soon Now
  • Generally organizing the livingcode site better, getting projects their own pages, putting code into SVN. Trying to use Sourceforge was a huge failure, their interface is just too crufty and they still don’t support Subversion. But Dreamhost (where livingcode.org is hosted) does support Subversion now, so I’m going to be moving all my public code over there.
  • I wrote a screensaver using PyObjC which I’m hoping to get included in their distribution as an example, but I need to do some refactoring first (in the time leftover from other projects). I want to write that up too, it was pretty interesting to work on.
  • I periodically take a stab at porting VPython to the Aqua environment. I have some ideas for this (some of them probably heretical to the VPython folks), but need time to work on them. Notice a pattern emerging?
  • Porting Apple’s Sketch demo application from ObjectiveC to PyObjC.
  • Several of these things are just precursors to DrawingBoard, my animation program for kids. I really, really want to be working on that, but need to get some of the smaller projects finished and out of my head so I can concentrate.
  • Exploring programming environments for my kids. My nine-year-old daughter and I will try competing in the PyWeek game challenge later this month.
  • I’m sure I’ve forgotten several significant things.

More news as it happens.

google

google

asus