[HowTo] make a simple loop with "exists image" and "click image"? (Sikuli)

Asked by David Borghi on 2018-10-19

Hello!

I don't know how to make a simple loop that lasts forever.

The script to be looped is this one:

exists("1539962756733.png")
click("1539962769464.png")

I just want it to loop if the conditions exist.

I already read some questions about it but still couldn't figure out how to do it.

Also, is it possible to make a script that is just activated in case another condition is false?

Example:

If "x" image exists don't do nothing.
If "x" image doesn't exist, run the script.

Thank you for all the help!

Question information

Language:
English Edit question
Status:
Solved
For:
Sikuli Edit question
Assignee:
No assignee Edit question
Last query:
2018-11-14
Last reply:
2018-11-15

This question was reopened

RaiMan (raimund-hocke) said : #1

If you want to do a little bit more complex things (loops, decisions, functions, ...) you need some basic knowledge of Python.
There are very good tutorials available in the net.

--- A very good and straight forward tutorial on Python language
http://www.tutorialspoint.com/python/index.htm

--- These faqs may help in your special case:
faq 1437: [HowTo] repeat something in a Sikuli script (make loops)
faq 1800: Indentation --- the secrets behind and how to recover

David Borghi (davidborghi) said : #2

Hello, RaiMan. :)

Thank you so much for your attention and for being so fast on the response!

I already tried both tutorials but still couldn't figure out. I mean, I've tried to adapt those examples to my "flow" (?) but It just doesn't go right.

Would you mind answering some questions so I can know If my projects are viable or not?

If so...

1. Which is better and which option will give less delay: running two scripts separately (is it possible) or running one script with all conditions?

2. Is it possible to make a condition where the script would only be activated in case "x" image exists?

The script I'm running is really simple, It consists:

exists("1539972965985.png", FOREVER)
exists("1539963812444.png", FOREVER)
doubleClick("1539967125264.png")

I have two scripts that are exactly the same, the only things are different, are the images, would be possible to combine them in a single script without one being negatively affected by the other? I mean, without the script pressing two buttons when he should just press one?

I'm sorry for all the confusion and for all the inconvenience.
I'm not trying to be a lazy person, I swear I'm not, I just couldn't understand from the other examples.

Thank you for all your time and your patient.

David Borghi (davidborghi) said : #3

Thanks RaiMan, that solved my question.

David Borghi (davidborghi) said : #4

Hello, RaiMan.

I'm sorry for all the inconvenience.

I've managed (I don't know) to keep it in looping just using while function:

while exists("1540053824433-3.png", FOREVER):
      exists("1540099469526.png", FOREVER)
      doubleClick("1540102517023.png")
      wait (10)

After that, I tried to find and learn how to make two scripts run into one, but I still don't know.

Both scripts are exactly the same, the only different thing is the images.

The most important condition is the line one, I'd like to make the script to opt for 1 of both scripts in case line 2 of one of both exists.

Is that possible? If I was not clear enough, please, let me know so I can share the pictures.

Thank you for all your help!
I'm sorry for all the inconvenience.

David Borghi (davidborghi) said : #5

are the images*

RaiMan (raimund-hocke) said : #6

Try to talk about your intention more natural like so (just track what you would do manually):

workflow 1:
when I see image 1
then i wait for image 2
when I see image 2
then I click on image 3
then I wait 10 seconds

and write down every possible variant from top to bottom

David Borghi (davidborghi) said : #7

Hello, RaiMan.

Thank you again for your fast response.

What I was expecting:

when I see image 1
then I wait for image 1.1 or 1.2
If I see image 1.1 click on image 1.1.1
If I see image 1.2 click on image 1.2.1
then I wait 10 seconds

For now, I was able to put both scripts to work in a single script.

The problem is, one script can be executed only If the other was previously executed.

Translating, If script 1 was executed, It'll be possible to execute it again only if script 2 was executed as well.

So, it's happening in this way:

script 1
script 2
script 1
script 2

How the workflow was written:

while exists("1540053824433-3.png", FOREVER):
      exists("1540099469526.png", FOREVER)
      doubleClick("1540102517023.png")
      wait (10)
      exists("1540053824433-3.png", FOREVER)
      exists("1540069005561.png", FOREVER)
      doubleClick("1540102829602.png")
      wait (10)

Would it be necessary to "separate" both scripts to avoid the "rule" of script 1/2/1/2?

I'm sorry If I was not clear enough, If possible, please, let me know If I can describe in a better way.

Thank you for all your help and your attention.
I'm sorry for all the inconvenience.

RaiMan (raimund-hocke) said : #8

To better see, what you are doing, you should name your image human readable:

while exists(“img1", FOREVER):
     exists(“img11", FOREVER)
     doubleClick(“img111")
     wait (10)
     exists(“img1", FOREVER)
     exists(“img12", FOREVER)
     doubleClick(“img121")
     wait (10)

if I understand right:

step2 = True # step1 should run the first time
while exists("img1", FOREVER):
# wait for img1 and then

  while True: #wait for img11 or img12 FOREVER

    # this is step1
    if step2 and exists("img11", 0): # check for img11
      # only do that when starting and then
      # on repeat only if step2 was run before
      doubleClick("img111")
      step2 = False # step2 must be executed

    # this is step2
    if exists("img12", 0): # check for img11
      doubleClick("img121")
      step2 = True

  wait (10) # wait and then repeat

> On 22. Oct 2018, at 19:12, David Borghi <email address hidden> wrote:
>
> Question #675373 on Sikuli changed:
> https://nam05.safelinks.protection.outlook.com/?url=https%3A%2F%2Fanswers.launchpad.net%2Fsikuli%2F%2Bquestion%2F675373&amp;data=02%7C01%7C%7C98c309c733e841df121608d6384198f5%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636758251717378370&amp;sdata=su%2BIAAZoOF%2BXSqmSABGvLDLTxNoqe7IM8cmnMRsNBd0%3D&amp;reserved=0
>
> Status: Answered => Open
>
> David Borghi is still having a problem:
> Hello, RaiMan.
>
> Thank you again for your fast response.
>
> What I was expecting:
>
> when I see image 1
> then I wait for image 1.1 or 1.2
> If I see image 1.1 click on image 1.1.1
> If I see image 1.2 click on image 1.2.1
> then I wait 10 seconds
>
> For now, I was able to put both scripts to work in a single script.
>
> The problem is, one script can be executed only If the other was
> previously executed.
>
> Translating, If script 1 was executed, It'll be possible to execute it
> again only if script 2 was executed as well.
>
> So, it's happening in this way:
>
> script 1
> script 2
> script 1
> script 2
>
> How the workflow was written:
>
> while exists("1540053824433-3.png", FOREVER):
> exists("1540099469526.png", FOREVER)
> doubleClick("1540102517023.png")
> wait (10)
> exists("1540053824433-3.png", FOREVER)
> exists("1540069005561.png", FOREVER)
> doubleClick("1540102829602.png")
> wait (10)
>
> Would it be necessary to "separate" both scripts to avoid the "rule" of
> script 1/2/1/2?
>
> I'm sorry If I was not clear enough, If possible, please, let me know If
> I can describe in a better way.
>
> Thank you for all your help and your attention.
> I'm sorry for all the inconvenience.
>
> --
> You received this question notification because your team Sikuli Drivers
> is an answer contact for Sikuli.
>
> _______________________________________________
> Mailing list: https://nam05.safelinks.protection.outlook.com/?url=https:%2F%2Flaunchpad.net%2F~sikuli-driver&amp;data=02%7C01%7C%7C98c309c733e841df121608d6384198f5%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636758251717378370&amp;sdata=UUYuEaI%2BIArxcqzsg6krfYkr2UrMwQyi8H2kp7WCnVo%3D&amp;reserved=0
> Post to : <email address hidden>
> Unsubscribe : https://nam05.safelinks.protection.outlook.com/?url=https:%2F%2Flaunchpad.net%2F~sikuli-driver&amp;data=02%7C01%7C%7C98c309c733e841df121608d6384198f5%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636758251717378370&amp;sdata=UUYuEaI%2BIArxcqzsg6krfYkr2UrMwQyi8H2kp7WCnVo%3D&amp;reserved=0
> More help : https://nam05.safelinks.protection.outlook.com/?url=https%3A%2F%2Fhelp.launchpad.net%2FListHelp&amp;data=02%7C01%7C%7C98c309c733e841df121608d6384198f5%7C84df9e7fe9f640afb435aaaaaaaaaaaa%7C1%7C0%7C636758251717378370&amp;sdata=2vd8NdRCclXoDhsSNoM4E1hj2A021jPBSosDcqbGRyo%3D&amp;reserved=0

David Borghi (davidborghi) said : #9

Hello, RaiMan.

I'm sorry for bothering you too much with my problems.

I don't know If I already expressed my gratitude enough for your creation, anyway, have in mind I'm grateful for that, It's an awesome tool that I know It has been helping a lot of people. THANK YOU!

Now, back to the code.

I'm sorry that I forgot to rename the images to make it readable.

About the code you suggested, I found two problems:

1. It's still only working interleaved.
2. In the step2, instead of just one doubleClick, the code keeps sending doubleClicks until the image disappears.

I'll try to explain in a clear way what I'm trying to achieve.

The most important condition is the existence of "img1". (If It doesn't appear, do nothing.)

Whenever "img1" exists, I'd like the code to check If either "img11" or "img12" is on the screen.

If "img11" appears, then doubleClick "img111".
If "img12" appears, then doubleClick "img121".

(Both lines can only be activated If the "img1" is on the screen. Most part of the time the "img1" will be on the screen, It'll only disappear when one of both lines were executed.)

I'm sorry If I was not clear enough, If possible, please, let me know If I can describe in a better way.

Thank you so much for your help and your attention.
I'm sorry for all the inconvenience.

RaiMan (raimund-hocke) said : #10

a variant based on what I understood from comment #9

while True:
  if exists("img1", 0):
    while True:
      #wait for img11 and then
      #look for img11 or img12 FOREVER

      # this is step1
      if exists("img1", 0) and exists("img11", 0): # check for img11
        doubleClick("img111")
        while exists("img11", 0):
          wait(0.3) # wait for img11 to vanish

      # this is step2
      if exists("img1", 0) and exists("img12", 0): # check for img11
        doubleClick("img121")
        while exists("img12", 0):
          wait(0.3) # wait for img12 to vanish

      if not exists("img1"):
        break # when img1 vanishes, end the inner loop

  # img1 not there
  wait (10) # wait and then repeat

the job:
when img1 exists
   when img11 is there
     doubleclick and wait for img11 to vanish
   when img12 is there
     doubleclick and wait for img12 to vanish
   repeat until img1 vanishes

"the job" is repeated every 10 seconds

David Borghi (davidborghi) said : #11

Hello, RaiMan.

Thank you so much for your effort, patience, and your kindness.

So far, I think It's perfectly working.

I know I already bothered you too much, I'm sorry for that, If you still have some free time and don't mind helping me a bit more, could you answer If I would be able to increment other functions in the code without making it too slow?

Let's take step 1 as an example:

# this is step1
      if exists("img1", 0) and exists("img11", 0): # check for img11
        doubleClick("img111")
        while exists("img11", 0):
          wait(0.3) # wait for img11 to vanish

Would be possible to add something like:

after doubleclick, check if the img1 (for 3 seconds) is still there, If it's true, then doubleclick img2.

I tried implementing it but I failed as It isn't obeying the sequential structure.

Again, I'm sorry for all the time you have spent with my problems. It's not fair nor right.

I apologize I couldn't implement it by myself but I swear I'm really trying.

Thank you for all your kindness and effort.
I'm sorry for all the inconvenience.

RaiMan (raimund-hocke) said : #12

--- check if the img1 (for 3 seconds) is still there
directly after the doubleclick or after img111 has disappeared?

BTW: since it is my personal decision, to help or not, just concentrate on writing down your request as precise as possible.

... this would have been enough:
So far, I think It's perfectly working.

Let's take step 1 as an example:

# this is step1
      if exists("img1", 0) and exists("img11", 0): # check for img11
        doubleClick("img111")
        while exists("img11", 0):
          wait(0.3) # wait for img11 to vanish

Would be possible to add something like:

after doubleclick, check if the img1 (for 3 seconds) is still there, If it's true, then doubleclick img2.

David Borghi (davidborghi) said : #13

That's fine, I'm sorry for all the needless information.

Q: directly after the doubleclick or after img111 has disappeared?
A: Directly after the doubleclick as the img111 is always on the screen.

So, right after (3 seconds later) the doubleclick, If the img1 didn't disappear, I would like to force another doubleclick in img2.

(The img2 hasn't been mentioned before because I was trying to implement only a basic execution.)

David Borghi (davidborghi) said : #14

Hello, RaiMan.

I'm sorry for the inconvenience, I'm not pressuring you, I understand that you'll answer as soon as you can (and just If you can), I'm just providing more information.

[step0]
open the program and log in 07:55 a.m (I've already created a script to do it, but I'm trying to figure out how to insert it in the same script.)

click("img0")
wait(20)
click("img01")
click("img02")
type("password")
click("img03")

Usually the follow script will end in the line "4" or line "10", but I'd like it to react in case anything goes wrong, I mean, If what I'm expecting doesn't happen, so it can't act like If I was in front of computer.

Do you think is it possible?

I tried several times to implement it, but I guess I'm failing in "closing" the sequences, as most of the time when I make something right, the other thing is ignored, like If It isn't even written.

What I'm trying to implement:
(* = lines to be added)

1 when img1 exists

[step1]
4 when img11 is there
5 doubleclick and wait for img11 to vanish
6 *if img1 didn't disappear (in 3 seconds) doubleclick img2 (If this is true, the loop must restart as It canceled the line above)
7 *If img3 exists and img31 exists
8 *doubleclick img2 and doubleclick img111

[step2]
11 when img12 is there
12 doubleclick and wait for img12 to vanish
13 *if img1 didn't disappear (in 3 seconds) doubleclick img2 (If this is true, the loop must restart as It canceled the line above)
14 *If img3 exists and img31 exists
15 *doubleclick img2 and doubleclick img111

17 repeat until img1 vanishes

Thank you for your help and your attention.
I'm sorry for all the inconvenience and any typo.

RaiMan (raimund-hocke) said : #15

sorry, missed your comment #13 and adding idea for #14

# this is step1
      if exists("img1", 0) and exists("img11", 0): # check for img11
        doubleClick("img111")
        start = time.time() # save current time
        while exists("img11", 0):
          wait(0.3) # wait for img11 to vanish
        while time.time() < (start + 3):
          if not exists(img1, 0):
            break
        if exists(img1, 0):
          doubleClick(img2)
          continue # repeat the outer loop
        if exists("img3", 0) and exists("img31", 0):
          doubleClick(img2)
          doubleClick(img111)

--- 17 repeat until img1 vanishes
everything from beginning or what?

You now have 2 problems:
- I cannot see, that your Python knowledge raises, which decreases my willingness to help
- I will not be available until after Nov. 19th and only answer very important requests

All the best

David Borghi (davidborghi) said : #16

No worries about missing the comment.

I'm trying to learn, I'm sorry that I'm not growing as fast as you expected. :|

Thank you for all the help and attention.

I'll try to implement what you suggested and what I'm trying to achieve.

I'm sorry for all the time I made you spent with my problems.

Have a nice time.

David Borghi (davidborghi) said : #17

Q: everything from beginning or what?
A: I'm sorry I didn't answer this question. The line 17 was just what you suggested me before.
     The structure of the script is the same, but I'd like it two work in 2 different scenarios (which you adjectived as step 1 and 2.)

Also, just to be clear, I put the numbers to represent the lines just so I could reference in case It was needed, what I described was not intended to be the code, I basically did the same as you when you described "the job" in comment #10, so you could "visible" understand what I was trying to do.

(If possible, please, can you answer If you thought that, what I sent to you in comment #14 was the code?) The answer is no, I was not pretending to code like that, It was just a description.

David Borghi (davidborghi) said : #18

Hello, RaiMan!

Just letting you know that It's perfectly working (thank you!) except the last part:

        if exists("img3", 0) and exists("img31", 0):
          doubleClick(img2)
          doubleClick(img111)

I don't know why but I'm trying to figure out, there is no need to get in touch with me, I'm just letting you know that what you've written is working.

As soon as I figure out why the last part isn't working I'll let you know.

Thank you so much for all your help and your attention.
I'm sorry for all the inconvenience. :\

Have a wonderful time and congratulations on your work. :)

Launchpad Janitor (janitor) said : #19

This question was expired because it remained in the 'Open' state without activity for the last 15 days.

David Borghi (davidborghi) said : #20

Janitor, I didn't add anything here as RaiMan told me he would not be able to answer until Nov 19.

If possible, please, leave it open, I'm still trying to figure out on how to do it.

Thank you for your attention.
I'm sorry for any inconvenience.

RaiMan (raimund-hocke) said : #21

Please Start a new question.
This one gets overloaded.

Til next week.