ruby

Periodically changing desktop wallpaper using Ruby

Here is small ruby script that picks up a random image from a given folder and sets it as the desktop wallpaper.

   1: require 'Win32API'
   2:  
   3: SPI_SETDESKWALLPAPER = 20
   4: SPIF_SENDCHANGE = 0x2
   5: SOURCE_FOLDER = "C:\\Users\\Gaurav\\Pictures\\Best\\"
   6:  
   7: files = Dir.entries(SOURCE_FOLDER) - [".", ".."]
   8: file = files[rand(files.length)]
   9:  
  10: systemParametersInfo = Win32API.new("user32","SystemParametersInfo",["I", "I", "P", "I"],"I")
  11: p systemParametersInfo.call(SPI_SETDESKWALLPAPER, 1, SOURCE_FOLDER + file, SPIF_SENDCHANGE)
 

The constant SOURCE_FOLDER can be changed to point at the folder which contain all your wallpaper and the script then randomly chooses a wallpaper from the collection.

You can also set your task scheduler to run this script automatically after a fixed interval so that your desktop wallpaper keeps changing.

Here is the pastie.

Tuesday, November 27th, 2007 XP, ruby, software, vista, windows 2 Comments

Renaming MP3 files based on ID3 tag information

Here is a neat little trick that I often find useful for renaming mp3 files with wrong names and correct tag titles. Just run this ruby script in the directory where you have stored your songs and it will read the tag title and rename the current file using it.

   1: require "rubygems"
   2: require "id3lib"
   3: require "fileutils"
   4:  
   5: files = `dir /s /b *.mp3`
   6:  
   7: files.split("\n").each do |file|
   8:   dir = File.dirname(file)
   9:   tag = ID3Lib::Tag.new(file)
  10:   new_name = tag.title.gsub("00",'') if tag.title
  11:   FileUtils.move(file, File.join(dir, new_name + ".mp3")) if new_name and !new_name.empty? rescue nil
  12: end

This script requires id3lib gem for ruby which can be installed simply by running the command:

gem install id3lib-ruby

One thing to notice in the script is that currently it works only on windows, but only with a little modification it can be made to work on other OSes as well (The only change I guess will be in the 5th line).

Here is the pastie link for this code.

del.icio.us Tags: ,,
Tuesday, November 27th, 2007 ID3, ruby, software 3 Comments

Creating a User Interface in Ruby using WPF

Let me first explain what is WPF?

WPF is the graphical subsystem if the .NET 3.0 framework and it is there to take the place of window forms which we used with earlier versions of .NET

So what’s so great about WPF?

Actually WPF is has its roots in DirectX API so you can easily create 2D and 3D interfaces in it without putting ant load on the processor. Also it is vector based and stores the information for the generating the UI in separate XAML files. So we have logical separation of a control from its appearance.

Further details: http://en.wikipedia.org/wiki/Windows_Presentation_Foundation

The following are the requirements for running this example:

  • rubyclr gem. Use gem install rubyclr -y
  • .NET framework 3.0

In this example I will creating a creating a textarea along with a button, trapping the events on the button and logging it inside the textarea .

Here is the code for the xaml file. Store this file with filename ui.xaml. This file contains the UI for the application.

   1: <Window
   2: xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
   3: xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
   4: <Grid x:Name="LayoutRoot">
   5: <Button Margin="8,0,8,5.723" VerticalAlignment="Bottom" Content="Button" Name="the_button"/>
   6: <TextBox Margin="8,8,8,38" Name="the_text_box"/>
   7: </Grid>
   8: </Window>
 

Next I will be creating the codebehind file in ruby. Here in this file we have all the application logic. Save this file as logic.rb.

   1: # load the libraries
   2: require 'rubygems'
   3: require 'wpf'
   4: # load the xaml file
   5: window = XamlReader.Load(System::IO::File.open_read('ui.xaml'))
   6:  
   7: # get the controls
   8: button = window.find_name('the_button')
   9: txt_box = window.find_name('the_text_box')
  10: # trap the mouse enter event
  11: button.mouse_enter do |sender, args|
  12: txt_box.text += "MOUSE ENTERED\n"
  13: end
  14: # trap the mouse leave event
  15: button.mouse_leave do |sender, args|
  16: txt_box.text += "MOUSE LEFT\n"
  17: end
  18: # trap the mouse click event
  19: button.click do |sender, args|
  20: txt_box.text += "MOUSE CLICKED\n"
  21: end
  22: # run the application (most important)
  23: Application.new.run(window)

The code given above explains itself.

We are first loading the ui.xaml file and then finding the two controls in the lines 8, 9.

Then the three event handlers are declared in lines 11, 14, 19. So whenever the mouse events are fired the corresponding event if traced into the textbox.

And here is the output.

image

del.icio.us Tags: ,,,,

Tags: , , , , , ,

Tuesday, October 9th, 2007 ruby, user interface, windows, wpf, xaml 2 Comments

Automating Powerpoint with ruby

We know that ruby is a language of few words. We can express a lot of things in a relatively easy manner.

I was trying to export few slides in my powerpoint presentation to an image format using ruby.I could not find any documentation for it anywhere except for a few basic things. But when I tried it by hit and trial I found out it to be surprisingly easy. It required just a few lines.

Here is what you have to do:

   1: require ‘win32ole’
   2: # open powerpoint
   3: ppt = WIN32OLE.new(‘Powerpoint.Application’)
   4: # make sure it is visible
   5: ppt.Visible = true
   6: # open the presentation to be exported
   7: pre = ppt.Presentations.Open(“d:\\imp_file.pptx”)
   8: # export the file
   9: pre.Slides(1).Export(“d:\\exp_file.png”,“png”)
  10: # close powerpoint, will close all the currently open files
  11: ppt.Quit()

You can always use RMagick to further process this image.

The Win32OLE extension library (actually spelled in lowercase, win32ole) provides an interface to Windows OLE automation. Your Ruby code can act as a client for any OLE automation server such as Microsoft Word, Outlook, and Internet Explorer and many other softwares.

So in the above code we are making a object is Powerpoint application, then opening a file and saving the first slide in the “png” format.

Tags: , , , ,

Tuesday, October 9th, 2007 powerpoint, ruby, win32ole, windows No Comments