Skip to content

Instantly share code, notes, and snippets.

@stenehall
Created April 25, 2012 21:25
Show Gist options
  • Save stenehall/2493577 to your computer and use it in GitHub Desktop.
Save stenehall/2493577 to your computer and use it in GitHub Desktop.
Rotating backgrounds for OS X Lion
#!/usr/bin/env ruby
require 'nokogiri'
require 'open-uri'
require 'pathname'
require 'appscript'
require 'json'
include Appscript, MacTypes
@count_file = "/path/random_screenshot/count"
# All the top backgrounds
if File.exists?(@count_file) then
open(@count_file, 'r') do |f|
while line = f.gets
@count = line.to_i + 1
end
end
else
@count = 1
end
# Create a new file and write to it
open(@count_file, 'w') do |f|
f.puts @count
end
@offset = 0
while (@offset + 20) < @count do
@offset += 20
end
@count = @count - @offset
@url = "http://wallbase.cc/toplist/#{@offset}/213/gteq/1366x768/000/110/20/0"
doc = Nokogiri::HTML(open(@url))
href = doc.css(".thumb:nth-child(#{@count}) a.thdraggable").first.attributes['href']
doc = Nokogiri::HTML(open(href))
@src = doc.css('#bigwall img').first.attributes['src']
# Looks the same for both cases
filename = Pathname.new(@src).basename.to_s
new_photo = '/path/'+filename
photo = open(@src).read
open(new_photo, 'wb') do |file|
file << photo
end
app('Finder').desktop_picture.set(FileURL.path(new_photo))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment