class Flashman::Flashman

Public Class Methods

new(window, output, interval, speed, times) click to toggle source
# File lib/flashman.rb, line 22
def initialize(window, output, interval, speed, times)
  @work_dir = File.join(Dir.home, '.flashman')
  begin
    Dir.mkdir(@work_dir, 0755)
  rescue Errno::EEXIST
    # Do nothing
  end
  @pid_file = "#{@work_dir}/flashman.pid"
  @stop_pid = "#{@work_dir}/flashstop.pid"

  check_macosx
  check_multirun
  optcheck(output, interval, speed, times)

  @windowid = `osascript -e 'tell app \"#{window}\" to id of window 1' 2>/dev/null`.strip
  unless @windowid =~ /^\d+$/
    puts "#{window} isn't the actual application name running on your mac."
    exit 1
  end

  @output = output
  @interval = interval
  @delay = (100 * @interval / speed).to_i
  @times = times.to_i
  @finish_trap = false
end

Public Instance Methods

check_macosx() click to toggle source
# File lib/flashman.rb, line 87
def check_macosx
  os = `uname`
  unless os =~ /^Darwin/
    puts "flashman is only for MacOSX."
    exit 1
  end
end
check_multirun() click to toggle source

TODO: There must be any good way to check multirunning.

# File lib/flashman.rb, line 96
def check_multirun
  if File.exist?(@pid_file)
    pid = File.open(@pid_file).first.to_i
    ps = ProcTable.ps(pid)
    if ps.cmdline =~ /flashman\s+/
      puts "flashman has already been running."
      exit 1
    end
  end
end
optcheck(output, interval, speed, times) click to toggle source
# File lib/flashman.rb, line 107
def optcheck(output, interval, speed, times)
  unless output
    usage
    exit 1
  end

  if interval < 0.1 or interval > 10
    usage
    puts "-i option has to be the range between 0.1 and 10."
    exit 1
  end

  if speed < 1 or speed > 10
    usage
    puts "-s option has to be the range between 1 and 10."
    exit 1
  end

  if times < 0
    usage
    puts "-t option has to be 0 or more."
    exit 1
  end
end
run() click to toggle source
# File lib/flashman.rb, line 49
def run
  begin
    Process.daemon(true, true)
    set_trap
    open(@pid_file, 'w') {|f| f << Process.pid}

    i = 0
    loop do
      break if @finish_trap
      file = "#{@work_dir}/#{i.to_s.rjust(6, "0")}"
      `screencapture -t gif -o -l #{@windowid} #{file}.gif 2>&1 >/dev/null`
      sleep(@interval)
      i += 1
    end
  rescue => e
    STDERR.puts "[ERROR][#{self.class.name}.run] #{e}"
    exit 1
  end

  `gifsicle -O2 --delay=#{@delay} --loopcount=#{@times} #{@work_dir}/*.gif > #{@output} 2>/dev/null`
  File.unlink *Dir.glob("#{@work_dir}/*.png")
  File.unlink *Dir.glob("#{@work_dir}/*.gif")
  File.unlink @pid_file

  if File.exist?(@stop_pid)
    pid = File.open(@stop_pid).first.to_i
    Process.kill("TERM", pid)
  end
end
set_trap() click to toggle source
# File lib/flashman.rb, line 83
def set_trap
  Signal.trap(:TERM) {@finish_trap = true}
end
usage() click to toggle source
# File lib/flashman.rb, line 79
def usage
  puts "flashman [-w appname] [-o output-file] [-i interval] [-s speed] [-t loop-times]"
end