Last active
April 16, 2018 12:26
-
-
Save mamantoha/e4a2e173a852d6901918eef56e5bc884 to your computer and use it in GitHub Desktop.
Ruby script to change desktop wallpaper in Plasma > 5.7 from GoPro photo of the day
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'net/http' | |
require 'open-uri' | |
require 'open3' | |
require 'json' | |
require 'optparse' | |
require 'tempfile' | |
options = {} | |
OptionParser.new do |opts| | |
opts.banner = "Usage: #{__FILE__} [options]" | |
opts.on('-nNUMBER', '--number=NUMBER', 'Image number from 1 to 9') do |n| | |
options[:number] = n | |
end | |
opts.on("-h", "--help", "Prints this help") do | |
puts opts | |
exit | |
end | |
end.parse! | |
number = options[:number] ? options[:number].match(/\A[1-9]{1}\z/) ? $&.to_i : 1 : 1 | |
number -= 1 | |
url = 'https://api.gopro.com/v2/channels/feed/playlists/photo-of-the-day.json?platform=web&page=1&per_page=9' | |
uri = URI(url) | |
response = Net::HTTP.get(uri) | |
data = JSON.parse(response) | |
image_url = data['media'][number]['thumbnails']['full']['image'] | |
image_uri = URI(image_url) | |
image_file = Tempfile.new('gopro_wallpaper') | |
set_wallpaper_script = %Q[ | |
var allDesktops = desktops(); | |
print (allDesktops); | |
for (i=0; i<allDesktops.length; i++) { | |
d = allDesktops[i]; | |
d.wallpaperPlugin = "org.kde.image"; | |
d.currentConfigGroup = Array("Wallpaper", "org.kde.image", "General"); | |
d.writeConfig("Image", "file://#{image_file.path}") | |
} | |
] | |
begin | |
# Download image to tempfile | |
File.open(image_file, "wb") do |f| | |
open(image_url, "rb") do |read_file| | |
f.write(read_file.read) | |
end | |
end | |
stdin, stdout, stderr, wait_thr = Open3.popen3( | |
"qdbus org.kde.plasmashell /PlasmaShell org.kde.PlasmaShell.evaluateScript '#{set_wallpaper_script}'" | |
) | |
if stdout.read =~ /Widgets are locked/ | |
puts "Cannot change the wallpaper while widgets are locked! (unlock the widgets)" | |
exit | |
end | |
sleep 1 | |
ensure | |
image_file.close | |
end |
Unfortunately, I didn't find a way to unlock widgets programmatically.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi there!
This script works very well as long as the desktop widgets are unlocked. But I'd like to keep them locked at all times. I was wondering if there is a way to unlock desktop widgets before setting the wallpaper. Thanks.