random object selection

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
sloepoke
Posts: 3
Joined: 18 Feb 2018, 16:28

random object selection

18 Feb 2018, 17:07

I have been working on a script to test a webpage i'm making with multiple choices. Trying to get a simplified way to select 1 of 5 pictures to click on. I used if statements for all 5 but then doesn't transfer the mouse click action. Being a before noob scripter I fugure there has to be an easier way. Saw a post about setting a variable with the choices. Any help would be appreciated.

My crack coding is as follows:

random, cats, 1, 5
ToolTip choices is %cats%
sleep 10000
if %cats% = 1
{
category=pic1.png
}
else if %cats% = 2
{
category=pic2.png
}
else if %cats% = 3
{
category=pic3.png
}
else if %cats% = 4
{
category=pic4.png
}
else if %cats% = 5
{
category=pic5.png
}

ImageSearch, FoundX, FoundY, %winx%, %winy%, %width%, %height%,*n5, %category%
ToolTip, Category %category% found at %FoundX%, %FoundY%
sleep 10000
MouseClick, left, %FoundX%+15, %FoundY%+15,5
User avatar
boiler
Posts: 16951
Joined: 21 Dec 2014, 02:44

Re: random object selection

18 Feb 2018, 17:27

Remove the % signs for all your if statements.

Code: Select all

cats = 4
if %cats% = 4
	MsgBox, It works using percent signs
if cats = 4
	MsgBox, It works without percent signs
sloepoke
Posts: 3
Joined: 18 Feb 2018, 16:28

Re: random object selection

18 Feb 2018, 17:49

works now.. tyvm..
User avatar
Xtra
Posts: 2750
Joined: 02 Oct 2015, 12:15

Re: random object selection

18 Feb 2018, 20:00

You variation parameter is in the imagename parameter and the syntax is incorrect.
Its not even possible for it to work as written in your original post.
ImageSearch, FoundX, FoundY, %winx%, %winy%, %width%, %height%,*n5, %category%
Should be:
ImageSearch, FoundX, FoundY, %winx%, %winy%, %width%, %height%,*5 %category%
n is where the number goes for variation also this is put into the same parameter as the imagename.

Another issue to consider is you are not checking ErrorLevel of ImageSearch so it will always try to click even when the image is not found.

X1,Y1,X2,Y2 can be expression syntax so the percent signs arent needed there. (Its allowed to be used to comply with ahk v1)

Example of how you can optimize the script:

Code: Select all

random, cats, 1, 5
ToolTip choices is %cats%
sleep 10000
ImageSearch, FoundX, FoundY, winx, winy, width, height,*n5 pic%cats%.png
if (ErrorLevel = 0)
{
    ToolTip, Category %category% found at %FoundX%, %FoundY%
    sleep 10000
    MouseClick, left, %FoundX%+15, %FoundY%+15,5    
}
ToolTip
I would also suggest look up CoordMode in the docs.

HTH
sloepoke
Posts: 3
Joined: 18 Feb 2018, 16:28

Re: random object selection

20 Feb 2018, 16:53

Thanks for the reply XTRA.

the error checking was done prior to this part of the code. it checked for the first pic.. if it did not exists this was skipped. so it is assumed if the first pic exists the rest do also.. the changes to the syntax seem appropriate.. seeing how I don't claim to be nor want to be but am forced to be a programmer.. lol.. Again.. Thanks for the input..

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Descolada, Google [Bot], inseption86, mikeyww and 419 guests