AutoHotkey Community

It is currently May 26th, 2012, 7:14 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: GoSub error
PostPosted: May 31st, 2009, 7:57 pm 
Offline

Joined: April 13th, 2009, 3:15 am
Posts: 101
I keep getting this error "A goto/gosub must not jump into a block that doesn't enclose it." What does this mean? Here's part of the script here the error occurs, highlighted in red.


Code:
if fullauto = 1 ;if its 1, means enter password & autocapcha, if 0, just enter username & password.
{
   ;---AutoCapcha Loop----
   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
;---------------Image Search------------------
;ImageSearch 0 = found, 1 = not found, 2 = error has occured

   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
         }
         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 = 2
            {
               Gosub, error2
               Break
            }
            Else if errorlevel = 1 ;image not found, you could be logged in??
            {
               Gosub, error1
               Break
            }
            else 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 = 2
               {
                  Gosub, error2
                  Break
               }
               else if errorlevel = 1
               {
                  gosub, error1
                  Break
               }
               else 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 = 2
                  {
                     Gosub, error2
                     Break
                  }
                  if errorlevel = 1
                  {
                     Gosub, error1
                     Break
                  }
                  Else if errorlevel = 0 ;found confirm button, auto decapcha can begin.
                  {
                     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
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
Gosub, mainloop
Return

error1:
MsgBox, Image not found, or you logged In.
Return

error2:
MsgBox, Somewhere an error has occured.
Return


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 31st, 2009, 9:15 pm 
Offline

Joined: August 2nd, 2008, 12:31 am
Posts: 101
I think I've found your problem.

You can't put the label 'MainLoop' within an if statement.

Here's a simple example of what your doing:

Code:
Var = 1

;Problematic piece statrs
if (var == 1)
{
   ; here you put your label, it won't work. AHK doesn't like this.
   MainLoop:
      Gosub Leftover
   Msgbox, hi

}
return
;end of problem


LeftOver:
   Gosub Mainloop
Return

_________________
Woot.

Please read forum etiquette


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: May 31st, 2009, 9:26 pm 
Offline

Joined: April 13th, 2009, 3:15 am
Posts: 101
ah I see, tyvm :D


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 1st, 2009, 2:40 am 
Offline

Joined: October 17th, 2006, 4:15 pm
Posts: 7502
Location: Australia
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.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: Alpha Bravo, bobbysoon, BrandonHotkey, Cephei1, joetazz, Miguel, rbrtryn, SKAN, Xx7 and 74 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group