Screen Captures with Nu

Friday, 04 Apr 2008

Here is a Nu version of Marcus Crafter’s RubyCocoa screen capture script. It doesn’t add anything new to Marcus’ script; it just shows how the same underlying interfaces can be used from Nu.

;; port of http://redartisan.com/2008/1/12/rubycocoa-screen-capture

(import Quartz)

(class CIImage

     (- writeToFile:target format:format properties:properties is
        (unless format (set format NSJPEGFileType))
        ((((NSBitmapImageRep alloc) initWithCIImage:self)
          representationUsingType:format properties:properties)
         writeToFile:target atomically:NO))

     (- cgimage is
        (((NSBitmapImageRep alloc) initWithCIImage:self) CGImage))

     (+ readFromFile:file is
        (unless ((NSFileManager defaultManager) fileExistsAtPath:file)
                (NSException raise:"File Not Found" 
                     format:"failed to read #{file}"))
        (CIImage imageWithContentsOfURL:(NSURL fileURLWithPath:file))))

(class Screen is NSObject

     (+ capture is
        (set screenshot (CGWindowListCreateImage CGRectInfinite
                             kCGWindowListOptionOnScreenOnly
                             kCGNullWindowID
                             kCGWindowImageDefault))
        (CIImage imageWithCGImage:screenshot))

     (+ captureWithFade is
        (set result nil)
        (self fadeAndDo:
              (do (token)
                  (self snapWithFadeToken:token)
                  (set result (self capture))))
        result)

     (+ fadeAndDo:block is
        (set err (CGAcquireDisplayFadeReservation kCGMaxDisplayReservationInterval
                      (set token (NuPointer new))))
        (if (eq err kCGErrorSuccess)
            (try
                (CGDisplayFade (token value) 0.3 kCGDisplayBlendNormal
                     kCGDisplayBlendSolidColor 0 0 0 YES)
                (block token)
                (catch (exception)
                       (NSLog "#{(exception name)}: #{(exception reason)}"))
                (finally
                        (CGDisplayFade (token value) 0.3 kCGDisplayBlendSolidColor
                             kCGDisplayBlendNormal 0 0 0 NO)
                        (CGReleaseDisplayFadeReservation (token value))))))

     (+ snapWithFadeToken:token is
        (set display (CGMainDisplayID))
        (set result (CGDisplayCapture display))
        (if (eq (CGDisplayCapture display) kCGErrorSuccess)
            (if (set context (CGDisplayGetDrawingContext display))
                (try
                    (set pic (CIImage readFromFile:"nikon.jpg"))
                    (set display_width (CGDisplayPixelsWide display))
                    (set display_height (CGDisplayPixelsHigh display))
                    (set pic_width  ((pic extent) third))
                    (set pic_height ((pic extent) fourth))
                    (set position_x (/ (- display_width pic_width) 2.0))
                    (set position_y (/ (- display_height pic_height) 2.0))
                    (CGContextDrawImage context
                         (list position_x position_y pic_width pic_height)
                         (pic cgimage))
                    (CGDisplayFade (token value) 0.3 kCGDisplayBlendSolidColor
                         kCGDisplayBlendNormal 0 0 0 YES)
                    (CGDisplayFade (token value) 0.3 kCGDisplayBlendNormal
                         kCGDisplayBlendSolidColor 0 0 0 YES)
                    (catch (exception)
                           (NSLog "#{(exception name)}: #{(exception reason)}"))
                    (finally
                            (CGDisplayRelease display)))))))

((Screen captureWithFade) writeToFile:"desktop.jpg" format:nil properties:nil)
Comments (0) post a reply