When faced with a tedious and repetitive task on the computer, it’s often tempting to think how much easier it would be to delegate that task to someone else. With the software package AutoIt, programmers can make that a reality, by having the computer do their work for them while they are away.
The automation software lets programmers move the mouse, click, send keystrokes, and read both text and colors from the screen. This software can be very useful in situations when direct API or database access to a software package is not available – enabling the developer to extract information in an automated way through the existing interface. The AutoIt package can also be extremely helpful for network engineers who need to deploy a GUI-only installer across the network. By simulating installer mouse clicks and keystrokes, it is possible to deploy complex software to several hundred computers without visiting individual PCs.
Executing the mouse clicks is very easy in AutoIt. Through a language similar to VBScript, each automation command takes one line:
MouseClick("left",125,125)
This code will move the mouse to a coordinate offset of (125,125), and then click with the left mouse button. The main challenge is defining the positioning of the offset. There are three options in AutoIt – absolute, relative, and client positioning. Absolute positioning will click relative to the physical screen, or top-left of the monitor. Relative positioning will click based on the top-left of the currently active window. Finally, client positioning will click relative to the client area of the current window, excluding the title bar and header area. Setting this mouse coordinate mode should be done at the beginning of the program through the command:
Opt("MouseCoordMode", 0) ;1=absolute, 0=relative, 2=client
Although absolute positioning is default value, relative positioning is more useful in automation applications.
Next, keystrokes can be sent through an additional one-line command:
Send("Hello{ENTER}")
This line of code will press the letters H-e-l-l-o on the keyboard, followed by the ENTER key. A variety of other special keys are available, such as the TAB and arrow keys.
Finally, since many Windows users spend much of their time waiting and watching the hour glass while programs load or actions are taken, when developing integration applications, it’s also important to tell the automation program how to wait. This can be accomplished with the following commands:
WinWaitActive("Internet Explorer")
Sleep(500)
;Do something here
WinWaitClose("Internet Explorer")
WinWaitActive will pause execution until the AutoIt program sees that a program with the specified title is currently active, while WinWaitClose will pause execution until that program is no longer running on the computer. Since often it takes a few seconds for the window to fully load even after it exists on the screen, the Sleep command can tell the computer to wait a certain number of milliseconds before proceeding to the next line of code.
In addition to these commands, a large subset of VBScript has been implemented, with IF statements, loops, and variables. AutoIt has personally saved us hundreds of hours over the years, from simplifying accounting data entry, to automating jobs on the network. Through its easy and effective language, many repetitive and low-value tasks can now be done by the PC – freeing up people to work toward more challenging and productive business goals.
Written by Andrew Palczewski
About the Author
Andrew Palczewski is CEO of apHarmony, a Chicago software development company. He holds a Master's degree in Computer Engineering from the University of Illinois at Urbana-Champaign and has over ten years' experience in managing development of software projects.
Google+