Exists

Asked by Natanael Morais

I'm programming in Java and when I use the code below the pattern isn't find and don't execute the "if"

--Match ok;
--Pattern automatico = new Pattern(path + "automatico.png");

--screen.click(Auxiliares);
--ok = screen.find(automatico);
--if(ok != null)
--{
-- screen.click(automatico);
--}

If I change (ok = screen.find(automatico); ) by (ok = screen.exists(automatico,0);) the "if" is done.

Is there something wrong?

Question information

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

what version of Sikuli?

there should be no difference between find and exists:
Both return the match object if found.

But generally:
if you want to check the returned match with if (match != null), then you must use exists, since find will throw a FindFailed in case of not found.

If your intention is to click something if it is found then you might just use
click(some_image)

this is only one find operation, whereas with your version you have 2.
When using exists, then depending on Sikuli version, you can reference the last match in the given region:

if(null != screen.exists(some_image)) { click(screen.getLastMatch) } // before version 1.0.1

if(null != screen.exists(some_image)) { screen.click() } // version 1.0.1+

Revision history for this message
Natanael Morais (natanael) said :
#2

The version is 1.1.0 beta 2.

Revision history for this message
Natanael Morais (natanael) said :
#3

Thanks RaiMan, that solved my question.