Jessica Fae (jessica_fae) wrote,
Jessica Fae
jessica_fae

  • Mood:

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

Subscribe
  • Post a new comment

    Error

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 0 comments