Introduction to AutoIt

Last updated: Aug 8, 2008

Ever dream about automating mundane tasks? Or wish you could train you computer to do your work for you? I think we would all love to have the power of automation in our lives. With AutoIt we can easily take a boring repetitive task and automate it. AutoIt can send key strokes and mouse movements to your computer. But what makes AutoIt most useful is that it is actually a scripting language allowing If/then logic, flow control, variables and has support for PCRE (Pearl Compatible Regular Expressions). Once you create your script you can even compile it into a single .exe file.

I hope to highlight the most used functions that I used and some of the syntax, as well as get you started with some example code.

Downloading Autoit

Go ahead and head over to autoit’s website and download Autoitv3 Full Installation. You will also benefit by getting the script editor.

When you go to install the software there will be an option on how to handle .au3 files. I usually select run since there is a right click menu for editing through the SciTE editor.

The basics

Start off by making a file called MyFirstScript.au3. Then right click on it and select edit script (assuming you installed the script editor).

Then we will add our first function, MouseMove:

MouseMove(500,500,1)

Save your script with a ctrl+s and run it by double clicking your .au3 file. Did you see it? It might have been fast but you just told your computer to move the mouse to the coords 500w by 500h. The 1 at the end of the code is the speed at which your mouse travels, 1 being almost instantaneous. Go ahead try putting different values and make your mouse move around.

Comments

Comments in autoit are a bit strange. They use a semicolon.

;This function will move my mouse MouseMove(500,500,1)

Sending keystrokes

The next thing you will probably want to do is send a keystroke. To send text and keytext you will use the send() function.

send("{TAB}{ENTER}{DOWN}")

This would obviously hit the tab button, then enter, then down. By default most commands in autoit are delayed by fractions of a second because if they wernt it would be hard to distinguish between hitting a key at the same time or seperately. In this cause the tab, enter, and down would be hit one by one in less than a second. NOT at the same time. More on this later.

send("I love this site")

This code would print out, “I love this site” as if it were typed in by hand.

Sleeping

Autoit is very powerful and can execute commands too quickly if you are not careful. Most of the functions actually have a speed variable or they are default delayed by fractions of seconds to limit the chance of going to fast. The best way to slow the script down is using the sleep command.

sleep(1000)

Since the sleep function is in milliseconds, this code would hold the script from further processing for one second. 500 would be half of one second.

If/Then

If statements are structured like this:

If $confirm == 6 Then ;Do something here Else Exit EndIf

Which brings me to my next point…

Variables

Variables in autoit are similar to php. They use the dollar sign naming convention

;Get the contents of the clipboard and put it in a string $clipboardContents = getClip()

Execute code when a shortcut is pressed

The easiest way to do this is make a never ending loop that is waiting for a specific keystroke.

#include<misc.au3> ;needed to capture keystrokes While 1 If _IsPressed("11") And _IsPressed("31") Then ;Do your movements here EndIf Wend

This snippit would execute code with the combination of ctrl+1 was pressed. You may wonder what the 11 and 31 is then. They are ascii codes.

Common keys used

BACKSPACE {BACKSPACE}, {BS}, or {BKSP} BREAK {BREAK} CAPS LOCK {CAPSLOCK} DEL or DELETE {DELETE} or {DEL} DOWN ARROW {DOWN} END {END} ENTER {ENTER}or ~ ESC {ESC} HELP {HELP} HOME {HOME} INS or INSERT {INSERT} or {INS} LEFT ARROW {LEFT} NUM LOCK {NUMLOCK} PAGE DOWN {PGDN} PAGE UP {PGUP} PRINT SCREEN {PRTSC} RIGHT ARROW {RIGHT} SCROLL LOCK {SCROLLLOCK} TAB {TAB} UP ARROW {UP} F1 {F1} F2 {F2} F3 {F3} F4 {F4} F5 {F5} F6 {F6} F7 {F7} F8 {F8} F9 {F9} F10 {F10} F11 {F11} F12 {F12} F13 {F13} F14 {F14} F15 {F15} F16 {F16}

What can you do with it?

Well, its most common usage is automating installs. When people make unattended Windows CDs they usually want to install a list of apps that they use all the time. Many times these apps have automation install flags. For example Firefox can be installed with, “start /wait setup.exe -ms -ira”.

The problem is that some programs don’t have these automation flags and some people want more control over the installation other than the defaults. This is where autoit comes in.

You can also automate pretty much anything that requires mouse and keystrokes. It might be as simple as making a script that opens up multiple programs or as complex as an auto aim bot for a video game.

I can tell you right now that you will probably get addicted to automating once you get started. I have automated tons of stuff with this already. There is just so much you can do with it.

Stay tuned for more later…

Need to print shipping labels on your site?

Checkout my product RocketShipIt for simple easy-to-use developer tools for UPS™ FedEx™ USPS™ and more.

Get notified on new posts or other things I'm working on

Share: