Adding logic via IF, ELSE statement in Sikuli Java API using the exist method

Asked by alex

I have the following code:

Screen s = new Screen();
s.find(imagePath).rightClick(imagePath);
s.find(imagePath).rightClick(imagePath);
s.find(imagePath).rightClick(imagePath);

I'm using the Java API in Eclipse. How can I modify this code so that if the image doesn't exist, it moves on to another image, instead of terminating the program?

In Eclipse, due to the auto-complete, I see there is a method named Exists, but I can't figure out how to get it working.

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
jm.trappier@gmail.com (jm-trappier) said :
#1

Hello,

I'm not really a specialist but for what I've seen so far, the main difference between "find" and "exists" is that find will raise a FindFailed exception in case no match was found when "exists" won't raise it, it will just return null. If think you can use :

if(s.exists(imagePath) != null){s.rightClick(imagePath)}

You can add a timeout with exists, just like this :

if(s.exists(imagePath, 2) != null){s.rightClick(imagePath)}

I may be wrong, I'm not a java specialist and just begining to use sikuliX (and testing java at the moment, 10 minutes long :p) but I think it will do the trick.

Revision history for this message
jm.trappier@gmail.com (jm-trappier) said :
#2

Dunno how to edit my own post, I forgot the ";", please read this :

if(s.exists(imagePath) != null){s.rightClick(imagePath);} instead (true for the other line as well, of course).

Note that in this case, you could just write :

if(s.exists(imagePath) != null) s.rightClick(imagePath);

But all is a question of style preference.

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

if (s.exists(imagePath1) != null))
   s.click() // clicks the last match in s
else if (s.exists(imagePath2) != null)
   s.click() // clicks the last match in s
else if (s.exists(imagePath3) != null)
   s.click() // clicks the last match in s
else
   System.exit(1) # none found, you might issue some log before

BeAware:
- the first found image wins
- each search lasts 3 seconds if not found (so max for the 3 is 9 secs)

You might use
exists(image, 0)
if you are sure, that at least one of the images should be on the screen.

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

about the features you should have a look at the docs
http://sikulix-2014.readthedocs.org/en/latest/index.html

and in parallel at the javadocs:
http://nightly.sikuli.de/docs/index.html

Can you help with this problem?

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

To post a message you must log in.