Create Automation Scripts For Windows With AutoIt

There are plenty of automation tools for Windows. You can use the built-in Task Scheduler to schedule tasks, or use Folder Actions to set up events for folders. If those are not enough for you and and you prefer to use a more complicated and powerful automation tool, AutoIt is a useful tool for you to create automation scripts.

AutoIt is a which is more powerful than batch scripting and can automate almost any kind of task in Windows.

Getting Started

AutoIt is not a complex scripting language. If you have some programming knowledge, you will be able to pick up AutoIt very easily. It will take a while for you to be familiar with the syntax, but once you get it going, you will be able to make use of it to automate repetitive tasks and create other programs that work in Windows.

First of all, you will need to download the AutoIt installer and install it in Windows. The default installation of AutoIt comes with a lite version of SciTE editor, which you can use for creating scripts. If you require more advanced functionality, you may need to download and install the complete SciTE editor.

AutoIt documentation is also available online. It includes (almost) everything you need to know about AutoIt language.

Below, we will show you a few examples of what AutoIt is capable of doing:

Automating the launching and closing of applications

To launch an application, use the Run command in AutoIt:

Run( "program.exe", "c:\program path")

You can also run the application with different user credentials with the RunAs command. If you want to wait for a particular application to close before launching the next one, you can use RunWait command.

To close an application, you can make use of the ProcessClose command.

For example, to close Firefox: local $pid = ProcessExists ("firefox.exe") if $pid then ProcessClose ($pid) Automating program installations

The beauty and power of AutoIt is that you can automate virtually anything in Windows, including the installation of application. If you are a network administrator and want to automatically install programs silently without user intervention, AutoIt can do this very easily.

Basically, you will need to run the setup installer first by using the Run function:

Run ("setup.exe")

You can also give full path of the program if it is not in the current directory.

Run ("C:\path\setup.exe")

Then we need to wait until the interface appears on the screen. We can use WinWaitActive function for this purpose.

WinWaitActive ("Window title", "text")

When the window becomes active, we will be using the shortcut keys to go through the setup process. Most of the installers allow you to use keyboard shortcuts to proceed with the installation process. Usually the keyboard shortcut is denoted by an underline letter, so you will need to press “Alt” and the underlined letter for action.

In AutoIt, you can use the Send function for processing the keyboard shortcut.

Send ("!a")

And when you only need to press the Enter key, simply send Enter:

Send ("Enter")

And when the installation is complete, you can close the window by using WinClose function.

For example, to automate the installation of Office, the script will look like this:

;Run the Office 2010 installer Run ("setup.exe") ;Wait for the setup window to be active WinWaitActive ("Microsoft Office Professional Plus 2010", "setup") ;Accept the license agreement Send (!a) ;Proceed to the next screen Send (!c) ;Install Office with default options WinWaitActive ("Microsoft Office Professional Plus 2010", "Choose the installation you want") Send (!i) ;Close the setup when office is installed WinWaitActive ("Microsoft Office Professional Plus 2010", "Setup Complete") Send (!c)

Since the installer will automatically close after installation, we don’t need to run the WinClose function.

Creating Macros

What makes AutoIt even better is the recorder which can be used for lengthy and tedious sequences of keystrokes. The Macro recorder is available in the full version of SciTE editor.

To access the Macro recorder, open SciTE editor and go to “Tools -> AU3Recorder” or simply press “Alt + F6″ shortcut key. The macro recorder will record all your keystrokes and then simulate those keystrokes when the script is run. The only limitation of the macro recorder is that we don’t get WinWaitActive function inserted automatically between each keystroke. It is important to include WinWaitActive function otherwise the script will complete its execution even before the first setup screen appears.

Conclusion

While there are several other ways to automate programs and tasks in Windows, AutoIt is much more powerful and can perform the most tedious tasks very easily.

 “It is important to include WinWaitActive function otherwise the script will complete its execution even before the first setup screen appears.”

While you are correct in the reason for using the WinWaitActive function, there’s a problem with using just that function by itself: the window being waited for could appear but, for numerous reasons, never become active, causing the script to wait forever (or until it gets cancelled). What should be used is the following:

WinWait(“title”, “text”) If Not WinActive(“title”, “text”) Then WinActivate(“title”, “text”) WinWaitActive(“title”, “text”)

The first command waits until the specified window appears. The second command checks to see if the window is active and if it isn’t, activates it so that the WinWaitActive function sees it and allows the script to carry on. “The only limitation of the macro recorder is that we don’t get WinWaitActive function inserted automatically between each keystroke”

Are you sure? The version of the recorder I currently have installed (admittedly a little old) doesn’t put the WinWaitActive function itself in each place the macro has to wait for a window but instead puts the 3 lines I have above into a function at the beginning of the macro, and then puts a call to *that* function in each place that the macro has to wait for a window. This is an efficient programming style, in which you don’t have large blocks of code repeated numerous times throughout the source file…it comes from a time when programmers had to keep their source files as short and tight as possible in order to keep the programs themselves as small as possible. The only reason I don’t like this new way of using WinWaitActive is that if debugging is turned on, then all that is indicated is that the macro is waiting in the new function. If you weren’t watching carefully, you have no idea what it’s waiting *for*…which makes it difficult to fix the problem.