Skip to content

Absolute Beginner's Guide

Jamie Lee edited this page Jun 24, 2014 · 3 revisions

Automating Windows Desktop Applications!

Before you begin: Make sure you have installed Ruby 1.9.3 or greater.

Install the RAutomation gem by typing this code into the command line or terminal.

gem install rautomation

Start off your script with the require statement so that the gem can be utilized.

require "rautomation"

Now let's target a particular window that we want to manipulate. How about Microsoft's Paint application? Simple enough.

Tell the computer you want to identify the paint window by instantiating a new RAutomation::Window object. Name the variable anything you like.

paint_window = RAutomation::Window.new :title => /Paint/

Ideally, the value that we assign to the :title key should be exactly what can be read in the top of the window in the browser's title bar. Then we would put it in quotes. For instance, if the top of the window read "This is the title of this window" then we would have typed:

paint_window = RAutomation::Window.new :title => "This is the title of this window"

However, sometimes titles change based on outside factors (such as what you decide to name your paint file). So we use the regexp (which stands for "regular expression") notation: /Paint/. This means that the script will look for the window that contains the string "Paint" but it is not restricted to looking for windows that only contain "Paint".

Ok! Now the computer knows which window we are targeting: The Paint window.

In order to "bot" the program (that is, make it do things that we tell it to do automatically), we need to give the commands by using keystrokes. Sometimes, you can use the click method to push a button, but the button requires a title so that the script knows where to click. You won't be able to click the button if the button is an image. There are other identifiers, but so far I have not had much luck using them (sometimes they work, sometimes they don't). The best bet is to use keystrokes to carry out our commands.

I am using Windows 7. When you push the right alt button on your keyboard for a Microsoft program, you can see all of the hotkeys for each button in the toolbar. When I press "h"...

Great! So all we need to do is tell the computer to "press" alt and then the hotkey that corresponds to our desired command. Run the paint program. Let's open up the main menu drop-down list by telling the computer to do alt + f.

paint_window.send_keys [:alt, 'f']

Save the text file as a .rb file (let's call it paint.rb) and run it in the command line (type ruby paint.rb and hit enter). Look at that! The menu opened up for us automatically! It's like a ghost was controlling the computer.

Let's say we want to send the "s" key so that we can save the file. In order to do so, we need to append "s" to the end of the previous code.

paint_window.send_keys [:alt, 'f'], "s"

This is like telling the computer to press alt + f and then after that press s.
Next, we see the "Save As" dialogue box open. Let's create an object for this window so that it can be detected.

second_window = RAutomation::Window.new :title => "Save As"

Naturally, we want to name the file we are about to save. In order to tell the computer to enter a word into the text box, we need to identify the text_field.

second_window.text_field(:class => "Edit", :index => 0).set "hahaha"

To be continued...