Loop Help (doens't loop)

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
NewYears1978
Posts: 20
Joined: 09 Jun 2016, 07:44

Loop Help (doens't loop)

03 Jun 2021, 12:42

I have this elaborate macro (well to me it is) that does a bunch of crap, then loops for set number of times.

The macro works fine, but when it hits the loop end it just stops. (referring to the main loop at Loop, %loopcount% (I have tried a variable and regular number here with same result)

Any ideas?

Code: Select all

InputBox, outputDate, My Input Box, What is the Campaign Date?
InputBox, outputSubject, My Input Box, What is the Email Subject?
InputBox, outputSubject2, My Input Box, What is the Second Subject?
InputBox, outputID, My Input Box, What is the ID number?
InputBox, loopcount, My Input Box, How many times to run?
Loop, %loopcount%
{
    MsgBox, 0, , Macro Start
    /*
    ChangeDateStart:
    */
    Send, {End}
    Sleep, 500
    Click, 470, 802 Left, 1
    Sleep, 10
    Send, {Control Down}{a}{Control Up}
    SendRaw, %outputdate%
    Sleep, 1000
    Click, 1903, 1080 Left, 1
    Sleep, 10
    Sleep, 1000
    Send, {Home}
    ImageLoopStart:
    CoordMode, Pixel, Client
    PixelSearch, FoundX, FoundY, 0, 0, 2048, 1152, 0x0078EF, 0, Fast RGB
    If (ErrorLevel)
    {
        Goto, ImageLoopStart
    }
    Else
    {
        Goto, ImageLoopEnd
    }
    ImageLoopEnd:
    Click, 775, 401 Left, 1
    Sleep, 10
    Send, {Control Down}{a}{Control Up}
    SendRaw, %outputSubject%  ; Change to Campaign Date 
    Click, 600, 463 Left, 1
    Sleep, 10
    Send, {Control Down}{a}{Control Up}
    SendRaw, %outputSubject2%  ; Change to Campaign Date 
    Click, 1084, 527 Left, 1
    Sleep, 10
    Send, {Backspace}
    SendRaw, %outputID%  ; Change to Campaign Date 
    Click, 1098, 652 Left, 1
    Sleep, 10
    Send, {Backspace}
    SendRaw, %outputID%  ; Change to Campaign Date 
    Click, 1904, 1076 Left, 1
    Sleep, 10
    Image2LoopStart:
    CoordMode, Pixel, Client
    PixelSearch, FoundX, FoundY, 1761, 1044, 2012, 1110, 0x041156, 0, Fast RGB
    If (ErrorLevel)
    {
        Goto, Image2LoopStart
    }
    Else
    {
        Goto, Image2LoopEnd
    }
    Image2LoopEnd:
    Send, {Control Down}{F4}{Control Up}
    /*
    Goto, ChangeDateStart
    */
}
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: Loop Help (doens't loop)

03 Jun 2021, 13:19

You are saying that the first MsgBox never appears, right?

Since the script does not work, you can debug by removing the loop command and most of the other commands. Start with the bare basics. Test line by line. It looks like your pixel searches could tie up the CPU in some infinite loops. What are the values of ErrorLevel in those searches?
NewYears1978
Posts: 20
Joined: 09 Jun 2016, 07:44

Re: Loop Help (doens't loop)

03 Jun 2021, 13:24

Well, the msg box does appear the first time, all the code runs and works fine, and then at the very end it just doesn't loop back to the start.

If I use a Label and a Goto instead it will keep going forever until I stop it just fine. So the overall code should be fine?

Those imageloops are just supposed to wait until the pixel is found before continuing, I couldn't figure out the "best way" to set that up or make it work so I used that sloppy label method, but there's no hang up there. (the errorlevel just checks if the pixel was found or not, if not it repeats the check, if so it moves past the check to the next bit of the code)

Sorry I am wordy :) There's no hang up in the actual macro I made it all in small portions for testing. It runs fine without a loop, or with a GoTo at the end back to start (the ones commented out).

Only when I stick the loop it it seems to just stop instead of loop. Thought maybe it was something simple but not sure =D

I mean it works if I just manually kill the macro when I want but I wanted to loop it set number of times.
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: Loop Help (doens't loop)

03 Jun 2021, 13:32

Within your loop, what is the last line that you are sure is reached, and what makes you sure of it? When you write, "it moves past the check", have you demonstrated, in some way, that the pixel was found? What is the ErrorLevel in the final pixel search?
Last edited by mikeyww on 03 Jun 2021, 13:36, edited 1 time in total.
NewYears1978
Posts: 20
Joined: 09 Jun 2016, 07:44

Re: Loop Help (doens't loop)

03 Jun 2021, 13:34

I know it reaches the end, the end has a CTRL-F4 which closes browser tab..which happens..and that's where it stops where it should loop back to the start.

The loop is the whole macro (except the input boxes at the start)

In other words if I put a msg box at the very end before the loop, it pops up and that's where the macro would stop. But if I put a Goto there and put it to a label at the start, that works.

It's just something broke in the loop.

As for the errorlevels I don't know how to answer that..I just use the code I found online

Code: Select all

    DClab_ImageS:  /*  <-- label */
    CoordMode, Pixel, Client
    PixelSearch, FoundX, FoundY, 0, 0, 2048, 1152, 0x0078EF, 0, Fast RGB
    If (ErrorLevel)
    {
        Goto, DClab_ImageS /* <---  pixel not found go to the first label */
    }
    Else
    {
        Goto, DClab_ImageE /* <--- pixel is found go to the bottom label and continue */
    }
    DClab_ImageE: <--- bottom label
Last edited by NewYears1978 on 03 Jun 2021, 13:40, edited 2 times in total.
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: Loop Help (doens't loop)

03 Jun 2021, 13:37

What is the value of loopcount?
How many times does the MsgBox appear?
What is the value of each ErrorLevel (which you can display)?
NewYears1978
Posts: 20
Joined: 09 Jun 2016, 07:44

Re: Loop Help (doens't loop)

03 Jun 2021, 13:41

I don't know what the value of errorlevel is, I thought that was just a true false flag (I am a newb) that you use with Ifs Loops, etc)

The %loopcount% is set by the input box at the start. However I changed that loop to just a number 2 or 3 or whatever and it still didn't loop (to eliminate variable issue)

I mean if I throw a msgbox at the end of the macro and display %ErrorLevel% I get 0...
in those image searches the only values are just 0 or 1 ..but there's no hang up on those like I said the whole macro plays fine, it just won't loop.


But I am a new so who knows what is going on..
Last edited by NewYears1978 on 03 Jun 2021, 13:44, edited 1 time in total.
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: Loop Help (doens't loop)

03 Jun 2021, 13:43

Since you do not know the value of ErrorLevel, you can add some MsgBox lines that will display the value of ErrorLevel after each PixelSearch.
NewYears1978
Posts: 20
Joined: 09 Jun 2016, 07:44

Re: Loop Help (doens't loop)

03 Jun 2021, 13:45

I did that in edited post above..but I am not sure why this matters? Unless I am misunderstanding how those ErrorLevels work.

I thought ErrorLevel was just a true false flag (0 or 1) and so it the image is there it does the else, if not it does the If..which is working fine.

My whole macro is playing far beyond those pixel searches so not sure why there would be a problem there?

I'll remove those and see if the macro loops.
NewYears1978
Posts: 20
Joined: 09 Jun 2016, 07:44

Re: Loop Help (doens't loop)

03 Jun 2021, 13:47

Okay I took out the image searches and just put a MsgBox with OK there, and it indeed will loop now.

Sooooo what is going on with the image searches? I probably set it up wrong, never used those before :)

Okay I got it now :)

I changed my image loops to this

Code: Select all

Loop
    {
        CoordMode, Pixel, Client
        PixelSearch, FoundX, FoundY, 0, 0, 2048, 1152, 0x0078EF, 0, Fast RGB
    }
    Until ErrorLevel = 0
    
Thanks for your help!
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: Loop Help (doens't loop)

03 Jun 2021, 13:55

The following sort of thing could work.

Code: Select all

CoordMode, Pixel, Client
Loop
{
 PixelSearch,,, 0, 0, 2048, 1152, 0x0078EF,, Fast RGB
 Sleep, 200 * ErrorLevel
} Until (ErrorLevel = 0)
Last edited by mikeyww on 03 Jun 2021, 13:57, edited 1 time in total.
NewYears1978
Posts: 20
Joined: 09 Jun 2016, 07:44

Re: Loop Help (doens't loop)

03 Jun 2021, 13:57

The pixel was definitely being found - because I was doing a specific test to see...
But not sure why it was still hanging up, but CPU use was going up and causing some crashing sooo.

New code I posted above works.

Thanks again.

Code: Select all

Loop
    {
        CoordMode, Pixel, Client
        PixelSearch, FoundX, FoundY, 0, 0, 2048, 1152, 0x0078EF, 0, Fast RGB
    }
    Until ErrorLevel = 0
    
User avatar
mikeyww
Posts: 27372
Joined: 09 Sep 2014, 18:38

Re: Loop Help (doens't loop)

03 Jun 2021, 13:57

I edited my last post. I suggest adding some sort of short sleep inside your loop so that your computer remains responsive.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], Mateusz53, peter_ahk, Pianist and 288 guests