click image onAppear in region

Asked by Shimizoki

I wrote a simple script to click an image in a region when it appears... at first it was working well, then it started breaking. I am not sure what I did to break it.

[CODE]
Settings.MinSimilarity = .99
Settings.MoveMouseDelay=0
RegionImg.onAppear(img, click(img))
Region(437,419,687,303).observe(25)

#other Image I will add later (img1)
#another image (img2)
#another image (img3)
#another image (img4)
#another image (img5)
[/CODE]

Anywhere where it says img is an actual image being displayed in Sikuli.
The reason MinSimilarity is .99 is because all the images possible have the same shape, just different coloring. Using .9 led to misclicks.

It keeps telling me there is an error in line 3, but I don't see what I have done wrong. (it only tells me there is an error there when MinSimilarity is > .9)

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
Last query:
Last reply:
Revision history for this message
Shimizoki (shimizoki) said :
#1

I have noticed that if the image is there then it does not cause an error... however if the image is not there, it throws an error even if the 25 seconds have not yet passed.

Revision history for this message
Best RaiMan (raimund-hocke) said :
#2

the usage

RegionImg.onAppear(img, click(img))

is not according to the feature definition of observe()

look: http://sikuli.org/docx/region.html#observing-visual-events-in-a-region

You have to move the click() action to the event handler function that has to be specified as the second parameter of onXXX() and onXXX() and observe() have to use the same region object.

your case e.g.:

with Region(437,419,687,303):
    onAppear(img, handler)
    observe(25)

But why not simply use:

with Region(437,419,687,303):
    wait(img, 25)
    click(getLastMatch())

so you do not need the complex construct of observe().

If you want to handle the notFound error, you might use exists() instead of wait():

with Region(437,419,687,303):
    if not exists(img, 25):
        print img, "not Found"
        exit(1)
    click(getLastMatch())

Revision history for this message
Shimizoki (shimizoki) said :
#3

well that would be because I obviously have no idea what I'm doing :P

I read through the docs but was having some difficulty navigating it. I'll have to do some more reading.
When I used the onAppear(img, handler) the program would not run, nor output any errors. At least incorrectly I got some use out of it.

Revision history for this message
Shimizoki (shimizoki) said :
#4

Thanks RaiMan, that solved my question.

Revision history for this message
RaiMan (raimund-hocke) said :
#5

You are right: what is missing is some tutorial, that leads a new bee into the magics of Sikuli scripting step by step.

Nevertheless, asking on this board is the second best, since you normally get helpful answers ;-)

To feed your curiosity, why your onAppear() solution worked without complaining:
1- since the syntactical rules where followed, the script worked
2- onAppear() does not do any action, it only registers an event that has to be observed later on for the specified region
3- since you used two different regions with onAppear() and observe(), the appear event was never observed and the observe had nothing to do, since no event was registered for this region.
4- the click(img) (2nd parameter of onAppear()) was evaluated at runtime, and did a click, if the image was there (target might not be clickable, so at least the mouse should have moved) or interrupted the script (FindFailed) if not

The last point is one thing, that one can complain against Sikuli: onAppend() and the other onXXX() should check, wether the second parameter is a valid handler reference and stop the script with an explaining error if not.

So come back whenever you need it:
There are no stupid questions - only stupid answers ;-9