Saturday, August 6, 2011

Designing AutoLogger with AutoIt3


AutoIt Website: https://www.autoitscript.com

As I explained that AutoIt3 scripting language can be used to automate many tasks on your Windows computers. A simple example of an automated task is an auto-login. Sometimes you feel lazy entering your username and password to log-in to Windows Live Messenger, Google Talk, or Skype. This tutorial will help you to make an auto-login script for Google Talk using AutoIt3. The same procedure can be followed for other chatting programs with few code modifications. Before we start, I assume that you at least skimmed through the first four AutoIt3 tutorials. If you didn't, please visit the page of AutoIt3 Tutorials.

Analyzing Auto-login Steps

Before we start designing our script, let's analyze the steps that we follow to log-in to our Google Talk account or any other chatting application.
1. We double-click on the program icon on the desktop,
or from the start menu:

2. After that, the Google Talk window will launch as the following, and you will see the cursor already placed in the username field, so you can start typing.


3. Then, you will start typing your username, then you will press [Tab] key on the keyboard, then you will type your password, and finally you will hit [Enter] key to submit.


Design Auto-login script

Now is the time to automate this simple task with AutoIt3 script. After you downloaded the full installation of AutoIt3, use the integrated editor of AutoIt3 that comes with it
The editor window will show, and now it's the time to start writing some code.
First, we will design the script using the AutoIt3 editor, then we will transform it into a standalone executable (.exe) file
1. Since the script will contain sensitive information such as password, let's predefine a constant that will hold the value of our password for authentication purpose (you don't want someone to come and run the application and get access to your email account, right?). Note that you are free to choose any password you like. I'm making it 1234 for simplicity. Defining a constant in AutoIt3 is like the following:
2. Next, we need to prompt the user to enter the password in order to let the script do the auto-login task. We will ask the user through calling InputBox() function in AutoIt3. The submitted password from the user will be saved into a variable called $password. The statement will look like:
3. After getting the password from the user, then we compare it with the one that we predefined as a constant in the first statement in order to let the script continue its execution. The comparison between the entered password and predefined password is is by inserting an if statement.
Note: @error is set to 1 when the user clicks Cancel and doesn't enter any password
4. Now comes the important part. Now, we want to run Google Talk or your favorite chatting program. Before this, let's try to locate the executable(.exe) file of our chatting program.
For locating the target executable file of the chatting program, we right-click on its shortcut, then choose properties as the following image explains:




After knowing the exact location, then we use it as parameter for the Run() function in AutoIt3 and call it like this:
Const $original_pass = "1234"
$password = InputBox("Authentication","Enter Password","","*",200,100)
If $password = $original_pass Then
&nbsp&nbsp&nbsp&nbspRun("C:\Program Files\Google\Google Talk\googletalk.exe")
Else
&nbsp&nbsp&nbsp&nbspif @error <> 1 then
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspMsgBox(0,"Error","The password you entered is not correct")
&nbsp&nbsp&nbsp&nbspEndIf
EndIf
5. At this point, Google Talk window will display, but we need to tell the script to wait for the Google Talk window to be ready to enter the username and password of the user. To do this, we instruct the script by calling WinWaitActive() function and provide the window title which is "Google Talk" in our case. The tutorial Getting Familiar with AutoIt3 Window Info Tool describes a tool that provides you with helpful information about programs windows.

Const $original_pass = "1234"
$password = InputBox("Authentication","Enter Password","","*",200,100)
If $password = $original_pass Then
&nbsp&nbsp&nbsp&nbspRun("C:\Program Files\Google\Google Talk\googletalk.exe")
&nbsp&nbsp&nbsp&nbspWinWaitActive("Google Talk")
Else
&nbsp&nbsp&nbsp&nbspif @error <> 1 then
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspMsgBox(0,"Error","The password you entered is not correct")
&nbsp&nbsp&nbsp&nbspEndIf
EndIf
6. The next step is to send both the username and password and submit them to login. We establish this by calling the Send() function in AutoIt3.

  • First, we will send the username (e.g. spongebob)
  • Then, we will send [Tab] to move to the password field
  • After that, we will send the password (e.g. mypassword)
  • Finally, we will send [Enter] to submit the information

From the four steps above, you may think that we will call the Send() function four times, but all these four steps can be done with one call of the function Send() as in the following:
If $password = $original_pass Then
&nbsp&nbsp&nbsp&nbspRun("C:\Program Files\Google\Google Talk\googletalk.exe")
&nbsp&nbsp&nbsp&nbspWinWaitActive("Google Talk")
&nbsp&nbsp&nbsp&nbspSend("spongebob{Tab}mypassword{Enter}")
Else
&nbsp&nbsp&nbsp&nbspif @error <> 1 then
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspMsgBox(0,"Error","The password you entered is not correct")
&nbsp&nbsp&nbsp&nbspEndIf
EndIf
Note that you need to replace "spongebob" with your real username and "mypassword" with your real password in the above labeled statement. Also, note that sending Tab and Enter keystrokes are indicated with {Tab} and {Enter} patterns.
Sometimes the password might contain special characters that need to be sent in a different format (e.g. Send({#}) or Send({^})). For this check this page that has documentation for the Send() function, and how to send special characters keystrokes.
The full view of the script is like the following:
Const $original_pass = "1234"
$password = InputBox("Authentication","Enter Password","","*",200,100)
If $password = $original_pass Then
&nbsp&nbsp&nbsp&nbspRun("C:\Program Files\Google\Google Talk\googletalk.exe")
&nbsp&nbsp&nbsp&nbspWinWaitActive("Google Talk")
&nbsp&nbsp&nbsp&nbspSend("spongebob{Tab}mypassword{Enter}")
Else
&nbsp&nbsp&nbsp&nbspif @error <> 1 then
&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbspMsgBox(0,"Error","The password you entered is not correct")
&nbsp&nbsp&nbsp&nbspEndIf
EndIf
Now, Save (Ctrl+S) the script into a location on your computer that you know.

Converting Auto-login Script into Executable(.exe) file

For converting our auto-login script to a standalone executable (.exe) file that can run on any Windows OS computer, please check this short tutorial (Getting Familiar with AutoIt3 Window Info Tool) that helps you in doing so.

Recommendations:

1. Delete the script that you designed at the beginning because anyone can open and steal your username and password, since it's written in plain text.
2. For the application we designed, make sure that you have chosen a strong password at the first statement above and you are able to remember it.
3. Make sure that you are signed out from your chatting application because we didn't consider this case in our script (I don't think you will run the program if you are signed in).
If you have any questions regarding this tutorial, please feel free to ask. Also, I welcome any idea for automating computer tasks, and I'm ready to help for achieving it
Still, there are many thing to automate. Take Care.

No comments:

Post a Comment