class Motion::ScreenshotHelper

A class for taking screenshots in Ruby apps running on the Mac

Constants

SCREENSHOT_MODE_FULLSCREEN
SCREENSHOT_MODE_MAINWINDOW

Attributes

config[RW]

Public Class Methods

new(config) click to toggle source
# File lib/project/screenshot_helper.rb, line 10
def initialize(config)
  self.config = default_config.merge(config)
  @counter = 0
  clear_dir
  self
end

Public Instance Methods

default_config() click to toggle source
# File lib/project/screenshot_helper.rb, line 17
def default_config
  {
    screenshot_mode: SCREENSHOT_MODE_FULLSCREEN,
    shots_dir: 'screenshots',
    file_basename: 'screenshot',
    app_name: NSBundle.mainBundle.infoDictionary['CFBundleName']
  }
end
pause(sec=1.0) click to toggle source
# File lib/project/screenshot_helper.rb, line 42
def pause(sec=1.0)
  intervall = NSDate.dateWithTimeIntervalSinceNow sec
  NSThread.sleepUntilDate intervall
end
shoot() click to toggle source
# File lib/project/screenshot_helper.rb, line 30
def shoot
  begin
    focus_app
    system screenshot_cmd
    @counter += 1
  rescue Exception => e
    puts "Exception #{e}"
    return false
  end
  true
end
target_filename() click to toggle source
# File lib/project/screenshot_helper.rb, line 26
def target_filename
  "#{config[:shots_dir]}/#{config[:file_basename].to_s}_#{@counter.to_s}.png"
end

Private Instance Methods

app() click to toggle source
# File lib/project/screenshot_helper.rb, line 54
def app
  NSApplication.sharedApplication
end
app_window() click to toggle source
# File lib/project/screenshot_helper.rb, line 73
def app_window
  return app.mainWindow unless app.mainWindow.nil?
  return app.keyWindow unless app.keyWindow.nil?
  app.windows.first
end
check_shots_dir() click to toggle source
# File lib/project/screenshot_helper.rb, line 49
def check_shots_dir
  self.config[:shots_dir] = File.absolute_path(config[:shots_dir])
  fail(IOError, "#{config[:shots_dir]} is not a directory") unless File.directory?(ndir)
end
clear_dir() click to toggle source
# File lib/project/screenshot_helper.rb, line 95
def clear_dir
  cmd = "rm -rf #{(config[:shots_dir] + '/*').dump}"
  system cmd
end
focus_app() click to toggle source
# File lib/project/screenshot_helper.rb, line 79
    def focus_app
      scpt = <<SCPT
      tell application "#{config[:app_name]}"
      activate
    end tell
SCPT
      osascript scpt
    end
osascript(script_src) click to toggle source
# File lib/project/screenshot_helper.rb, line 88
def osascript(script_src)
  error = Pointer.new('@')
  apple_script = NSAppleScript.alloc.initWithSource(script_src)
  apple_script.executeAndReturnError(error)
  puts "NSAppleScript::error #{error.value.inspect}" unless error.value.nil?
end
screenshot_cmd() click to toggle source
# File lib/project/screenshot_helper.rb, line 58
def screenshot_cmd
  screen_rect = NSScreen.mainScreen.frame
  case config[:screenshot_mode]
  when ScreenshotHelper::SCREENSHOT_MODE_MAINWINDOW
    fr = app_window.frame
    x_origin = fr.origin[0]
    y_origin = screen_rect.size[1] - fr.origin[1] - fr.size[1]
    capture_frame = "#{x_origin},#{y_origin},#{fr.size[0]},#{fr.size[1]}"
    rv  = "screencapture -R#{capture_frame} #{target_filename}"
  else
    rv = "screencapture #{target_filename}"
  end
  rv
end