Initiation to programming with AutoIt
[30 mn de lecture - paru le 11/5/2006 2:39:53 PM - Public : Confirmé]
|
   
|
Auteur
3 Example of a software conceived with AutoIt
We are
going to approach on this section the realization of a software step by
step, by leaning on a program which I developed for the necessities of
a company, and which will be available in downloading on the site
http://www.supinfo-projects.com :)
Called Supinfo Software
Installer (SSI), it is a software which automates the installation of
certain programs; these last ones can be chosen by the way of a graphic
interface (GUI).
It also integrates some small specific and very practical features which we shall discover according to the development.
We
are going to review the main features of the application code, and I
shall bring according to explanations on the met lines of codes. Please
also note that the code which will be presented here is voluntarily
deprived of comments, well to explanation on the technical part
itself; you will find complete code commented during the downloading of
the software in the part "Projet Chef d'oeuvre" of the site.
To begin, it is necessary to include the libraries program of which is going to need to comply.
|
#include< GUIConstants.au3 > #include< Process.au3 >
|
GUICONSTANTS --> Contains all the variable and the necessary functions to program a graphic interface.
Process --> Necessities for the execution of process in MS-DOS mode.
N.B: This library is include after AutoIt; you will as a general rule have no need to install them.
Once it makes, one can begin the statement of the variable. One begins naturally with the variable first of the program, which corresponds to the main window:
|
$mainwindow = GUICreate("Supinfo
Software Installer – Version 1.0", 320, 400)
|
One declares the variable mainwindow, and one uses the function
GUICreate which allows to create a window Windows. it takes in
parameter the name of the window, and the dimensions (height, width).
N.B:
There is for the AutoIt suite an editor of GUI who works like Visual
Studio of Microsoft (Koda Editor, relatively simple to use);
nevertheless, I decided for the learning of the language to code the
completeness of the graphic interface manually, this last one not being
moreover extremely complex.
Now our main window is
created, we are going to make the graphic interface with the elements
which are going to constitute it, being some totally optional.
Here
for example, one proceeds to the creation of Control Group, which
allows as a general rule to regroup elements (of the same nature for
example) in a window; in that case precise, it will be a question
especially of separating the menu of the rest of the application.
|
GuiCtrlCreateGroup("",
-1, -1, 325, 2)
|
Parameters here are: text (a title for example), the coordinates (horizontal and vertical), the width, and finally the height.
Separation being made, one can now pass in the elaboration of the menu of our application.
|
$filemenu = GUICtrlCreateMenu
("&Fichier")
$fileitem =
GUICtrlCreateMenuitem ("Quitter",$filemenu)
$helpmenu = GUICtrlCreateMenu ("?")
$infoitem = GUICtrlCreateMenuitem ("A
propos...",$helpmenu)
|
Every entity (menu or under menu) is represented with a variable; function GUICtrlCreateMenu allows the creation of the main menu (takes in parameter the name of the menu), whereas GUICtrlCeateMenuItem serves for creating an element of the menu (takes in parameter the name of under menu, followed by the name of the menu on which he depends).
Let us see adding how now an image to decorate the presentation of our application:
|
$logo = GUICtrlCreatePic("Files\_logo_.bmp",60,270,
200,90)
|
One begins as every time to declare a variable, to which one attributes the function GUICtrlCreatePic which allows to insert an image; it sets in parameter: the location of the file (as well as its extension), the coordinates (horizontal then vertical), and finally the dimensions.
One passes then in the creation of thumb indexes; indeed, application having different functions it seemed to me sensible to separate these last ones in various thumb indexes. Furthermore, it allows to have everything on an one and only window without for all that overloading it.
One begins by creating the variable of the "main" thumb index which, as for the menus, will contain the others variable representing the various thumb indexes.
|
$tab=GUICtrlCreateTab (10,15, 300,250)
|
One uses here the function GUICtrlCreateTab which allows to create a group of thumb indexes; it sets in parameter: coordinates in the window (horizontal then vertical), as well as the dimensions.
Then, come the creation of the others variable representing the various thumb indexes.
|
GUICtrlCreateGroup
("", 20, 60, 200, 150)
GUICtrlCreateLabel("Cochez les
applications que vous souhaitez installer :", 20, 45)
$avast
= GUICtrlCreateCheckbox("Avast", 40, 70, 60)
$acrobat
= GUICtrlCreateCheckbox("Acrobat", 40, 100, 60)
$diskeeper
= GUICtrlCreateCheckbox("DisKeeper", 40, 130, 80)
$winzip
= GUICtrlCreateCheckbox("WinZip", 120, 100, 70)
$windefender
= GUICtrlCreateCheckbox("WinDefender", 120, 130, 85)
GUICtrlCreateGroup("", -99,
-99,1,1) ;Fermeture du groupe
|
Here, we proceeds first of all to the creation of a "group" in a concern of aesthetics and organization of the application.
One uses then the function GUICtrlCreateLabel which allows to create a label (a field of text, as in C# Ndlr:); The parameters correspond simply following character to be registered between quotation marks, follow-up of the position of the label in the window.
Let us see right now the contents of the thumb index; this one should so present, besides the title ( the label) a group of checkbox indicating the softs that has it wishes to install in a automatic way. Every software (freewares only:d) will be so represented with a variable, which will give place to the creation of a checkbox on the window of the software itself.
Used instruction is GUICtrlCreateCheckBox (allows the creation of checkbox), in the following way:
$avast
= GUICtrlCreateCheckbox("Avast", 40, 70, 60)
Variable
Fonction de création Chaine
de Coordonnées &
des checkbox caractères dimensions
N.B: As in many others languages, indentation is recommended and even required during the development with AutoIt.
Finally, to confirm the boxes marked, we will use a button OK classic.
|
$okbutton1 = GUICtrlCreateButton("Lancer
l'installation", 40, 220, 150)
|
Here is coming the most important part of the program. Indeed, to there, we built only the graphic interface of our application; it is going now to need to attribute to every control the function for which it was created.
For it, we are going to adopt the following technique. We are going to create a buckle While associated to a variable to which one is going to attribute function GUIGetMsg(). This function allows in fact "to get back" in a permanent way the value of a variable, and according to this last one to allow to make an action.
|
While 1
$listener = GUIGetMsg()
|
For example here, one opens the buckle While While 1 and one creates then the variable $listener which uses function GUIGetMsg ().
The variable $listener will be so " listening " of what happens on the window. The least action (pressure of a button, coach of a compartment, etc. …) will be recorded and instructions will use its value to make suited operations.
We are going to begin by giving a function to the menu "Conveniently" (variable $infoitem); it is going to need, that as one click on this one, an applet opens and shows information about the software.
|
If $listener = $infoitem
Then Msgbox(0,"#### A Propos de Supinfo
Software Installer 1.0 ####", "SupInfo SoftWare InstalleR By
M.ROMANOS -- © Supinfo 2006 -- @ : maxime.romanos@supinfo.com")
EndIf
|
One uses for it a buckle If - Then; condition is the following one: if $listener sets the value of $infoitem (in other words, if a click occurs on $infotitem in A comment) then one proceeds to the posting of one message box (seen first) containing information about the application.

N.B: After every If-Then or If-Then-ElseIf, it is not necessary to forget to close the buckle due to the keyword EndIf.
One continues to use the variable $listener to listen to the other controls of the program; for example, here , for the checkbox of the applications which we first created, we are going to add a rule allowing to take into account the fact that an user is not marked any checkbox.
|
If $listener = $okbutton1 Then
If GUICtrlRead($avast)
= 4 And GUICtrlRead($acrobat) = 4 And
GUICtrlRead($diskeeper) = 4 And GUICtrlRead($winzip) = 4 And
GUICtrlRead
($windefender) = 4 Then
MsgBox(48, "Infos Supinfo Software
Installer", "Vous n'avez coché aucun
programme ! Choisissez un ou
plusieurs programmes à installer SVP.")
EndIf
EndIf
|
We use so again a buckle If; function GUICtrlRead allows to get back the values of a control (in this particular case here, the variable $nom_du_logiciel which represent each of the checkbox). The value of a checkbox can be or 0 (if it is marked) or 4 (if it is empty).
Our buckle allows so, in case all the variable have for value 4 (so all the checkbox is empty) to subject an error message to the user, without affecting the functioning of the program.

Let's see what about the automation of the installation of software packages. In fact, we are going to use AutoIt functions which allow the sending of precise "commands", according to several parameters which can be programmed.
|
If $listener = $okbutton1 Then
If
GUICtrlRead($avast) = 1 Then
Run("AVAST\avast_setupfre")
WinWaitActive("avast!
Antivirus Setup", "Bienvenue dans le programme")
Send("s")
WinWaitActive("avast!
Antivirus Setup", "Lisez moi")
Send("s")
WinWaitActive("avast!
Antivirus Setup", "&J'accepte")
Send("{LEFT}")
Send("s")
WinWaitActive("avast!
Antivirus Setup", "Destination")
Send("{ENTER}")
WinWaitActive("avast! Antivirus Setup",
"Configuration")
Send("{ENTER}")
WinWaitActive("avast! Antivirus Setup",
"Information sur l'installation")
Send("s")
WinWaitActive("Question",
"&Non")
Send("n")
WinWaitActive("avast! Antivirus Setup",
"Installation terminée")
Send("e")
Send("f")
Sleep(1000)
FileDelete(@DesktopCommonDir & "\avast! Antivirus.lnk")
Sleep(1000)
EndIf
|
Let us review so the set of new functions used in this piece of code:
- Run: this command allows to execute any file .exe, in the passer-by in parameter.
- WinWaitActive: the appearance of an active window allows " to wait " (that is in the foreground) to execute a precise action; sets in parameter the title of the window, may optionally of the present text in this last one.
N.B: It is recommended to use AutoIt Windows Info (to see Chapter 1) to find correct and complete information concerning a window.
- Send: feign the support of a touch or a combination of touchees on the keyboard; takes in parameter the touch or the continuation of touch which will be sent.
- Sleep: this function allows to place an injury time during the execution of the program; time or program will make anything is expressed in parameter in milliseconde.
- FileDelete: allows to delete a file; we cross the complete way and the name of the file in parameter.
To end program, it is necessary now to close the buckle While which one opened to the beginning; before it, one adds a condition for the lock of the application, as well as a window of thanking which appears during the closing.
|
ElseIf $listener = $GUI_EVENT_CLOSE Or
$listener = $fileitem Then
MsgBox(64,
"Fermeture de Supinfo Installer", "Merci
d'avoir utilisé Supinfo Installer... Pour toutes questions ou
remarques : m*****.r******@s******.com")
ExitLoop
EndIf
Wend
|
Given that we used a Yew first, new condition is going to be announced by means of ElseIf. One uses also always $listener to determine actions to make on the interface; in that case, one will evoke two possibilities for the lock of the application:
-Or by a click on the cross of lock of the window, which corresponds to the appeal of the function $GUI_EVENT_CLOSE.
-Either by a click on the variable $fileitem, or under menu "To leave" of the menu "File".
Then which follows allows to call up message box as we saw it first.
The lock is then executed due to the office ExitLoop, which orders the lock of the program. If follows by it EndIf which allows to put an end to the opening of the Yew at the beginning of code, and finally Wend who closes the main buckle While and closes definitively program.
N.B: You will have also noted the presence of the keyword Now which, as in many other languages, allows to specify a choice for the condition. The keyword And also exists, as well as many others ;)
A little screenshot to show final result:
 Here we are, and this is the end of our sequence of explanation of code. Meeting in some week to discover this tempting application, unless this article already allowed you to develop a similar of it …
|