AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

GoSub error

 
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help
View previous topic :: View next topic  
Author Message
lbrtdy



Joined: 13 Apr 2009
Posts: 101

PostPosted: Sun May 31, 2009 6:57 pm    Post subject: GoSub error Reply with quote

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
Back to top
View user's profile Send private message
Jex



Joined: 01 Aug 2008
Posts: 101

PostPosted: Sun May 31, 2009 8:15 pm    Post subject: Reply with quote

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
Back to top
View user's profile Send private message
lbrtdy



Joined: 13 Apr 2009
Posts: 101

PostPosted: Sun May 31, 2009 8:26 pm    Post subject: Reply with quote

ah I see, tyvm Very Happy
Back to top
View user's profile Send private message
Lexikos



Joined: 17 Oct 2006
Posts: 7293
Location: Australia

PostPosted: Mon Jun 01, 2009 1:40 am    Post subject: Reply with quote

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.
Back to top
View user's profile Send private message Visit poster's website
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Ask for Help All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group