September 16th, 2003

Tink!

New and interesting things.

So, I have this new programming language that I’m learning. It’s called Ruby. For any of you out there who program this post may make sense, but for those of you to whom it does not make any sense, I apologize for that. hehe.

Ok, a simple Python program that writes a menu configuration file for setting backgrounds on my fluxbox windows manager. Limitations are that it’s very hard coded. It only works for the user account joel . Not for any account that wants to run it.

Here is the python code:


import os, string

images = os.listdir(“/home/joel/.fluxbox/backgrounds/”)
images.sort(lambda x, y: cmp(string.lower(x), string.lower(y)))
imagefile = file(“/home/joel/.fluxbox/imagemenu”, “w”)
imagefile.write(“[submenu] (Backgrounds) {Zarchanalin’s Backgrounds}\n”)
imagefile.write(“\t[exec] (Update Backgrounds) {python /home/joel/.fluxbox/makeimagemenu.py}\n”)
for image in images:
entry = string.split(image, “.”)
imagefile.write(“\t[exec] (”)
imagefile.write(string.capwords(entry[0]))
imagefile.write(“) {bsetbg -c /home/joel/.fluxbox/backgrounds/”)
imagefile.write(image)
imagefile.write(“; cp /home/joel/.fluxbox/backgrounds/”)
imagefile.write(image)
imagefile.write(“ /home/joel/.fluxbox/background.jpg}\n”)
imagefile.write(“[end]\n”)
imagefile.close()



Now, the Ruby code. Improvements include the fact that it works for any user. It also has better formatting of file names. (i.e. it removes any ‘-’ or ‘_’s from the file name to write the menu). Now I could have done all the improvements in Python with a lot of research and work. Ruby it just sort of came naturally.

The Ruby Code:


class Menu
@@menu_path = ENV[“HOME”] + “/.fluxbox/imagemenu”
@@image_path = ENV[“HOME”] + “/.fluxbox/backgrounds/”
@@images = Dir::entries(@@image_path)
@@images.shift.shift.sort! {|a, b| a.downcase <=> b.downcase}
def Print_path
print @@menu_path
print “\n”
end
def List_directory
@@images.each do |image|
print image
print “\n”
end
end
def Write_menu (menuname)
File::open (@@menu_path, “w”){|f|
f.write(“[submenu] (Backgrounds) {” + menuname + “}\n”)
f.write(“\t[exec] (Update Backgrounds) {ruby /home/joel/ruby-source/makeimagemenu.rb}\n”)
@@images.each do |image|
f.write(“\t[exec] (” + image.gsub(“.jpg”,“”).gsub(“-”,“ ”).gsub(“_”, “ ”).capitalize + “) {bsetbg -c ” + @@image_path + image + “}\n”)
end
f.write(“[end]\n”)
}
end
end

  • Current Mood
    accomplished accomplished
Tink!

The finished product.

Ok, so here is an update to the code. The last Ruby code posted was very early, and very sketchy. Here is my streamlined version of it.:

class Menu

def to_s
File::open(“#{ENV[’HOME’]}/.fluxbox/imagemenu”, “w”) do |f|
f.puts “[submenu] (Backgrounds) {#{ARGV[0]}}”
f.puts “\t[exec] (Update Backgrounds) {makeimagemenu.rb \”#{ARGV[0]}\“}”
Dir::entries(“#{ENV[’HOME’]}/.fluxbox/backgrounds”).select{|i| i !~/^\./}.sort{|a,b| a.downcase <=> b.downcase}.each do |image|
f.puts “\t[exec] (#{image[0..-5].gsub(”-“,” “).gsub(”_“, ” “).capitalize}) {Esetroot -c #{ENV[’HOME’]}/.fluxbox/backgrounds/#{image}}”
end
f.puts “[end]”
end
end
end
print Menu::new


I know, you’re thinking “What the hell does this do?” Here’s what it does. In fluxbox there is a file called menu. It’s read every time you right click the background. You can include other files. So if you include “imagemenu” then you can use this program to make an easy menu for setting your background.

Here’s how it works. When you get your new account you pull up your console and type ‘makeimagemenu.rb “Whatever Your Menu Is Called”’ and then it makes the menu. Drop all the image files you want into the .fluxbox/backgrounds directory and select “update menu” and it’ll add them to the menu. Then just select the name from the menu and it sets that image as your background. Nice and simple like. Oh, and if you want to change the name of the menu just re-run it from the console.
  • Current Mood
    bouncy bouncy