Some General Help For a New Learner :)

Asked by Mark Bergh

Hi Everyone, I just found out about Sikuli yesterday.... and it's AWESOME but I don't have a lot of experience with Python or any other programming language.... yet :)

I have a pretty simple thing to accomplish but I'm confused, all of this is so new to me. Basically this is what I want to do:

1. Look for an image and double click on it
2. Wait 20 seconds

(I would like steps 1 and 2 to repeat for 5 minutes)

That all seems pretty simple but I don't know how to loop it for 5 minutes.

I also want this to be running the whole time too:

3. If the image doesn't appear for more than 30 seconds, do another click and repeat from step 1

Can someone please show me what this code should look like? I would really appreciate it. I know this is probably a really dumb question but I just got into this yesterday. Thanks for your time

Question information

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

If I understood right:

import time

max = 5*60 # 5 minutes in seconds
start = time.time() # now
while (time.time() - start) < max: # loop 5 minutes
    if exists(<image>, 30): # wait max 30seconds for image
        click(getLastMatch()) # click it
        wait(20) # wait 20 secs and loop again
    else: # did not come within 30 seconds
        click(<image1>)
        # loop again

the <image...> have to be replaced by the respective shots.

Revision history for this message
Mark Bergh (beinthesun) said :
#2

Hi RaiMan, thanks for the quick answer :)

Basically I want the whole thing to last 5 minutes, even if the image doesn't come within 30 seconds. If I'm not mistaken, the way you explained it, the whole script would stop after the 30 second time out, right?

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

--- the whole script would stop after the 30 second time out, right?
no ;-)

This is a speciality of Python: indentation
Where other programming/scripting languages use some kind of bracketing Python uses indentation, to show, which of the following statements belong to a block like while, if, else def, class, .... (see faq 1800 to avoid traps for newbees)

so some more buzz with the above example:

max = 5*60 # 5 minutes in seconds
start = time.time(); # now
# loop:
while (time.time() - start) < max: # loop 5 minutes
    if exists(<image>, 30): # wait max 30seconds for image
        click(getLastMatch()) # click it
        wait(20) # wait 20 secs
    # end-if, skip else
    else: # did not come within 30 seconds
        click(<image1>)
    # end-else
    # end if-else
# end-while go back to loop
# AFTER while loop because same indent
print("you will see me after 5 minutes")

on looping see faq 1437

Revision history for this message
Mark Bergh (beinthesun) said :
#4

Thanks a lot. I got a long way to go. I appreciate your help :)