Home > SysAdmin > Windows Automation with AutoIt

Windows Automation with AutoIt

September 12th, 2007 Lol Leave a comment Go to comments

AutoIt LogoWhen thinking about windows automation the question, “how many ways can you skin a cat?” springs to mind, because there are so many different scripting languages and tools. Visual Basic Script (or VBS) for example is built-in to Windows making it very accessible.

But one that I have rediscovered recently is a tool called AutoIt which is now in its 3rd generation. It’s compatible with all versions of Windows old and new (including Vista), and those of you that are familiar with BASIC are going to love it.

AutoIt is completely free and a reasonable 4MB download provides you everything you need to get started.So what do you get;

  • AutoIt program files, documentation and examples.
  • Aut2Exe, which lets you compile your scripts into standalone .exe files. (really nice).
  • AutoItX, a DLL/COM control which lets you call upon AutoIt’s features from your favorite programming environment.
  • Editor, a cut down version of the SciTE script editor which is syntactically aware of AutoIt’s keywords and functions.

What do I like about it

AutoIt can do sooo much, but there are two simple but really neat things I love about AutoIt which work really well together, and they are what I am going to focus on in this article.

The first is it’s ability to pause execution of a script while it waits for a designated window to be activated using the WinWaitActive function. This window can be any dialog such that you might see when installing a program for example, and basically you get AutoIt to identify the window by it’s title, and optionally further text from the dialog.

The second is how it is able to simulate key presses or mouse clicks. This of course works well with the above feature, because not only can I wait for a dialog to appear, but I can then click a button on that dialog also.

Lets work through an example. Supposing I wanted to install the Save as PDF and XPS Add-in for Microsoft Office 2007 to a bunch on machines. I could just email everybody a link to the file with instructions on what options to select, but I may like to simplify the process.

Now the astute among you would suggest that in this case I should simply use a login script to run the installation with the /quiet switch for a completely silent install, to which I would retorte, “Don’t be a smartass, this is just an example ;) ”. So, I might like to have a script click through these screens for my user automatically.

Save As PDF 1

The first dialog that pops up during an install of the Add-in is the one above, and to have AutoIt wait for this dialog to be active I can WinWaitActive like this;

WinWaitActive("Microsoft Save as PDF", "You must accept the Microsoft")

This tells AutoIt to wait for a window to be active with the title “Microsoft Save as PDF”. You will notice that isn’t the entire title, but that doesn’t matter. It’s enough to work out it has the correct one. The second parameter, “You must accept the Microsoft”, is optional and can just be used to Then once AutoIt detect the dialog is active it will resume execution.

So, we’ve got the window we want. Now we want to tick the accept the licence terms blah blah blah box and hit Continue. Well, AutoIt actually does allow you to simulate mouse clicks, but I prefer to use the keyboard if possible since its just a little less prone to problems. So lets hit TAB twice to get down to the checkbox control and then hit Space to tick the box. We can do all these things like this;

So then we want to tell it to click the OK button. There a few ways we could do this including imitating the mouse movement and click, but in this case will just simulate hitting the Enter key like this;

Send("{Tab}")
Send("{Tab}")
Send("{Space}")

And then finally we need to hit the continue button which we can do using ALT + C. Lets send that key combination like this;

Send("!c")

The exclamation mark before the “c” tells AutoIt to send ALT.

Ok, so now the install is underway, and before too long you will get the following dialog telling you that the installation has completed.

Save As PDF 2

Lets add a final couple of lines of code to hit the OK button, thus finishing the install. As before we will instruct AutoIt to pause execution until it finds that the appropriate window is active before sending the Enter key to click the button (as its already in focus).

WinWaitActive("Microsoft Save as PDF", "The installation is complete")
Send("{Enter}")

And that’s it. Well lets improve one step further. We don’t really want to kick of our new AutoIt script and also the SaveAsPDFandXPS.ese file as two seperate steps. So, why don’t we get AutoIt to execute SaveAsPDFandXPS.exe for us :)

To do that we need one simple line at the top of our script. Of course we could worry about where the file is going to be located, and checking to see if it exists etc, but this just an example after all. So, lets just assume we will store it in the same folder as our AutoIt script. So the line looks like this;

ShellExecute("SaveAsPDFandXPS.exe")

This tells AutoIt to execute the executable file within the quotes, and then continue to execute the AutoIt script. That is to say, that AutoIt doesn’t wait for the executable to finish before carrying on itself. (Although AutoIt *can* do that too ;) ).

Ok, so our entire finished AutoIt script looks like this;

ShellExecute("SaveAsPDFandXPS.exe")
WinWaitActive("Microsoft Save as PDF", "You must accept the Microsoft")
Send("{Tab}")
Send("{Tab}")
Send("{Space}")
Send("!c")
WinWaitActive("Microsoft Save as PDF", "The installation is complete")
Send("{Enter}")

Compiling your script into an EXE

Aut2Exe

The final step that you’re probably going to want to do, is to compile your automation script into a packaged Executable. This means you can run it on any computer without the need to install AutoIt first, and to do this you use the Aut2Exe program.

Within the editor save your script, then fire up the Aut2Exe program. Click the browse button next to the Source (AutoIt .au3 file) text box and navigate to your script. In the Destination (.exe/ . a3x file) text box type in the path and filename of the EXE you wish to create, then hit the Convert button. It’s as easy as that.

You can now double-click the EXE file you made in just the same way as any other EXE you ever ran, and you can copy this file and run it on any computer, even if AutoIt isn’t installed.

Lets test it. Make sure you have the SaveAsPDFandXPS.exe file in the same folder as your EXE file, then simply double-click the EXE file. Your compiled AutoIt script will execute the SaveAsPDFandXPS.exe file then automatically click and tick through the dialogs and finish the installation for you. Pretty neat huh.

I hope you’ve enjoyed this simple tutorial for AutoIt. As I said at the beginning of this article, AutoIt can do pretty much anything you could ever want to do with Windows automation including reading and writing to the registry, changing system configuration settings, creating, deleting and copying files, and of course executing them as we have seen here. I recommend getting over to the AutoIt site and checking out the great documentation they have there.

  1. No comments yet.
  1. No trackbacks yet.