PEscobar

How to restart a loop if there is an @error?

16 posts in this topic

So I made a loop beginning with while $e = 1. It opens a file and searches for a colour in the file, if the colour isn't there $e = 2 so the loop ends and restarts. Instead of that, the loop runs once and then just stops completely, even if the colour is there. 

Here is what it looks like basically.

While $e = 1 

    OpenPng()

      $aCoord = PixelSearch ($left, $top, $right, $bottom, 0x3B5E05, 0)
      If @error Then $e = 2

WEnd

How do I make it so the loop restarts if the colour isn't there, but it continues normally if the colour is there?

 

 

Share this post


Link to post
Share on other sites



That made it open the file again, but now it stops the rest of the script. There was more after it, I just shortened it.

Share this post


Link to post
Share on other sites

Move OpenPng() above the Do then

Do...Until will always execute everything between the Do and Until at least once, no matter what. Then it's going to test against the condition after Until (the Not @Error). If there was an error in the pixel search then it's going to continue to execute the pixelsearch until it doesn't fail. Once it succeeds it will do everything after the do...until

Share this post


Link to post
Share on other sites

I tried both of your solutions and it still only runs once. 

Share this post


Link to post
Share on other sites

My Crystal ball isn't telling me much so the information will have to come from you to understand what you are trying. ;)

Jos 


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

Local $aCoord
Local $sColor = 0x3B5E05
Local $left = 0, $top = 0, $right = @DesktopWidth, $bottom = @DesktopHeight
While 1
    $aCoord = PixelSearch($left, $top, $right, $bottom, $sColor)
    If @error Or Not IsArray($aCoord) Then ContinueLoop
;~  OpenPng()
;   MouseClick($aCoord[0], $aCoord[1])
;   Sleep(100)
WEnd
Edited by Trong

Regards,
 

Share this post


Link to post
Share on other sites

#9 ·  Posted (edited)

So the script opens a file and then searches for a colour. If the colour is there, then I want the loop to continue as normal, if the colour isn't there I want the loop to restart. I also want it to run more than once

Edited by PEscobar

Share this post


Link to post
Share on other sites

So the script opens a file and then searches for a colour. If the colour is there, then I want the loop to continue as normal, if the colour isn't there I want the loop to restart. I also want it to run more than once

These are words, not code, so open for interpretation.
Please post a code snippet that doesn't work yet and what the issue is.

Jos 


Visit the SciTE4AutoIt3 Download page for the latest versions        Beta files                                                          Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

While 1
    OpenPng()
    sleep(250)
    $coord1 = PixelSearch ($left, $top, $right, $bottom, 0x432D1E, 1)
    If @error Then ContinueLoop
    If not @error Then Selectgreen()
WEnd

It will open the picture and then select the colour once, but it doesn't keep doing it like a normal loop would. It only goes through it once.

Edited by PEscobar

Share this post


Link to post
Share on other sites

#12 ·  Posted (edited)

Opt("TrayAutoPause", 0)

Local $sColor = "0x3B5E05"
Local $pLeft = 0, $pTop = 0, $pRight = @DesktopWidth, $pBottom = @DesktopHeight
Local $sPNG = @ScriptDir & "\image.png"
HotKeySet("{ESC}", "_Exit")
_OpenPNG()
Local $aCoord
While 1
    ToolTip("")
;~  _OpenPNG()
    $aCoord = PixelSearch($pLeft, $pTop, $pRight, $pBottom, $sColor)
    If @error Or Not IsArray($aCoord) Then ContinueLoop
    ToolTip($sColor & " color found at position " & $aCoord[0] & " " & $aCoord[1])
    Sleep(250)
WEnd

Func _OpenPNG()
    ShellExecute($sPNG)
EndFunc   ;==>_OpenPNG

Func _Exit()
    Exit ToolTip("")
EndFunc   ;==>_Exit
Edited by Trong

Regards,
 

Share this post


Link to post
Share on other sites

@PEscobar ,

I am assuming that "Select" is your function. But SciTE is considering it as "Select" keyword.

 


My Contributions

UDF Link Viewer   --- A tool to visit the links of some most important UDFs 

 Includer_2  ----- A tool to type the #include statement automatically 

 Digits To Date  ----- date from 3 integer values

PrintList ----- prints arrays into console for testing.

 Alert  ------ An alternative for MsgBox 

 MousePosition ------- A simple tooltip display of mouse position

GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function

Access_UDF  -------- An UDF for working with access database files. (.*accdb only)

 

Share this post


Link to post
Share on other sites

Oops I copied that from an older version, I fixed that already and make it selectgreen()

Share this post


Link to post
Share on other sites

And you don't need to use 2 If statements. You can use ElseIf 

 


My Contributions

UDF Link Viewer   --- A tool to visit the links of some most important UDFs 

 Includer_2  ----- A tool to type the #include statement automatically 

 Digits To Date  ----- date from 3 integer values

PrintList ----- prints arrays into console for testing.

 Alert  ------ An alternative for MsgBox 

 MousePosition ------- A simple tooltip display of mouse position

GRM Helper -------- A littile tool to help writing code with GUIRegisterMsg function

Access_UDF  -------- An UDF for working with access database files. (.*accdb only)

 

Share this post


Link to post
Share on other sites

I found a simple solution.

I just put the search for the colour into the function "OpenPng()"

Then I put "If @error Then OpenPng()"

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now

  • Similar Content

    • TheDcoder
      By TheDcoder
      Hello, I recently opened a bug report without reading the Helpfile... My bad . After @Melba23's gentle reminder, I was curious about why it was like that.
      It is about SetError's behaviour. This is the example from the bug report:
      Example() If @error Then ConsoleWrite("Error" & @CRLF) Else ConsoleWrite("No Error" & @CRLF) EndIf Func Example() SetError(1) Sleep(1000) EndFunc What I tried to do is set Example's (my user defined function's) @error value to 1... but the value set by SetError is cleared after calling a function, I wonder why? Why should calling to an external function effect my function's @error which is set when my function returns.
      Setting the error of a UDF in advance by using SetError makes sense... but I cannot find a reason why calling a function should clear it? Please note that I am not talking about @error, I am talking about the @error set by my function when it ends/returns!
      I hope someone can enlighten me, thanks for the answers in advance!
      P.S I tried to explain my best but my English is not very good and I didn't feel like I did a good job explaining today, so please pardon any mistakes that I have made
    • Nareshm
      By Nareshm
      How to repeat my script when my control class does not match ?
       
      My Script : winActivate ( " PS :: Version - 2.2.0.0  - [PS Bill]")                   ControlClick ( "PS :: Version - 2.2.0.0  - [PS Bill]", "", "[NAME:btnSavebyFP]")                   ControlSend(" PS :: Version - 2.2.0.0  - [PS Bill]", "", "[NAME:btnSavebyFP]", "{enter}")                   WinActive ("Verification Number Entry ::")                   ControlClick("Next >", "","[NAME:btnNext]") ;if   >>>> Control <<<<  does not match repeat my script, ;if match exit script  

      Class:    WindowsForm734a
      Instance:    15
      ClassnameNN:    WindowsForm34a15
      Name:    BillPrint
      Advanced (Class):    [NAME:BillPrint]
      ID:    7875
      Text:    
      Position:    39, 310
      Size:    1221, 306
      ControlClick Coords:    745, 20
      Style:    0x56010000
      ExStyle:    0x00000000
      Handle:    0x00000000000C0468
    • SkysLastChance
      By SkysLastChance
      I am not sure why I am getting the this error on my second pass of the code.
      1 - $oWorkbook is not an object or not a workbook object
      Any help or advice on my code appreciated. 
      #include <Excel.au3> #include <Array.au3> #include <MsgBoxConstants.au3> Global $sExcelFile1 = FileOpenDialog("Choose/Create Excel File", @ScriptDir, "(*.xlsm)") Global $sExcelFile2 = FileOpenDialog("Choose/Create Excel File", @ScriptDir, "Excel Sheet (*.xlsx;*.xls)|All (*.*)") Global $vRow = 2 If FileExists($sExcelFile2) Then Global $oExcel2 = _Excel_Open () $oExcel2 = _Excel_BookOpen($oExcel2,$sExcelFile2) EndIF If FileExists($sExcelFile1) Then Global $oExcel1 = _Excel_Open () $oExcel1 = _Excel_BookOpen($oExcel1,$sExcelFile1,Default,Default,"2007") EndIF $oRead = _Excel_RangeRead ($oExcel2,"Untitled","A2",3) $oFind = _Excel_RangeFind ($oExcel1,$oRead,"E4:FD92",Default,$xlWhole) $Clip = _ArrayToClip($oFind,"",0,0,"",2,2) Send("{ScrollLock Off}") $hWnd = WinWait("[CLASS:XLMAIN]") ControlSend($hWnd, "", "", ("^g")) WinWait("[CLASS:bosa_sdm_XL9]") ; Go To ControlSend($hWnd, "", "", ("^v")) ControlSend($hWnd, "", "", ("{Enter}")) ControlSend($hWnD, "", "", "{Down " & $vRow & "}") Do $oTime = _Excel_RangeRead ($oExcel2,"Untitled","B2",3) If @error Then Exit MsgBox(0, "Error", "Error" & @CRLF & "@error = " & @error & ", @extended = " & @extended) MsgBox(0,"Test",$oTime) IF $oTime = "7:10:00 AM" Then $oCalls1 = _Excel_RangeRead ($oExcel2,Default,"C" & $vRow,3) $oCalls2 = _Excel_RangeRead ($oExcel2,Default,"D" & $vRow,3) ControlSend($hWnd, "", "", $oCalls1) ControlSend($hWnd, "", "", ("{RIGHT}")) ControlSend($hWnd, "", "", $oCalls2) $vRow = $vRow + 1 ContinueLoop Else $vRow = $vRow + 1 EndIf Until $vRow = 4 1.xlsm
      2.xlsx
    • IamKJ
      By IamKJ
      So I have a lot of different functions in the little program I'm making.  Most of them use the Do statement, such as finding images, clicking buttons, etc.
       
      The question I have is how would I be able to both
      1) Create a hotkey to pause whatever I am doing at the moment.  If I have to put something into every function in the program, I don't mind.
      2) Create a hotkey to exit whatever I am doing and stop all loops or commands and bring back the main GUI?
    • nlta1992
      By nlta1992
      I want creat GUI, have button 1 to 10, but only use loop for ... to ... step ... next.
      Thanks.