...

Friday, February 26, 2010

AutoIt 1

There are many times where you need to do automation. Tasks like farming in games, or downloading large batches of files are always repetitive and boring. Thus comes the need for automation.
We will now look at AutoIt today. AutoIt is a scripting engine that features a language similar to BASIC. AutoIt allows creation of GUI driven interface. You can run scripts on the fly on systems with AutoIt installed, or create executables (*.exe) to be run elsewhere.

We will begin by creating a basic script that shows a message box. AutoIt syntax is very similar to BASIC, and the logic is as simple as C. To show a message box, create a blank script and type:
MsgBox(0,"Hello World!","How are you today?")

There is no need for a semicolon to terminate. Semicolons are comments (similar to // in C).

If you see a pop-up, you've successfully created your first AutoIt program. We'll begin constructing the GUI. The GUI is very simple, and the buttons are event driven. We'll deal with the events later on.

Before we deal with the advanced stuff, let's create a basic GUI that has a label, a textfield, and a button. Delete the message box first. We then import the GUI library:
#include <GUIConstantsEx.au3>

Next, we create the main panel:
GUICreate("GUI Example",200,100)

We then populate the GUI with buttons and a field:
GUICtrlCreateLabel("User Input:",5,5,80)
$input = GUICtrlCreateInput("",100,5,95)

$button = GUICtrlCreateButton("Read",5,30,190)


Notice that I assigned the creation of the input and button into their respective variables. This is so that we can do referencing to it later on.

Then we'll make the GUI visible:
GUISetState(@SW_SHOW)

If you try running this right now, you will see nothing. This is because when the program is executed to the bottom, it automatically exits. We want to pause this for a while to take a look at our preliminary GUI design, so we'll do a delay through:
Sleep(5000)

The whole code will look like this:
#include <GUIConstantsEx.au3>

GUICreate("GUI Example",200,60)

GUICtrlCreateLabel("User Input:",5,5,80)
$input = GUICtrlCreateInput("",100,5,95)

$button = GUICtrlCreateButton("Read",5,30,190)

GUISetState(@SW_SHOW)

Sleep(5000)




Right now our window will appear for 5 seconds then disappear. Looks pretty good right now, but the buttons do nothing at all! Right now, we'll start adding in the events.

We'll need to allow events to occur:
Opt("GUIOnEventMode",1)

This is typically put on the top of the program right after the include.

Next, we'll create a function to exit. This will be our event handler:
Func guiClose()
Exit
EndFunc

We then assign this to the GUI's event listener:
GUISetOnEvent($GUI_EVENT_CLOSE,"guiClose")

I typically put this right below the GUI creation line. $GUI_EVENT_CLOSE is the event triggered when you press the close button. Right now the program exits when you click the close button, but it still closes after 5 seconds. We fix this by replacing the Sleep(5000) with an endless loop:
While (1)
Sleep(1000)
WEnd


Your code should look like this now:
#include <GUIConstantsEx.au3>
Opt("GUIOnEventMode",1)
GUICreate("GUI Example",200,60)
GUISetOnEvent($GUI_EVENT_CLOSE,"guiClose")

GUICtrlCreateLabel("User Input:",5,5,80)
$input = GUICtrlCreateInput("",100,5,95)

$button = GUICtrlCreateButton("Read",5,30,190)

GUISetState(@SW_SHOW)

While (1)
Sleep(1000)
WEnd

Func guiClose()
Exit
EndFunc


Right now the close button works, but not the Read button. What do we do now? Of course, we create an event handler for the read button as well. I'll do an empty one first:
Func readClicked()
EndFunc


Then we assign it to the listener (type this right below the button creation):
GUICtrlSetOnEvent($button,"readClicked")

You can also assign it to the input's handler so that when the user hits ENTER, it will call the handler:
GUICtrlSetOnEvent($input,"readClicked")

Right now the button calls the handler, but it's empty. We'll start filling in the handler. Remember the message box command we used before? We'll use it here:
MsgBox(0,"Read","User Input is """&GUICtrlRead($input)&"""")

Let's break down what's written in there. To print double quotes, we must type them in a pair like this "". Next, the value inside $input is concatenated with the sentence. Finally, a double quote is concatenated at the end of the line.

The final completed code should look like:
#include <GUIConstantsEx.au3>
Opt("GUIOnEventMode",1)
GUICreate("GUI Example",200,60)
GUISetOnEvent($GUI_EVENT_CLOSE,"guiClose")

GUICtrlCreateLabel("User Input:",5,5,80)
$input = GUICtrlCreateInput("",100,5,95)
GUICtrlSetOnEvent($input,"readClicked")

$button = GUICtrlCreateButton("Read",5,30,190)
GUICtrlSetOnEvent($button,"readClicked")

GUISetState(@SW_SHOW)

While (1)
Sleep(1000)
WEnd

Func guiClose()
Exit
EndFunc

Func readClicked()
MsgBox(0,"Read","User Input is """&GUICtrlRead($input)&"""")
EndFunc




When you run the program, you should be able to see exactly what you typed into the user input field when you press Read.

3 comments :

  1. Just stumbled upon AutoIT today, I found this a great help. There is a lot of information on AutoIT but it is tricky to find.

    Is it wrong that I have no desire to use this language to automate tasks in other windows applications, but rather develop my own?

    ReplyDelete
  2. You can actually make applications with this, but you would be limited in terms of available libraries and community support. What kind of application do you have in mind? :D

    ReplyDelete
  3. I used it once to create a custom front end for a very specific database that had multiple data/file formats.

    ReplyDelete

<