Noprobo

Same Time, Eat More Bagels with AutoIt Automation

January 10th, 2009

I once held a job with a midsized company that provided delicious bagels every morning. Mmm, those fragrant hand-rolled doughy loops and rich organic cream cheese were what got me out of bed. The only problem was that when I came into work they expected me to, you know, work. By break time the bagels were gone and I was left dabbing at crumbs with a disappointed finger.

Something had to be done. Thankfully my job was repetitive and administrative in nature. Even more thankfully, nobody ever realized how inefficient my role was. That’s where I saw opportunity.

AutoIt v3 Logo

Using a brilliant and free software package called AutoIt I created a simple automated routine (henceforth: “script” or “macro”) that did the bulk of my job for me. When those bagels came I was poised and ready whilst my computer was doing my work for me on another floor. With a bit of tweaking about 60% of my ongoing daily workload was completed without any effort or attention.

AutoIt can do almost anything. At its most basic it simply moves the mouse and presses keys, much like you do. At its most sophisticated it can make simple decisions and complete tasks for you.

You don’t have to be a professional programmer to automate with AutoIt. You do however need to have a programmer’s stick-to-itiveness. Your scripts will not work perfectly the first time you run them. Tweaking and tuning are par for the course.

Getting Comfortable

If you’re willing it put in a bit of time in order to save a lot, you need to first become familiar with AutoIt. I’ll walk you through the basics however if any of this requires signification elaboration, you might not be ready for AutoItLand. Don’t despair; there are many graphical, drag-and-drop macro programs out there for you. For the unperturbed, read on.

This article is based on AutoIt version 3.3 running in Windows XP. There is currently no Mac incarnation of AutoIt.

AutoIt v3 Examples

First download and install the AutoIt Full Installation file. Once installed go to the installation directory (usually “Program Files” > “AutoIt3″). You will find a subfolder named “Examples”. Run a few of those example scripts ([right click], “Run Script”) and look at their contents ([right click] “Edit Script”). This will give you a basic idea of the caliber of code you’re looking at.

If you have zero programming knowledge, you might want to back away at this point. If you have even a smidgen of experience with zeroes and ones, AutoIt should appear comfortingly easy.

AutoIt v3 Win Info

AutoIt v3 Help

Also in AutoIt’s installation directory you will find a file called “AutoIt3.chm”. This is AutoIt’s definitive help file. I spend most of my time looking up commands under the “Function Reference” heading on the “Contents” tab (as illustrated). Almost everything you can make AutoIt do is found in this help file so get to love it.

The last important item in the installation directory is a file entitled “Au3Info.exe”. This “AutoIt v3 Window Info” program gives you the inside scoop into what your computer is thinking. It will tell you what the computer calls the Word text entry box (”Class _WwG, instance 1″), the coordinates of your mouse, the dimensions of a window, et cetera. Information from this tool is integral to writing an AutoIt script.

And what would any programming language be without the most important tool of all: Google. Having trouble making AutoIt do exactly what you want? JFGI.

Hello World

So now that you’re equipped with the right tools, you can start with a few basic scripts. Check out AutoIt’s official tutorials and start with the “Hello World” tutorial, or jump to the “Notepad” if you’re no n00b.

Using the code from the “Notepad” example, you can see most of what AutoIt does is send simple instructions. Let’s examine the steps. AutoIt ignores anything after a semicolon (;), so I’ve added comments after a semicolon on each line.

Run("notepad.exe") ; run Notepad
WinWaitActive("Untitled - Notepad") ; wait until the Notepad window is open
Send("This is some text.") ; type "This is some text." on the keyboard
WinClose("Untitled - Notepad") ; close the Notepad window
WinWaitActive("Notepad", "Do you want to save") ; wait for the pop-up window asking to save the file
Send("!n") ; type Alt+N to tell the popup "No" - AutoIt uses an ! to represent Alt key presses

As your scripts get more sophisticated you may need a way to exit a macro in a hurry. Adding the following code will allow you to hit the Esc key to kill the script. I recommend this for every project.

HotKeySet("{ESC}", "Terminate") ; if Esc key is pressed, run the code function "Terminate"
Func Terminate() ; begin function "Terminate"
	Exit 0 ; exit the AutoIt script
EndFunc ; end function "Terminate"

Fuctions are little pockets of code that don’t run unless you ask them to. In this case the Escape key asks the Terminate function to run. Functions may be a bit advanced depending on your programming knowledge. If so, don’t worry about it and just cut ‘n paste the aforementioned code to the top of your script. Alternatively you can right click the AutoIt icon that pops up in your system tray (near the clock) to exit a macro, but I find adding the Esc hotkey code much quicker and easier. Since you will be fine tuning the script a lot in the beginning, this code saves a fair bit of time.

Common Actions

The “Notepad” example sets a good foundation, but there are a few basics I’d like to add. Here are some of the most common things you’ll ask AutoIt to do for you:

Win… Functions: In the AutoIt help file, under the “Index” tab, type Win. Up pops everything you can do with a window for easy reference. The programming language uses Win as a prefix for lots of helpful actions. They’re mostly intuitive: for example WinActivate activates a window. WinMove moves a window. WinClose, you guessed it, closes a window. Look at their entry in the help file for a cut-n-paste-ready example.

MouseClick ( “left”, x, y ): This function simply clicks the left mouse button as specified coordinates x and y. Use the AutoIt Window Info tool to find your required x/y location onscreen.

Send ( “text” ): this function simulates typing on the keyboard. In this case it types the word “text”. You can send non-letter keys as well: ! represents Alt, + is Shift, ^ is Ctrl. If this is too complex to remember, many keys can simply be typed in squiggly brackets: Send ( “{ALT}{ESC}{DEL}” ) performs Alt, Escape, and Delete keystrokes for example. The complete list of keys can be found under the “Send” entry in the help file.

Sleep ( 1000 ): Sometimes you just need to make your script wait a bit. Using this function you can specify how long your script will wait in milliseconds before moving onto the next line of code. Sleep ( 1000 ) represents an 1 second pause.

Bagel Time

1 hour spent programming

=

45 minutes of work saved
Once Every day

Photo by purpleslog

Photo by purpleslog

In my bagel situation, I found that the six hours I spend coding saved me 4.5 hours of work every day. Your mileage may vary since particularly inefficient tasks tend to benefit most from automation. Although you may be put off by having to fight with code, the gains in time are often well worth it; the computer revolution was built on this phenomenon. No less, those mindless, menial, repetitive chores are the ones we’re most eager to dispose of. With a little coding experience you will find yourself outsourcing more and more of these jobs to your computer. What you pay for in programming frustration you make up for in savory bagels. Looks like being replaced by a computer isn’t so bad after all!

Home | Top | Contact © 2008-2010 Noprobo | Legal