Error with loop using while not .inside().find():

Asked by dpoon2000

I tried a routine similar to the "Desktop Surveilance" tutorial but it's not working. I am getting the error below. I have tried this on 2 XP laptops and a 10.6.2 SL iMac and they all have the same problem. I am able to run simple script that uses switchApp, type, wait etc. But as soon as I use "while..." in my script, this error occur. Can someone help?

[sikuli] Stopped
[sikuli] An error occurs at line 6
[sikuli] Error message:
Traceback (most recent call last):
  File "C:\DOCUME~1\Daniel\LOCALS~1\Temp\sikuli-tmp1261069811330106710.py", line 6, in <module>
    while not Region(597,523,223,80).inside().find("1275888980218.png"):
  File "C:\Program Files\Sikuli\sikuli-script.jar\Lib\sikuli\Region.py", line 59, in find
  Line 8, in file C:\DOCUME~1\Daniel\LOCALS~1\Temp\sikuli-tmp1261069811330106710.py

 at edu.mit.csail.uid.Region.wait(Region.java:240)
 at org.python.proxies.sikuli.Region$Region$1.super__wait(Unknown Source)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
 at java.lang.reflect.Method.invoke(Unknown Source)

edu.mit.csail.uid.FindFailed: FindFailed: 1275888980218.png can't be found.
  Line 8, in file C:\DOCUME~1\Daniel\LOCALS~1\Temp\sikuli-tmp1261069811330106710.py

Question information

Language:
English Edit question
Status:
Answered
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
RaiMan (raimund-hocke) said :
#1

You should read through the documentation: http://sikuli.org/trac/wiki/reference-0.10
at least through the intros of the topics summarized under 2.1

while not Region(597,523,223,80).inside().find("1275888980218.png"):

will never work in the standard configuration, since the fact, that the picture is NOT found is not reported back. Instead the script stops with a FindFailed exception.

As a convenience there is the method exists(), that returns None, if the image is not there, otherwise the match object, without raising the exception.

So use:

while not Region(597,523,223,80).exists("1275888980218.png"):

and this will loop, until the image is found.

If this is a waiting-loop, you may save processing time, by saying something like this:

while True:
   if Region(597,523,223,80).exists("1275888980218.png", 0):
      break
   wait(30)

this would look once (0 as timing parameter) every 30 seconds for the image and end the loop if it's there.

Revision history for this message
dpoon2000 (danielpmpoon3) said :
#2

But I am still getting an error. Somehow I need another loop for matching the pattern. But when it finds the pattern or evaulating it I get an error. Here's the new code:

while True:
   if Region(573,470,312,198).exists("1275939229187.png"):
  if find(Pattern("1275939229187.png").similar(0.94)):
   popup("Found it");
  wait(0.2);
   wait(0.2);

Here's the error:

[sikuli] Error message:
Traceback (most recent call last):
  File "C:\DOCUME~1\Daniel\LOCALS~1\Temp\sikuli-tmp8551897776798660874.py", line 7, in <module>
    if find(Pattern("1275939229187.png").similar(0.94)):
  File "C:\Program Files\Sikuli\sikuli-script.jar\Lib\sikuli\Region.py", line 59, in find
  Line 5, in file C:\DOCUME~1\Daniel\LOCALS~1\Temp\sikuli-tmp8551897776798660874.py

 at edu.mit.csail.uid.Region.wait(Region.java:240)

 at org.python.proxies.sikuli.Region$Region$1.super__wait(Unknown Source)

 at sun.reflect.GeneratedMethodAccessor23.invoke(Unknown Source)

 at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

 at java.lang.reflect.Method.invoke(Unknown Source)

edu.mit.csail.uid.FindFailed: FindFailed: Pattern("1275939229187.png").similar(0.94) can't be found.
  Line 5, in file C:\DOCUME~1\Daniel\LOCALS~1\Temp\sikuli-tmp8551897776798660874.py

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

again: if a find is not successful, the script will stop with an exception FindFailed. A failing find will only return something if you switch off exception handling. So "if find()" will not work.

I don't understand, what you want to do. What kind of programming have you done before? Know some Python?

The indentation is not readable. The loop will only end when an error like FindFailed occurs

What are the two wait(0.2) for?

Why are you trying to find (if find()) an image, that you know is there (if exists())?

Only a few lines, but many questions.

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

I set this to answered, because the same question is again posted.

Can you help with this problem?

Provide an answer of your own, or ask dpoon2000 for more information if necessary.

To post a message you must log in.