Class: Mvscreenshot::Watcher

Inherits:
Object
  • Object
show all
Defined in:
lib/mvscreenshot.rb

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Watcher

Returns a new instance of Watcher.



8
9
10
11
12
13
14
15
16
# File 'lib/mvscreenshot.rb', line 8

def initialize(opts = {})
  apply_defaults({
    dir: "~/Desktop",
    dest: "~/Pictures/ScreenShots",
    open: "Preview",
    out_format: "screenshot_%Y%m%dt%H%M%S",
    parse_format: "Screen Shot %Y-%m-%d at %l.%M.%S %p"
  }.merge(opts))
end

Instance Method Details

#date_of(file) ⇒ Object



35
36
37
# File 'lib/mvscreenshot.rb', line 35

def date_of(file)
  DateTime.strptime(File.basename(file, File.extname(file)), @parse_format)
end

#move(image) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/mvscreenshot.rb', line 27

def move(image)
  new_name = date_of(image).strftime(@out_format) + File.extname(image)
  new_path = File.expand_path(File.join(@dest, new_name))
  FileUtils.mkdir_p File.expand_path @dest
  FileUtils.mv(image, new_path)
  `open -a #{@open} '#{new_path}'` if @open
end

#watchObject



18
19
20
21
22
23
24
25
# File 'lib/mvscreenshot.rb', line 18

def watch
  fsevent = FSEvent.new
  fsevent.watch File.expand_path(@dir) do |directories|
    glob = File.expand_path(File.join(@dir, 'Screen\ Shot*.*'))
    Dir[glob].each { |image| move image }
  end
  fsevent.run
end