This Ruby script fetches a random wallpaper from Unsplash and sets it as your desktop wallpaper periodically.
- Ruby installed on your system
- Internet connection
- Clone the repository (or download the script directly):
- Install required Ruby gems:
gem install json fiddle open-uri net-http
To use the Unsplash API, you need to obtain a client_id
. Follow these steps:
- Sign Up/Login to Unsplash:
- If you don't already have an account, go to Unsplash and sign up.
- If you already have an account, log in.
- Register as a Developer:
- Go to the Unsplash Developer section at Unsplash Developers.
- Create a New Application:
- Click on "Your Apps" in the top-right menu.
- Click the "New Application" button.
- Fill out the required information for your new application. This includes the application name, description, and your usage plan.
- Agree to the terms and create the application.
- Get the
client_id
:
- Once the application is created, you'll be redirected to the application details page.
- On this page, you'll find your
Access Key
andSecret Key
. TheAccess Key
is yourclient_id
.
Replace 'your_actual_access_key'
in the script with your actual Access Key
from Unsplash.
Run the script using Ruby:
ruby background.rb
The provided Ruby script performs the following tasks:
- Fetch a random wallpaper URL from Unsplash using the provided
client_id
. - Download the wallpaper to a local directory (
~/Pictures/
). - Set the downloaded image as the desktop wallpaper using Windows API.
- Repeat the process every 45 minutes (configurable).
Fetching Wallpaper URL:
def fetch_wallpaper_url(client_id)
url = "https://api.unsplash.com/photos/random/?client_id=#{client_id}"
response = Net::HTTP.get_response(URI.parse(url))
if response.is_a?(Net::HTTPSuccess)
data = JSON.parse(response.body)
return data['urls']['raw']
else
raise "Failed to fetch wallpaper: #{response.message}"
end
end
Downloading the Image:
def download_image(url, path)
open(path, 'wb') do |file|
file << open(url).read
end
end
Setting the Wallpaper:
def set_wallpaper(image_path)
User32::SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, image_path, SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE)
end
Changing Wallpaper Periodically:
def change_wallpaper_periodically(client_id, interval)
loop do
begin
wallpaper_url = fetch_wallpaper_url(client_id)
wallpaper_path = File.join(ENV['HOME'], 'Pictures', 'wallpaper.jpg')
download_image(wallpaper_url, wallpaper_path)
set_wallpaper(wallpaper_path)
puts 'Wallpaper changed successfully!'
rescue => e
puts "Error: #{e.message}"
end
sleep(interval)
end
end
The script starts by calling change_wallpaper_periodically
with the specified client_id
and interval (default is 45 minutes):
client_id = 'your_actual_access_key' # Replace with your actual Access Key from Unsplash
interval_in_seconds = 45 * 60
change_wallpaper_periodically(client_id, interval_in_seconds)