Shorten my script Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
Khalilo91
Posts: 2
Joined: 27 Nov 2022, 15:12

Shorten my script

Post by Khalilo91 » 27 Nov 2022, 18:21

Hello, I made this script and it is 100% working as I wanted it to be. However, I have used a long way which ended up with 1900 line code. I know that it is doable with like a 20-30 line if not less.
My code functions like this: Image search for 16 different pictures in a specific area in the 1st line( in a table) > if any image is found > do a function > go to 2nd line and do image search for same 16 pics and etc. It also would go to 2nd line even if images weren't found.
I have 7 lines, 16 image, and 1 image search function that would repeat with every image found or image not found in each line.

Code: Select all

Line1:
CoordMode, Pixel
ImageSearch, FoundX, FoundY, 2506, 428, 2554, 464, *100 img1.png
if (ErrorLevel = 0){
    Click %FoundX%, %FoundY%. 
    RandomSleep(88,111)
	Click 2533, 1230
	RandomSleep(88,111)
	Send {Enter}
}
CoordMode, Pixel
ImageSearch, FoundX, FoundY, 1881, 875, 2011, 916, *100 Buynow.png
if (ErrorLevel = 0){
    Click %FoundX%, %FoundY%.
    RandomSleep(200,300)
    Send, {Enter}
    Sleep 9000
}
ImageSearch, FoundX, FoundY, 2506, 428, 2554, 464, *100 img2.png
if (ErrorLevel = 0){
    Click %FoundX%, %FoundY%. 
    RandomSleep(88,111)
	Click 2533, 1230
	RandomSleep(88,111)
	Send {Enter}
}
CoordMode, Pixel
ImageSearch, FoundX, FoundY, 1881, 875, 2011, 916, *100 Buynow.png
if (ErrorLevel = 0){
    Click %FoundX%, %FoundY%.
    RandomSleep(200,300)
    Send, {Enter}
    Sleep 9000
}
ImageSearch, FoundX, FoundY, 2506, 428, 2554, 464, *100 img3.png
if (ErrorLevel = 0){
    Click %FoundX%, %FoundY%. 
    RandomSleep(88,111)
	Click 2533, 1230
	RandomSleep(88,111)
	Send {Enter}
}
CoordMode, Pixel
ImageSearch, FoundX, FoundY, 1881, 875, 2011, 916, *100 Buynow.png
if (ErrorLevel = 0){
    Click %FoundX%, %FoundY%.
    RandomSleep(200,300)
    Send, {Enter}
    Sleep 9000
}
As you see in the code, the only thing changing is the img#.png and the search area. I only listed the script up to img3.png as it just repeats till 16. then Line2, Line3, Line 4.. etc after which also has same thing except for img name and search area cords.

What I am looking for is something that would make it search a whole folder for 16 image instead of adding a a script for each image. and since the lines are parallel and search area in each line is parallel also, a code like add this number to Line1 to make line 2, 3, 4, 5 cords and make it shorter.

I would appreciate the help if possible, and hope I explained what I wish to have clearly
thanx

User avatar
mikeyww
Posts: 26601
Joined: 09 Sep 2014, 18:38

Re: Shorten my script  Topic is solved

Post by mikeyww » 27 Nov 2022, 19:15

Welcome to this AutoHotkey forum!

Code: Select all

#SingleInstance Force
dir = %A_ScriptDir%\t
CoordMode, Pixel
CoordMode, Mouse
Loop, Files, %dir%\*.*
 n++
Loop, Files, %dir%\*.*
{
 Sleep, 50
 ToolTip, %A_Index%/%n%: %A_LoopFileName%
 ImageSearch, x, y, 2506, 428, 2554, 464, *100 %A_LoopFilePath%
 If !ErrorLevel {
  MouseClick,, x, y
  RandomSleep(88, 111)
  Click, 2533 1230
  RandomSleep(88, 111)
  Send {Enter}
 }
 ImageSearch, x, y, 1881, 875, 2011, 916, *100 %dir%\Buynow.png
 If !ErrorLevel {
  MouseClick,, x, y
  RandomSleep(200, 300)
  Send {Enter}
  Sleep, 9000
 }
}
ToolTip
MsgBox, 64, Done, Done!

RandomSleep(min, max) {
 Random, rnd, min, max
 Sleep, rnd
}

Khalilo91
Posts: 2
Joined: 27 Nov 2022, 15:12

Re: Shorten my script

Post by Khalilo91 » 27 Nov 2022, 21:46

Thank you, worked perfectly

Post Reply

Return to “Ask for Help (v1)”