Find method in Java

Asked by Joe Smith

When the find method is called in my program it seems to go into a state of limbo...

When I call the find method when the URL of the image is incorrect it returns a FindFailed exception like it is supposed to, but when the URL is correct and the image is located on the screen "Searching for Server" only is printed to the log and the application hangs at that point, no matter how long I wait, any Idea why this is happening? The image it is trying to find is not on the screen so it should just throw the exception but it does not

import edu.mit.csail.uid.Screen;

Screen screen = new Screen();
Pattern server1 = new Pattern("URL").similar(.80F);

try{
     writelog("Searching for Server"); //a method that I defined that writes the string to a log file
     screen.find(server1);
     writelog("Server found");
}
catch (Findfailed exception){
     ffwritelog(exception); //a method I defined that writes find failed exceptions to a log file
}

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
Joe Smith
Solved:
Last query:
Last reply:
Revision history for this message
Joe Smith (mj21181) said :
#1

Correction, when the URL is correct and the image is NOT on the screen, "Searching for Server" only is printed

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

If the code is copied and pasted, it is not correct:
should read: catch (FindFailed exception) --- camel cased!!!

But why don't you use the convenience method exists() --- the FindFailed is caught internally and it either returns the match object or None. So you can use a simple if-statement and you can use a timing parameter as with wait().

Screen screen = new Screen();
Pattern server1 = new Pattern("URL").similar(.80F);

writelog("Searching for Server"); //a method that I defined that writes the string to a log file

if (screen.exists(server1, 0)){ // only one search, eventually give more time
     writelog("Server found");
}
else {
     ffwritelog(exception); //a method I defined that writes find failed exceptions to a log file
}

May be you have to add some wait()'s to be sure, what you expect is on the screen.

Revision history for this message
Joe Smith (mj21181) said :
#3

With the if statement if (screen.exists(server1, 0)) { } it gives me an incompatible types error, since it is returning a Match doesn't this require a Match to set it equal to? if (screen.exists(server1, 0) == someothermatch) { }

Also you have the double in the exists method set to zero, what is this double used for in the method? same question with the click() method?

Thanks a lot for the help!

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

Sorry, I forgot, that Java is more type sensitive than Python. I think you should check if the returned match object is None.

To find out the type and meaning of the parms and the returns, pls. look at the different docs (that's why we have it ;-)
Complete guide to Sikuli script: http://sikuli.org/trac/wiki/reference-0.10
Javadocs: http://sikuli.org/doc/java/
and as a last chance: the sources here in launchpad.

Revision history for this message
Joe Smith (mj21181) said :
#5

I did:

Screen screen = new Screen();
Pattern server1 = new Pattern("URL of image").similar(.80F);

public static boolean boolfind(Pattern pattern) {
     try{
          screen.find(pattern);
          return true;
     }
     catch (FindFailed ffexception) {
          ffwritelog(ffexception);
          return false;
     }
}
writelog("Searching for Server");
if (boolfind(server1)==true) {
     writelog("Server found");
}

Also, I noticed that the javadoc isn't very detailed is this because you have the other documentation? Because it seems like the Complete Guide is catered more towards python because some of the params that are there in java and are not in python aren't given as much detail it seems.

But thank you again! it was a great help and thank you for taking so much time to listen to people's problems and respond

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

Sikuli script has two interface layers: (http://sikuli.org/trac/wiki/SystemDesign)
1. the Jython(Python) layer, which is available through Sikuli IDE and can be made available in a Python environment
2. underneath you have the Java classes, where the work is done. This layer is used internally by the Jython layer and can be made available in a Java environment

For 1. we have the "complete guide", that is aimed at the people who are working with Sikuli IDE.

For 2. the developers gave access to the Java class doc (generated by javadoc) and you are right, it is not of that quality and completeness.

But you have to take into account, that the scenarios, where people are working directly with Java and Python (mainly in testing visual apps) where not the priority of the developers.

I learned: if you really want to know how to use it outside the IDE, you have to look into the source code.

So, keep on running!