Last active
December 29, 2015 17:19
-
-
Save masaakif/7703097 to your computer and use it in GitHub Desktop.
CentOSで壁紙を変更するRubyスクリプト
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
#!/usr/local/rbenv/shims/ruby | |
require "open3" | |
def gen_export_dbus | |
pid = %x[ pgrep -u `whoami` gnome-panel ].chomp | |
dbus_address = "" | |
File.read("/proc/#{pid}/environ").split("\0").each do |line| | |
if /DBUS_SESSION_BUS_ADDRESS/ =~ line | |
dbus_address = line.split("=")[1..-1].join("=") | |
end | |
end | |
"export DBUS_SESSION_BUS_ADDRESS=#{dbus_address}" | |
end | |
def get_datetime | |
Time.now.strftime("%Y-%m-%d %H:%M:%S") | |
end | |
def exec_cmd(cmd) | |
so, se, sts = Open3.capture3(cmd) | |
puts "#{get_datetime} [OUTPUT] : #{so}" if so != "" | |
puts "#{get_datetime} [ERROR] : #{se}" if se != "" | |
puts "#{get_datetime} [STATUS] : #{sts}" if sts != "" | |
end | |
def select_wallpaper | |
current = `gconftool-2 --get '/desktop/gnome/background/picture_filename'` | |
puts "#{get_datetime} : Current picture is #{current}" | |
pics = Array.new() | |
nextpic = "" | |
Dir.glob("#{ENV['HOME']}/wallpapers/*").each do |f| | |
pics << f if f != current | |
end | |
pics.sample | |
end | |
exd = gen_export_dbus | |
wallpaper = select_wallpaper | |
exec_cmd("#{exd}; gconftool-2 --type string --set '/desktop/gnome/background/picture_options' scaled") | |
exec_cmd("#{exd}; gconftool-2 --type string --set '/desktop/gnome/background/picture_filename' #{wallpaper}") | |
puts "#{get_datetime} : Changed to #{wallpaper}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment