Monday, August 1, 2011

Getting Familiar with AutoIt3 Script Editor


In this tutorial, I will help you to get familiar with the script editor of AutoIt3, and its features that makes programming with AutoIt3 easier than any text editor. This script editor was part of the full installation package that we installed in the previous tutorial: Installing AutoIt3 Full Package. The script editor is called SciTE Script Editor, and you can find it as shown below. Click on its shortcut to run.


This is the editor window after we launched it.


The menu bar contains all the commands of the editor. You are not required to learn all the commands of the editor unless you are curious, but I will cover the commands that you will mostly use from each menu.

The most commands that you will mostly need in your development of AutoIt3 scripts are:
  • File Menu (Alt+F)
    • Create new script (Ctrl+N)
    • Open an existing script (Ctrl+O)
    • Save a script (Ctrl+S)
  • Edit Menu (Alt+E)
    • Undo an action (Ctrl+Z)
    • Redo an action (Ctrl+Y)
    • Cut, move code to the clipboard, and remove it from the code (Ctrl+X)
    • Copy, move code to the clipboard (Ctrl+C)
    • Paste, move code from the clipboard to the editor (Ctrl+V)
  • Search Menu (Alt+S)
    • Find a keyword or a phrase in the current selected script (Ctrl+F)
    • Replace a keyword or a phrase with a keyword or phrase in the current selected script (Ctrl+H)
  • View Menu (Alt+V)
    • Output window, it shows the of script compilation result and syntax check result (F8)
  • Tools Menu (Alt+T)
    • Compile the script, and produce an executable(.exe) file of the script, and save it in the directory where the original script is located (Ctrl+F7)
    • Go, or start executing the script (F5)
    • Stop Executing the script, and this command is mostly used in emergency cases when the script doesn't stop execution for some error in coding (Ctrl+Break)
  • Help Menu (Alt+H)
    • Help, it runs the help file and the documentation for AutoIt3 (F1)


The first important thing, when you open an AutoIt3 script editor to write a script is to save the script in your machine with (Ctrl+S) shortcut. There are some commands that can't be performed unless the script is saved first; such as Compile(Ctrl+F7) and Go(F5) commands


The title of the editor window gets updated and includes script name.


The commands in the Tools Menu related to building and running the script becomes active now, and ready to run.


Then, you can start writing your script. I will demonstrate the commands in the Tools Menu including Syntax Check, Go and Compile with a script sample. After we saved the script, let's write a script that displays a box that contains a message like (Hello World). This can be established with one line of code with using the AutoIt3 function MsgBox().

Once you start typing in AutoIt3 editor, a small menu will display that provides an auto-completion facility.


Once you choose the function you are looking for (in our case it's MsgBox() function), then open a left brace like ( , then a small box will display that tells you about the parameters that this function asks for in order to run properly, plus description of the function. In the MsgBox() function, the first parameter is the flag that determines the type of the box to display, the second parameter is the title of the box, the third parameter is the text of the box, and so on. Note that any parameters that are enclosed between square brackets [] are optional to provide for the function. The timeout determines the period for the message box to display, but it is optional to provide it, so it's not important to provide it.


Now, let's write the full statement that calls the MsgBox() function with providing some parameters to it.

MsgBox(0,"Message","Hello World")

In the editor, it will look like this:


Now, save the script with (Ctrl+S) shortcut, and let's run Syntax Check (Ctrl+F5) command to see if it has any syntax errors. The result of syntax check is as shown below that the script has 0 errors and 0 warnings
syntax errors are errors in the rules of AutoIt3 scripting langauge
warnings are not considered errors, but they can be considered as signs for errors during the execution of the script. If you know what you are doing, then warnings can be ignored


Let's try to create a syntax error in the code. For example, instead of calling MsgBox() function let's remove B from the function name, so it will look like Msgox() as the following.



Run Syntax Check (Ctrl+F5) and you will the following result that says that the script contains an error as denoted below.


If you double-click on the error text, you will see a yellow circle that points to the line where the corresponding error is located.


Now let's recover the statement as it was before:

MsgBox(0,"Message","Hello World")

After we checked that the script doesn't contain any errors, let's run it to see the result of our work. To run the script, run the Go command by pressing (F5) in the keyboard, or from the Tools Menu. The message box will display like the following one.


So far, our script is free or errors, and its execution is fine. Now, let try to compile it, and make an executable(.exe) version of it. For compiling a script, run Compile command by pressing (Ctrl+F7), or run it from the Tools Menu. The process of compilation will take a short delay to complete. The following image display the compilation result for our script.


After that, go to the directory where you saved the original script and you will find the executable version of it in the same directory. This executable can run on any Windows OS computer without the need for AutoIt3 to be installed.


There's another useful shortcut for creating this executable which is by right-clicking on the original script then choosing (Compile Script) from the popup menu. It will do the same job for creating the executable file of the original script as we did above.


Finally, let's have a look at the Help file of AutoIt3 script editor. You can run the Help file by pressing (F1), then the Help window will launch. Most frequently, you will use the Help file to know how to use a certain function, and how to call it. Simply, select the Index tab as shown below.


After you locate the function you are looking for, then double click on the function name.


The Help window will display the function reference where it has a full description about how to use it, starting with description about function's parameters.




At the end of the function reference, it will show you an example that contains a call for the function, and there's a button that will open the example script related to the targeted function in AutoIt3 script editor.

No comments:

Post a Comment