Skip to content

Instantly share code, notes, and snippets.

@gouf
Created January 9, 2014 18:31
Show Gist options
  • Save gouf/8339354 to your computer and use it in GitHub Desktop.
Save gouf/8339354 to your computer and use it in GitHub Desktop.
Mac のクリップボードを監視して、設定されたファイル拡張子をwget で実行フォルダにダウンロード。
require 'pp'
require 'clipboard'
module Downloader
@@target_file_exts = [:jpg, :png, :gif, :bmp, :doc, :pdf]
@@cache = []
def clipboard_text
#%x(pbpaste).scrub
Clipboard.paste.scrub
end
def download
clipboard_text = clipboard_text()
return if cached? clipboard_text
return unless download? clipboard_text
puts "URL: #{clipboard_text}"
puts '----'
begin
spawn("wget #{clipboard_text}", STDERR => STDOUT)
rescue Erno::ENOENT => e
puts "failed download."
puts "Error: #{e.message}"
end
cache clipboard_text
clipboard_text
end
def target_file_exts ar=nil
if ar.is_a? Array then
unless ar.nil? then
@@target_file_exts = ar.map(&:to_sym)
end
end
@@target_file_exts
end
def download? str
ext = file_ext str
return false if str.nil?
target_file_exts.find_index(ext.to_sym) ? true : false
end
def file_ext str
str.split('.').last.to_s[0..-1]
end
def cache str=nil
return @@cache if str.nil?
@@cache << str
@@cache.uniq!
end
def cached? url
@@cache.find_index(url) ? true : false
end
end
class ClipboardWatch
include Downloader
end
c = ClipboardWatch.new
c.target_file_exts ARGV if ARGV.size > 0
print "target file extensions: "
pp c.target_file_exts
loop do
c.download
sleep 2
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment