...

Friday, February 26, 2010

AutoIt 2

For my example, I am going to create an auto-downloader for the Nokia Music service. To do this, I will need to send Enter once and Tab 5 times for a number of repetitions, with a delay between each repetition.
At the end of this article, you'll learn learn how to set focus to different windows, how to check for the window title, and how to send keystrokes to an application. You'll also learn the application and syntax of timers, for-loops and if-conditions.

Begin by creating a basic skeleton:
#include <GUIConstantsEx.au3>
#include <Timers.au3>

Dim $timerCountDown, $timerWindowCheck, $timerSendKeystrokes, $countCurrent, $repetitionsCurrent

Opt("GUIOnEventMode",1)
$gui=GUICreate("Nokia Music Automator",200,85)
GUISetOnEvent($GUI_EVENT_CLOSE,"guiClose")

GUICtrlCreateLabel("Repetitions:",5,5,55)
$repetitions=GUICtrlCreateInput("40",65,5,130)

GUICtrlCreateLabel("Delay (ms):",5,30,55)
$delay=GUICtrlCreateInput("15000",65,30,130)

$help=GUICtrlCreateButton("Help",5,55,90)
GUICtrlSetOnEvent($help,"helpClicked")

$start=GUICtrlCreateButton("Start",105,55,90)
GUICtrlSetOnEvent($start,"startClicked")

GUISetState(@SW_SHOW)

While 1
Sleep(1000)
WEnd

Func guiClose()
Exit
EndFunc

Func helpClicked()
MsgBox(0,"Help","Instructions:"&@CRLF&@CRLF&"1) Start Nokia Ovi Player"&@CRLF&"2) Go to the Download Page"&@CRLF&"3) Set the Necessary Parameters"&@CRLF&"4) Click on Start"&@CRLF&"5) Highlight the Download button"&@CRLF&@CRLF&"Note: To highlight the Download button - Hold your left mouse button on it, move the pointer away, then release. You should see a dotted box around it."&@CRLF&@CRLF&"By Syraxius (http://basicdraft.blogspot.com/)")
EndFunc

Func startClicked()
EndFunc

Func start()
EndFunc

Func stop()
EndFunc

Func timerCountDown($hWnd, $Msg, $iIDTimer, $dwTime)
#forceref $hWnd, $Msg, $iIDTimer, $dwTime
EndFunc

Func timerWindowCheck($hWnd, $Msg, $iIDTimer, $dwTime)
#forceref $hWnd, $Msg, $iIDTimer, $dwTime
EndFunc

Func timerSendKeystrokes($hWnd, $Msg, $iIDTimer, $dwTime)
#forceref $hWnd, $Msg, $iIDTimer, $dwTime
EndFunc




Notice that all the timer functions have 4 parameters and makes use of the #forceref directive. This is required for any functions used by timers.

When start is clicked, window should be focused to Nokia Ovi Player and the timerCountDown should be started:
WinActivate("Nokia Ovi Player")
$countCurrent=0
$timerCountDown=_Timer_SetTimer($gui,0,"timerCountDown")


In the timerCountDown function, it should increment $countCurrent by 1 each time, and make a beep:
_Timer_SetTimer($gui,1000,"timerCountDown",$timerCountDown)
If ($countCurrent<5) Then
$countCurrent+=1
Beep(5000/$countCurrent,100)
Else
stop()
start()
EndIf


Notice that I set the timer again. This is because the setting of the timer in the startClicked handler has the time set to 0 so that the method will be called immediately. The next calls will be 1s each.

Once the method is run 5 times, all timers are killed in stop(), and then the process is started with start().

In stop(), simply destroy all timers in $gui:
_Timer_KillAllTimers($gui)

In start(), set repetitionCount to 0 and start the timerWindowCheck and timerSendKeystrokes:
$repetitionsCurrent=0
$timerWindowCheck=_Timer_SetTimer($gui,0,"timerWindowCheck")
$timerSendKeystrokes=_Timer_SetTimer($gui,0,"timerSendKeystrokes")


In timerWindowCheck(), check for the active window and if it's not "Nokia Ovi Player", stop all processes and display a message:
_Timer_SetTimer($gui,1,"timerWindowCheck",$timerWindowCheck)
If Not (WinGetTitle("[active]")="Nokia Ovi Player") Then
stop()
MsgBox(16,"Halted","The downloading process has been halted because Nokia Ovi Player lost focus!")
EndIf


Notice that I used 16 for the flag. This is to make it appear as an error. Refer to here for flag codes.

In timerSendKeystroke(), check if $repetitionCurrent is larger than the value in $repetition. If it's not, continue on with the automation. If it is, stop all processes and display a message:
_Timer_SetTimer($gui,GUICtrlRead($delay),"timerSendKeystrokes", $timerSendKeystrokes)
If ($repetitionsCurrent<GUICtrlRead($repetitions)) Then
$repetitionsCurrent+=1
Send("{ENTER}{TAB}{TAB}{TAB}{TAB}{TAB}")
Else
stop()
MsgBox(64,"Completed","The downloading process has completed!")
EndIf


You should end up with this code:
#include <GUIConstantsEx.au3>
#include <Timers.au3>

Dim $timerCountDown, $timerWindowCheck, $timerSendKeystrokes, $countCurrent, $repetitionsCurrent

Opt("GUIOnEventMode",1)
$gui=GUICreate("Nokia Music Automator",200,85)
GUISetOnEvent($GUI_EVENT_CLOSE,"guiClose")

GUICtrlCreateLabel("Repetitions:",5,5,55)
$repetitions=GUICtrlCreateInput("40",65,5,130)

GUICtrlCreateLabel("Delay (ms):",5,30,55)
$delay=GUICtrlCreateInput("15000",65,30,130)

$help=GUICtrlCreateButton("Help",5,55,90)
GUICtrlSetOnEvent($help,"helpClicked")

$start=GUICtrlCreateButton("Start",105,55,90)
GUICtrlSetOnEvent($start,"startClicked")

GUISetState(@SW_SHOW)

While 1
Sleep(1000)
WEnd

Func guiClose()
Exit
EndFunc

Func helpClicked()
MsgBox(0,"Help","Instructions:"&@CRLF&@CRLF&"1) Start Nokia Ovi Player"&@CRLF&"2) Go to the Download Page"&@CRLF&"3) Set the Necessary Parameters"&@CRLF&"4) Click on Start"&@CRLF&"5) Highlight the Download button"&@CRLF&@CRLF&"Note: To highlight the Download button - Hold your left mouse button on it, move the pointer away, then release. You should see a dotted box around it."&@CRLF&@CRLF&"By Syraxius (http://basicdraft.blogspot.com/)")
EndFunc

Func startClicked()
WinActivate("Nokia Ovi Player")
$countCurrent=0
$timerCountDown=_Timer_SetTimer($gui,0,"timerCountDown")
EndFunc

Func start()
$repetitionsCurrent=0
$timerWindowCheck=_Timer_SetTimer($gui,0,"timerWindowCheck")
$timerSendKeystrokes=_Timer_SetTimer($gui,0,"timerSendKeystrokes")
EndFunc

Func stop()
_Timer_KillAllTimers($gui)
EndFunc

Func timerCountDown($hWnd, $Msg, $iIDTimer, $dwTime)
#forceref $hWnd, $Msg, $iIDTimer, $dwTime
_Timer_SetTimer($gui,1000,"timerCountDown",$timerCountDown)
If ($countCurrent<5) Then
$countCurrent+=1
Beep(5000/$countCurrent,100)
Else
stop()
start()
EndIf
EndFunc

Func timerWindowCheck($hWnd, $Msg, $iIDTimer, $dwTime)
#forceref $hWnd, $Msg, $iIDTimer, $dwTime
_Timer_SetTimer($gui,1,"timerWindowCheck",$timerWindowCheck)
If Not (WinGetTitle("[active]")="Nokia Ovi Player") Then
stop()
MsgBox(16,"Halted","The downloading process has been halted because Nokia Ovi Player lost focus!")
EndIf
EndFunc

Func timerSendKeystrokes($hWnd, $Msg, $iIDTimer, $dwTime)
#forceref $hWnd, $Msg, $iIDTimer, $dwTime
_Timer_SetTimer($gui,GUICtrlRead($delay),"timerSendKeystrokes", $timerSendKeystrokes)
If ($repetitionsCurrent<GUICtrlRead($repetitions)) Then
$repetitionsCurrent+=1
Send("{ENTER}{TAB}{TAB}{TAB}{TAB}{TAB}")
Else
stop()
MsgBox(64,"Completed","The downloading process has completed!")
EndIf
EndFunc

No comments :

Post a Comment

<