Module: ScreenShooter

Defined in:
ext/accessibility/screen_shooter/screen_shooter.c,
lib/accessibility/screen_shooter.rb,
ext/accessibility/screen_shooter/screen_shooter.c

Overview

A module that adds a simple API for taking screen shots. It only uses default options at the moment, so screen shots may not come out as fancy as they could...this could be fixed in the future...

Instance Method Summary collapse

Instance Method Details

#screenshot(rbrect, path) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'ext/accessibility/screen_shooter/screen_shooter.c', line 7

static
VALUE
rb_ss_take_shot(__unused const VALUE self, const VALUE rbrect, const VALUE path)
{
    NSString* const ns_path =
	[[NSString alloc] initWithBytesNoCopy:RSTRING_PTR(path)
                                       length:RSTRING_LEN(path)
                                     encoding:NSUTF8StringEncoding
                                 freeWhenDone:NO];

    CGRect rect = unwrap_rect(rbrect);
    if (rect.size.width < 0 || rect.size.height < 0)
	rect = CGRectInfinite;

    CGImageRef const image =
	CGWindowListCreateImage(rect,
				kCGWindowListOptionOnScreenOnly,
				kCGNullWindowID,
				kCGWindowImageDefault);

    NSBitmapImageRep* const rep =
	[[NSBitmapImageRep alloc] initWithCGImage:image];

    NSData* const data = [rep representationUsingType:NSPNGFileType
                                           properties:@{}];

    const VALUE result =
	[data writeToFile:ns_path atomically:NO] ? Qtrue : Qfalse;


    [ns_path release];
    if (image)
	CFRelease(image);
    [rep release];
    [data release];

    return result;
}

#shoot(rect = CGRect.new(CGPoint.new(0, 0), CGSize.new(-1, -1)), path = '~/Downloads') ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/accessibility/screen_shooter.rb', line 6

def shoot rect = CGRect.new(CGPoint.new(0, 0), CGSize.new(-1, -1)),
          path = '~/Downloads'

  path = File.expand_path path.to_s
  path = "#{path}/AXElements-ScreenShot-#{Time.now.strftime '%Y%m%d%H%M%S'}.png"

  raise 'Failed to save screenshot' unless screenshot rect, path

  path
end