Module: Desklickrb

Defined in:
lib/desklickrb.rb,
lib/desklickrb/version.rb

Constant Summary collapse

VERSION =
"1.0.2"

Instance Method Summary collapse

Instance Method Details

#apply_desktop_pictureObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/desklickrb.rb', line 57

def apply_desktop_picture

  puts "applying desktop picture to #{filename}" if @verbose

  # set desktop picture with another filename because Dock does not change
  # the picture of the same filename with previous.
  another_file = filename + ".jpg"
  FileUtils.cp(filename, another_file)

script =<<EOS
tell application "System Events"
set picture of every desktop to "#{another_file}"
set picture of every desktop to "#{filename}"
end tell
EOS

# 2015/07/13: osascript がこんなエラーを出すので 2>&1 した。
# osascript: OpenScripting.framework - scripting addition "/Library/ScriptingAdditions/LCC
# Scroll Enhancer Loader.osax" cannot be used with the current OS because it has no OSAXHandlers
# entry in its Info.plist.
  IO.popen("/usr/bin/osascript > /dev/null 2>&1", "r+") {|io| io.puts script }
end

#choose_photoObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/desklickrb.rb', line 20

def choose_photo
  if @tag
    puts "picking up a photo by a tag #{@tag}" if @verbose
    photos = flickr.tag(@tag)
  elsif @user_id
    puts "picking up a photo of a user #{@user_id}" if @verbose
    abort "unknown user: #{@user_id}" unless user = flickr.users(@user_id)
    photos = user.photos
  else
    puts "picking up a photo from interestingness" if @verbose
    photos = flickr.photos_request('interestingness.getList')
  end

  return nil if photos.size < 1

  # find photo by random
  index = [*0..(photos.size-1)].sample
  photo = photos[index]
  puts "chosen photo index=#{index} of #{photos.size}" if @verbose

  # find maximum
  max_size = nil
  photo.sizes.each do |size|
    max_size = size if max_size == nil || max_size['width'].to_i < size['width'].to_i
  end
  puts "chosen photo size is #{max_size['width']} x #{max_size['height']}" if @verbose
  max_size
end

#filenameObject



8
9
10
# File 'lib/desklickrb.rb', line 8

def filename
  @filename ||= "/tmp/desklickrb.jpg"
end

#flickrObject



12
13
14
15
16
17
18
# File 'lib/desklickrb.rb', line 12

def flickr
  unless @flickr
    raise 'API Key must be created at https://www.flickr.com/services/apps/create/' unless @api_key
    @flickr = Flickr.new(@api_key)
  end
  @flickr
end

#write_file(size) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/desklickrb.rb', line 49

def write_file(size)
  puts "downloading image file to #{filename}" if @verbose
  file = Net::HTTP.get_response(URI.parse(size['source'])).body
  open(filename, 'w') do |io|
    io.puts file
  end
end