ImageSearch and click with refreshing Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
GeminiContractor
Posts: 17
Joined: 03 Jul 2016, 10:30

ImageSearch and click with refreshing

Post by GeminiContractor » 28 Jan 2023, 00:36

I'm trying to make a script that checks for an image, if it finds it then clicks on the image, but if it doesn't for a certain amount of time, refreshes the page.
So far, the finding and clicking is fine, but I don't know how to get it to refresh after a certain amount of time not finding the image.

Current code:

Code: Select all

+x::
loop
{
CoordMode, Mouse, Screen
MouseGetPos, px,py
CoordMode Pixel
ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, C:\folder\image.png
MouseMove, FoundX, FoundY
if (ErrorLevel = 0)
{
MouseClick, left
}
else if(ErrorLevel = 1)
{
Send ^r
}
else
{
Send ^r
}
Sleep, 5000
Mousemove px,py, 1
Sleep, 5000
}

+c::ExitApp
Edit:
I can't really check it right now, but does this code look like it would work?

Code: Select all

+x:: ; Shift+X to start script loop.
loop
{
CoordMode, Mouse, Screen
MouseGetPos, px,py ; Saves current mouse position.
CoordMode Pixel  ; Interprets the coordinates below as relative to the screen rather than the active window.
ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, C:\folder\image.png ; Finds image from example image.
MouseMove, FoundX, FoundY ; Moves mouse to coordinates found in ImageSearch results.

errorCheck = 0 ; Variable to store failed image checks.

if (ErrorLevel = 0) ; What to do if image is found.
{
MouseClick, left
}
else if(ErrorLevel = 1) ; What to do if image is not found.
{
errorCheck = errorCheck + 1
Sleep, 5000 ; Stop script for 5 seconds.
}
else ; What to do if neither of the if statements cover what to do.
{
errorCheck = errorCheck + 1
Sleep, 5000 ; Stop script for 5 seconds.
}

if (errorCheck = 25) ; What to do if image checking failed 25 times 
{
Send ^r ; Refresh page.
}

Sleep, 5000 ; Stop script for 5 seconds.
Mousemove px,py, 1 ; Move mouse to previously saved position.
Sleep, 5000 ; Stop script for 5 seconds.
}

+c::ExitApp ; Shift+C to close script.

Rohwedder
Posts: 7614
Joined: 04 Jun 2014, 08:33
Location: Germany

Re: ImageSearch and click with refreshing  Topic is solved

Post by Rohwedder » 28 Jan 2023, 02:17

Hallo,
does this code (the 2nd) look like it would work?
Since this line is executed at each loop pass:errorCheck = 0 ; Variable to store failed image checks.
the condition:
if (errorCheck = 25) ; What to do if image checking failed 25 times
will never be fulfilled.
Try:

Code: Select all

+x:: ; Shift+X to start script loop.
SetTimer, Refresh_page, 125000 ; Timer 125 seconds 
loop
{
	CoordMode, Mouse, Screen
	MouseGetPos, px,py ; Saves current mouse position.
	CoordMode Pixel  ; Interprets the coordinates below as relative to the screen rather than the active window.
	ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, C:\folder\image.png ; Finds image from example image.
	MouseMove, FoundX, FoundY ; Moves mouse to coordinates found in ImageSearch results.
	if (ErrorLevel = 0) ; What to do if image is found.
	{
		MouseClick, left
		SetTimer, Refresh_page, On ; Timer is reset
	}
	else if(ErrorLevel = 1) ; What to do if image is not found		
		Sleep, 5000 ; Stop script for 5 seconds.
	else ; What to do if neither of the if statements cover what to do.
		Sleep, 5000 ; Stop script for 5 seconds.
	Sleep, 5000 ; Stop script for 5 seconds.
	Mousemove px,py, 1 ; Move mouse to previously saved position.
	Sleep, 5000 ; Stop script for 5 seconds.
}
Refresh_page: ; What to do if image checking failed 25 times
Send ^r ; Refresh page.
Return
+c::ExitApp ; Shift+C to close script.

GeminiContractor
Posts: 17
Joined: 03 Jul 2016, 10:30

Re: ImageSearch and click with refreshing

Post by GeminiContractor » 28 Jan 2023, 03:30

Rohwedder wrote:
28 Jan 2023, 02:17
Hallo,
does this code (the 2nd) look like it would work?
Since this line is executed at each loop pass:errorCheck = 0 ; Variable to store failed image checks.
the condition:
if (errorCheck = 25) ; What to do if image checking failed 25 times
will never be fulfilled.
Try:

Code: Select all

+x:: ; Shift+X to start script loop.
SetTimer, Refresh_page, 125000 ; Timer 125 seconds 
loop
{
	CoordMode, Mouse, Screen
	MouseGetPos, px,py ; Saves current mouse position.
	CoordMode Pixel  ; Interprets the coordinates below as relative to the screen rather than the active window.
	ImageSearch, FoundX, FoundY, 0, 0, A_ScreenWidth, A_ScreenHeight, C:\folder\image.png ; Finds image from example image.
	MouseMove, FoundX, FoundY ; Moves mouse to coordinates found in ImageSearch results.
	if (ErrorLevel = 0) ; What to do if image is found.
	{
		MouseClick, left
		SetTimer, Refresh_page, On ; Timer is reset
	}
	else if(ErrorLevel = 1) ; What to do if image is not found		
		Sleep, 5000 ; Stop script for 5 seconds.
	else ; What to do if neither of the if statements cover what to do.
		Sleep, 5000 ; Stop script for 5 seconds.
	Sleep, 5000 ; Stop script for 5 seconds.
	Mousemove px,py, 1 ; Move mouse to previously saved position.
	Sleep, 5000 ; Stop script for 5 seconds.
}
Refresh_page: ; What to do if image checking failed 25 times
Send ^r ; Refresh page.
Return
+c::ExitApp ; Shift+C to close script.
Thank you.
I'll try it.
I hadn't though of using a timer like that.
It's a lot better than measuring time with ErrorLevel.

Edit:
I'm not sure if it worked or not, as I can't tell if I ran into any errors (I should've used a log, but I didn't think to do that), but usually I do run into errors, so I'm guessing it did work.
Either way, the script seems to have gone through without issue, so thank you again.

Post Reply

Return to “Ask for Help (v1)”