Class: Mvscreenshot::Watcher
- Inherits:
-
Object
- Object
- Mvscreenshot::Watcher
- Defined in:
- lib/mvscreenshot.rb
Instance Method Summary collapse
- #date_of(file) ⇒ Object
-
#initialize(opts = {}) ⇒ Watcher
constructor
A new instance of Watcher.
- #move(image) ⇒ Object
- #watch ⇒ Object
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.(File.join(@dest, new_name)) FileUtils.mkdir_p File. @dest FileUtils.mv(image, new_path) `open -a #{@open} '#{new_path}'` if @open end |
#watch ⇒ Object
18 19 20 21 22 23 24 25 |
# File 'lib/mvscreenshot.rb', line 18 def watch fsevent = FSEvent.new fsevent.watch File.(@dir) do |directories| glob = File.(File.join(@dir, 'Screen\ Shot*.*')) Dir[glob].each { |image| move image } end fsevent.run end |