Multiple visual events --- Use observe() or exists()?

Asked by paul

Hello there !

Im quite new to the magical world of Sikuli and im just trying learn how it works. Since i have searched some topics, google and the website with the tutorials without getting a clou how to do what i would like to ask the community for some advise.

I would like to programm a "simple" script that works with the onAppear command. I read about it having a bug, but im not sure if i could see the difference between a bug or a wrong syntax..

My goal would be to make Sikuli search for several picture events that appear (a bit like the facebook example) and react to them.
like : if Picture A appears - click picture Y / if picture B appears - click picture Z - and so on... the whole prozess should loop infently- like running on background.

i already tried to use the "while exists" guide from the "desktop Surveillance tutorial" but i gues i got something wrong there. Simply coply the command and paste it within the following lines does not do the job (i gues because there is something like a "if not exists - please just go on with the skript without doing anything - command is missing.

So. now my questions is - could you give me some advise how to achiev this - im kind of a total newbie to script language so maybe a example i could modify or a guide that explains how to do it would be great.

If u think i would need expierinced skills to do stuff like that - could you maybe post some links where i could possible find answers ? Since its just kind of a "playing arround" project of mine, im not sure if will make it through learning the whole thing, but i could give it a try if there is no other way..

Thanks for reading,
hoping for help.

All the best

Question information

Language:
English Edit question
Status:
Answered
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:

This question was reopened

  • by paul
Revision history for this message
RaiMan (raimund-hocke) said :
#1

how to repeat something: faq 1437

about observe you should first look at:
--1. http://sikuli.org/docx/region.html
--2. http://sikuli.org/docx/region.html#finding-inside-a-region-and-waiting-for-a-visual-event
--3. http://sikuli.org/docx/region.html#observing-visual-events-in-a-region

general comment:
the observer is convenient, if you want to look for visual objects in parallel and if reaction timing is the focus. But it is relatively complex to implement,because of the communication between your script and the event handlers.

To get a start it is mostly sufficient to start with a while loop, that uses exists().

example:

img1 = "some_image1.png"
img2 = "some_image2.png"
img3 = "some_image3.png"

imgstop = "some_other_image.png"
# to stop the loop, make imgstop invisible

isImg1 = False
isImg2 = False
isImg3 = False

while exists(imgstop, 0): # loop as long imgstop is visible
   if exists(img1, 0):
      isImg1 = True
   if exists(img2, 0):
      isImg2 = True
   if exists(img3, 0):
      isImg3 = True
   if not (isImg1 or isImg2 or isImg3): # no img visible?
      wait(1) # time to be adjusted
      continue # we look again
   # at this point we know which images are present
   # some code to do something depending on
   # which images exist e.g.
   if isImg1 and isImg2:
      # do something
   #prepare to loop again
   isImg1 = False
   isImg2 = False
   isImg3 = False
   wait(1) # time to be adjusted
   # we go back to loop again
# first statement after the loop

I think for up to 5 visual objects this could work rather good, especially when the search is restricted to a smaller region (e.g. app window). The second parameter 0 in exists() makes the search to come back after the first try (0.3 seconds in average) and not wait the standard 3 seconds.

You may try to convert the "if exists()" to onAppear(img, handler). The challenge would be to know inside a handler, wether one of the other events has happened meanwhile (global variables or added region attributes).

Finally: yes, the complexity of your goal needs some diving into the theoretics of Python scripting and the Sikuli API.

Have fun though ;-)

Revision history for this message
paul (paul-annenhof) said :
#2

hey !

thanks for the fast answer... ill defenetly try this out asp..

i got one more little question : is it possible to do something like this:

If picture A is found, and Picture X has appeared as well - Click Picture Z

just wondering.. in general

Anyway, thanks for your advise.. ill have "dive into"

cheers

Revision history for this message
paul (paul-annenhof) said :
#3

sorry think its already answerd... i ll just try

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

That is exactly the example I chose above:

 if isImg1 and isImg2:
      # do something

since isImgN is set before by the corresponding if exists(imgN, 0) and "# do something" would be done if img1 AND img2 exists in this moment.

Welcome to come back anytime with your questions.

Revision history for this message
paul (paul-annenhof) said :
#5

hey there..

just got a bit further with my "trying to understand this"

I got one essential question:

is there a simple way to do something like:
if isimg1 and ismg2 is not

means - if u find one image but the other one is missing - do something -
in one line (so it only reacts if both of the images are found) - if not - it should skip the "do" line

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

If you do not need the matches later, this is your one liner (based on my first comment):

if exists(img1, 0) and not exists(img2, 0):
   #do something

Hope it helps ;-)

Can you help with this problem?

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

To post a message you must log in.