Sunday, March 13, 2005

Fun Script Project

OK,
I decided to take a little break this weekend and do something that I've been wanting for a little while. What is it you might ask? Well, it seems rather dumb, but I like for my wallpaper to change. Now, when I was back in X Windows land, this was simple. Perl, crontab, and voila changing wallpapers. It seems to be more a challenge on windows. So, I dusted off google and found the solution. I implemented it in a quick and dirty little ruby script. All I did was put all of my images (already converted to bmp) into one directory and it cycles between them all. Nothing sexy really. I've just been curious on how to do this on windows. I have Squeak that changes my wallpaper of my current project. So, I wanted it all of the time. I'm a happy boy now. Here it is (like I said, it is ROUGH)
    require 'win32/registry'

    #show bmp in file_name stretched
    def show_wallpaper(file_name)
    Win32::Registry::HKEY_CURRENT_USER.open('Control Panel\Desktop', Win32::Registry::KEY_ALL_ACCESS) do |reg|
    reg['Wallpaper', Win32::Registry::REG_SZ] = file_name;
    reg['WallpaperStyle', Win32::Registry::REG_SZ] = 2;
    reg['TileWallpaper', Win32::Registry::REG_SZ] = 0;
    end
    `RUNDLL32.EXE user32.dll,UpdatePerUserSystemParameters`
    puts "Showing: #{file_name}"
    end

    #Obtain the next file to use as wallpaper. Basically, cycle through the directory

    def next_file_name(current_file_name)
    found = false
    first_file_name = nil
    Dir['C:/wallpaper/*.bmp'].each do | any |
    first_file_name = any unless first_file_name
    return any if found
    found = true if any == current_file_name
    end
    first_file_name
    end

    #Get the current wallpaper file
    current_file_name=nil
    Win32::Registry::HKEY_CURRENT_USER.open('Control Panel\Desktop') do |reg|
    current_file_name=reg['Wallpaper', Win32::Registry::REG_SZ]
    end

    #get the next and then show

    image_file_name = next_file_name(current_file_name.gsub(/[\\]/, '/'));
    show_wallpaper(image_file_name) if image_file_name

That's it! It's amazing how little information there is. I found changing the registry only by chance. And then, it will not update. Calling the rundll32 command by pure chance. It was a good learning experience and it makes me a little bit of a happier boy. One more note, I converted all of my jpg and gifs to bmp via Squeak. I couldn't find any conversion image stuff for Ruby. If anyone knows of any, let me know. Now, back to Fox/Ruby land for work on my Trail Blazer inspired tool...=)

1 comment:

Vincent Foley said...

The most used graphic library in Ruby is probably RMagick, which is a binding to ImageMagick. Converting a file is also rather easy: http://studio.imagemagick.org/RMagick/doc/comtasks.html#convert

Enjoy Ruby :) If one day we could have Ruby's pragmatism and Smalltalk's IDE, we'd show those Java boys...

Amazon