There is a more subtle problem with your code which is more obvious in Jex' example. If the code were to work, it would cause infinite recursion (leftover -> mainloop -> leftover -> mainloop etc.), inevitably resulting in the script crashing. In your actual script it is a bit different, since the ImageSearch can fail and stop the recursion.
Gosub should not be used to
return control flow back to another subroutine; that is what
Return is for. If you remove the line
Gosub, mainloop from leftover,
Return will
return control flow to the line after
Gosub, leftover. If you then remove
Break, it will continue back to the start of the loop.
Code:
mainloop:
Loop
{
ImageSearch, x, y, 566, 513, 635, 540, *10 %A_scriptDir%\Login Screenshots\ConfirmButton.jpg ;Searches for confirm button, left over from previous.
if errorlevel = 0 ;if confirm button is found (previous decapcha was wrong), it re-runs autocapcha.
{
Gosub, leftover
;Break
}
...
leftover:
...
;Gosub, mainloop
Return
You can also replace this:
Code:
;---AutoCapcha Loop----
RunWait, %A_ScriptDir%\clear_screenshots.bat
sleep 1000
WinActivate, ahk_class CLIENT
...etc.
Sleep, 4000
with this:
Code:
;---AutoCapcha Loop----
Gosub, leftover
Again, you can replace this:
Code:
Else if errorlevel = 0 ;found confirm button, auto decapcha can begin.
{
RunWait, %A_ScriptDir%\clear_screenshots.bat
sleep 1000
WinActivate, ahk_class CLIENT
...etc.
Sleep, 4000
}
with this:
Code:
Else if errorlevel = 0 ;found confirm button, auto decapcha can begin.
{
Gosub, leftover
}
The second Return is redundant:
Code:
Return
Return
The only way for control flow to reach the line after a Return is via some type of label (Gosub, Goto, hotkey, hotstring, etc.).
It probably won't help much, but you can shorten this:
Code:
if errorlevel = 2
{
Gosub, error2
Break
}
Else if errorlevel = 1 ;image not found, you could be logged in??
{
Gosub, error1
Break
}
else if errorlevel = 0
into this:
Code:
if ErrorLevel
{
Gosub, error%ErrorLevel%
Break
}
else
Now I see another pattern: each ImageSearch is dependent on the last, and if any of the last three fail, an error is shown and the loop breaks. With a bit of refactoring, we can shorten the script further. After my "tweaks", it looks like this:
Code:
if fullauto = 1 ;if its 1, means enter password & autocapcha, if 0, just enter username & password.
{
;---AutoCapcha Loop----
Gosub, leftover
;---------------Image Search------------------
;ImageSearch 0 = found, 1 = not found, 2 = error has occured
Loop
{
ImageSearch, x, y, 566, 513, 635, 540, *10 %A_scriptDir%\Login Screenshots\ConfirmButton.jpg ;Searches for confirm button, left over from previous.
if errorlevel = 0 ;if confirm button is found (previous decapcha was wrong), it re-runs autocapcha.
{
Gosub, leftover
}
else if errorlevel = 2 ;error has occured.
{
Gosub, error2
Break
}
Else ;if errorlevel = 1 ;Image not found (previous decapcha is good)
{
ImageSearch, x, y, 6, 73, 242, 118, *10 %A_scriptDir%\Login Screenshots\Connect Top.jpg ;Searches for the Connect Top
if ErrorLevel = 0 ;found top connect
{
sleep, 100
ImageSearch, x, y, 4, 695, 118, 728, *10 %A_scriptDir%\Login Screenshots\Press[connect].jpg
if ErrorLevel = 0 ;found Press [Connct]
{
Click, 460,580 ;Presses Connect Button
ImageSearch, x, y, 566, 513, 635, 540, *10 %A_scriptDir%\Login Screenshots\ConfirmButton.jpg
if ErrorLevel = 0
{
Gosub, leftover
Continue ; Don't gosub%ErrorLevel%.
}
}
}
; If we got to this point, one of the image searches failed.
Gosub, error%ErrorLevel%
Break
}
}
}
Return
;---Go Subs----
leftover:
RunWait, %A_ScriptDir%\clear_screenshots.bat
sleep 1000
WinActivate, ahk_class CLIENT
Send, {PrintScreen}
Sleep, 700
Runwait, %A_ScriptDir%\decode.bat
sleep, 1000
FileReadLine, code, %A_ScriptDir%\code.txt, 1
WinActivate, ahk_class CLIENT
click, 626, 492
Send, %code%
Send, {enter}
Sleep, 4000
Return
error1:
MsgBox, Image not found, or you logged In.
Return
error2:
MsgBox, Somewhere an error has occured.
Return
Of course, I'm unable to actually test the code.