IDE: with: selects whole screen instead of specified region on rerun --- workaround

Asked by Nidere

**** workaround

--- do not use with:, when running script in IDE more than once after IDE start .-- use the region.method() syntax instead

--- use command line to run script

---------------------------------------------------------

Hello.
Here's my code:
cs9867.vkontakte.ru/u3380791/97737138/z_16ff18ac.jpg
Sorry for its stupidity - I'm only studying.
Thing is, I'm trying to achieve random clicks in the area of buttons I need to push and I get it, BUT program starts clicking at random places of the screen every several minutes.
All patterns were checked not to be mismatched.
What can cause such a thing?
Please, ask for any additional information to provide, I just dunno what can be helpful.
And thanks in advance!

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
RaiMan (raimund-hocke) said :
#1

pls. send me the script and screenshots showing the buttons zipped together and send it to my mail at https://launchpad.net/~raimund-hocke

I have to check some things in your code.

Revision history for this message
Nidere (nidere) said :
#2

Sent the email.
Also, I ried to re-write the same cosw today and bow it wont run at all!
Simple "if exists ("pattern"):" command now gives an error:

 File "C:\Users\Nidere\AppData\Local\Temp\sikuli-tmp7954504748688119487.py", line 4, in
 while exists(Pattern("AmyMP-2.png").similar(0.98)):
AttributeError: 'str' object has no attribute '__exit__'

Is this a bug, or am I combpletely dumb? :(

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

I will have a look at it.

--- Is this a bug, or am I combpletely dumb? :(
surely not dumb, but may be a bug ;-)

That's a problem, that I already doubted: a problem with the implementation of with:

I will come back.

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

this is a bug --- see reedited question above

Revision history for this message
Nidere (nidere) said :
#5

Oh, GREAT thanks for your help - your explanations are awesome!

One more question if you please, regarding the same script - sometimes
it returns stops with an error on the 10th line - "click(m1)". Can't
provide exact text of the error, because it appears really randomly.
It says something about "can't find picture", although there is
definitely the targeted picture on the screen.
This is the reason I tried to use exceptions handling in that place,
unsuccessfully, though)
Do you have any ideas, what can cause such error? And if not, is there
any way to force the script running forward without stopping on the
error?

2011/11/13 RaiMan <email address hidden>:
> Your question #178538 on Sikuli changed:
> https://answers.launchpad.net/sikuli/+question/178538
>
> Summary changed to:
> IDE: with: selects whole screen instead of specified region on rerun --- workaround
>
> Description changed to:
> **** workaround
>
> --- do not use with:, when running script in IDE more than once after
> IDE start .-- use the region.method() syntax instead
>
> --- use command line to run script
>
> ---------------------------------------------------------
>
> Hello.
> Here's my code:
> cs9867.vkontakte.ru/u3380791/97737138/z_16ff18ac.jpg
> Sorry for its stupidity - I'm only studying.
> Thing is, I'm trying to achieve random clicks in the area of buttons I need to push and I get it, BUT program starts clicking at random places of the screen every several minutes.
> All patterns were checked not to be mismatched.
> What can cause such a thing?
> Please, ask for any additional information to provide, I just dunno what can be helpful.
> And thanks in advance!
>
> --
> You received this question notification because you asked the question.
>

Revision history for this message
Nidere (nidere) said :
#6

Thanks RaiMan, that solved my question.

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

-- error with click(m1)

Your usage at this point in your script is

m1 = capture(x,y,w,h)
click(m1)

the rectangle (x,y,w,h) is internally captured and the image is stored in a temporary file. Then in preparation of the click, this image is searched and should be found, since it was captured nearly in the same moment.

My first idea: Since you are using this in an endless loop, there might be a problem with the generation of temporary filenames and or files, when this process is repeated too often and too fast.
But testing this scenario, I did not notice any problems (on my Mac): 50 repetitions of capture/find and the similarity score was always 1.0.
So this might not cause the problem.

Another more possible reason: you are randomizing width and height for the captured rectangle. I did not check this calculations. Since the search is done in a small region afterwards, it might be, that there are cases, where part of the image is outside of the region, which will produce a FindFailed exception.
So check your randomizing bounds. The captured images must be inside the restricting region in all cases.

-- is there any way to force the script running forward without stopping on the error?
generally the only way is to use try:/except:

try:
    print "some code, that might crash"
    1 / 0
except:
    print "code crashed, but it does not matter"
print "script is still running"

In the special case of FindFailed exceptions in Sikuli scripts, there are some options, how to handle these situations generally by using options: http://sikuli.org/docx/region.html#exception-findfailed

Revision history for this message
Nidere (nidere) said :
#8

Again, great thanks, your code works perfectly last 12 hours without stopping :)