Randomize wallpaper upon KDE login
October 12th 2008
I wanted my wallpaper to change whenever I logged in, but found no easy way to do this. In KDE you can tell it to slideshow the wallpaper, but that’s not what I wanted. Just, when I log, switch to a new wallpaper. I want to say “pick a file randomly out of the files in a directory and set the wallpaper on all the desktops”
So here’s my ruby script.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | #!/usr/bin/env ruby require 'rubygems' require 'getopt/std' module RandomArray def random self[rand(size)] end end opts = Getopt::Std.getopts("d:n:") raise "No directory given. Use #{__FILE__} -d <directory> -n <number of desktops>" unless opts.include?('d') raise "No desktop count given. Use #{__FILE__} -d <directory> -n <number of desktops>" unless opts.include?('n') DIR = opts['d'] image = Dir.entries(DIR).map do |e| "#{DIR}#{File::Separator}#{e}" end.reject do |e| File.directory?(e) end.extend(RandomArray).random 1.upto(opts['n'].to_i) do |i| system("dcop kdesktop KBackgroundIface setWallpaper #{i} #{image} 8") end |
I realize python has some dcop stuff and you can sort of just call it, and ruby probably does too, but I couldn’t find good docs on any of that, and I just found the command line thing to call, so I hacked this up in ruby, and it works quite well.
I realize the reject should really be done before the map, and originally had it like that, but then the File.directory? call was more complicated, so I changed it to this.
Make a bash script in ~/.kde/Autostart and add the following.
1 2 3 | #!/usr/bin/env bash random-wp.rb -d $HOME/wallpaper -n 4 |
I have 4 workspaces, hence the 4 at the end. Alter appropriately.

