"While not" loop failing

Asked by shears

This is my script, very simple:

while not find( ):
   sleep(5)
switchApp("Firefox.app")
click( )
sleep(1)
find( )
click( )

However the script fails at line 1:
capture: java.awt.Rectangle[x=0,y=0,width=1440,height=900]
0 matches found
capture: java.awt.Rectangle[x=0,y=0,width=1440,height=900]
0 matches found
capture: java.awt.Rectangle[x=0,y=0,width=1440,height=900]
0 matches found
[sikuli] [Error] source lineNo: 1
[sikuli] [Error] Traceback (innermost last):
  File "/var/folders/6e/6eRH52OiE6SYqDRJm1xypE+++TI/-Tmp-/sikuli-tmp7468072292170430654.py", line 1, in ?
  File "/Applications/Utilities/Sikuli-IDE.app/Contents/Resources/Java/sikuli-script.jar/Lib/python/edu/mit/csail/uid/Sikuli.py", line 363, in find
  Line 1, in file /var/folders/6e/6eRH52OiE6SYqDRJm1xypE+++TI/-Tmp-/sikuli-tmp7468072292170430654.py

 at edu.mit.csail.uid.SikuliScript.wait(SikuliScript.java:209)
 at edu.mit.csail.uid.SikuliScript.find(SikuliScript.java:178)
 at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
 at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
 at java.lang.reflect.Method.invoke(Method.java:597)

edu.mit.csail.uid.FindFailed: FindFailed: 1270768938536.png can't be found.
  Line 1, in file /var/folders/6e/6eRH52OiE6SYqDRJm1xypE+++TI/-Tmp-/sikuli-tmp7468072292170430654.py

The .py file mentioned says:
wait("1270767315064.png", timeout=0)
switchApp("Firefox.app")
click("1270767315064.png")

What is wrong? I'm using Sikuli 0.9.9 on OSX 10.6.3

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

the .py, you are running and that produces the error FindFailed is not the script, you mention in the beginning (1270768938536.png fails and that is not mentioned in the .py).

But thats not the problem:

With version 0.9.9, by default, a find not found throws exception FindFailed and ends the script. So your script will never do what you want: wait until "your whatever" appears and then shoot :-((

how to:
setThrowException(False) # we want to go on after find failed
while True: # would run forever, if target never comes up ! (try shift-cmd-c to kill it)
   if wait("image you are waiting for", 5000) # 5 seconds max, then returns None
      break
setThrowException(True)
# now you can shoot

If you want it limited:
setThrowException(False)
gotYou = False
for i in range(10): # would anyway stop after about 1 minute
   if wait("image you are waiting for", 5000)
      gotYou = True
      break
setThrowException(True)
if not gotYou:
   popup("No success, sorry")
   raise "No success, sorry" # to stop the drama here
# now you can shoot

I'm a snow leo too ;-) - have fun!

Revision history for this message
shears (differenxe) said :
#2

Thank you for the reply. I want the script to want indefinitely, so i tried the first code you posted. If i understood correctly, I should make my code like this:

setThrowException(False)
while True:
    if wait( , 5000):
      break
setThrowException(True)
switchApp("Firefox.app")
click( )
sleep(2)
find( )
click( )

But when it completes the whole procedure it does not loop to the beginning, it just stops.

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

ok, so if this is your workflow that should be repeated for ever, you have to add an outer loop.

while True:
  setThrowException(False)
  while True:
    if wait( , 5000):
      break
  setThrowException(True)
  switchApp("Firefox.app")
  click( )
  sleep(2)
  find( )
  click( )

or again a limited version:

for i in range(aNumber): # instead of the first while True:

Revision history for this message
Michael Oman (av36mo) said :
#4

I think this bug is still in 0.10.1!

I was just testing Sikuli out so I made this:
while not find( ):
     sleep(1)
popup("I found Messenger!")

Indeed, it fails to run indefinitely. So, what's the proper way to make it loop forever—unless Messenger is found of course?

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

With 0.10.1 the right way to do things like this is:

while not exists(image, 0):
    wait(1)
popup()

You may look at the "Complete guide to Sikuli" for more information:
- exists() does not stop the script with a FindFailed
- with the ... ,0 you tell exists() to only look once (standard is some tries within 3 seconds)
- so with wait(time) you can control the timing

an alternative may be to check out the new observer feature.

Can you help with this problem?

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

To post a message you must log in.