Exist function in sikuli java API

Asked by Muhammed

Hai

Can Any body please tell me how can i check whether an image is there in the screen or not.

Presently i am using this :-
Screen s = new Screen();
if(!s.exists("C://Images//Screen1.png").contains(s));

But this is not perect. If the image is not there it is not going to else condition.

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
RaiMan (raimund-hocke) said :
#1

if (s.exists("some_image")) { }

Revision history for this message
Muhammed (muhammed-k) said :
#2

Sorry its showing syntax error

if(s.exists("C://Images//Screen1.png"))
{
}

Incomaptible type need "Boolean" found "Match".

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

Ok, for someone programming Java this should be an easy one:

if(s.exists("C://Images//Screen1.png") != null)
{
}

Revision history for this message
Muhammed (muhammed-k) said :
#4

Thank you, its solved my problem.

Thank you ones again.

Revision history for this message
tjonnyc (tjonnyc) said :
#5

I usually use the 2-part "if-exists" / "if-not-exists" method:

def FunctionName(n):
    Region(500,300,125,40)
    if exists("Condition.png"):
        {do something}
    if not exists("Condition.png"):
        {do something else}

This way, there's an explicit definition of what to do in both cases. Yes, it's more lines of code, but it's a lot easier to maintain. You can also reverse the order, put "if not exists" first if it's more critical.

Also, doing it this way allows multi-level checking & actions.

Here's an example of a game-bot routine that checks if an item is active on the screen, if not found, it goes into inventory & activates one, if there's none in inventory, it buys one from the in-game store:

Inventory = Location(1000,500) #X,Y coordinates of "Inventory" icon

def FunctionName(a):
    Region(700,200,240,35)
    if exists("ItemIsActive.png"):
        wait(1)
    if not exists("ItemIsActive.png"):
        click(Inventory)
        if exists("ThisIsTheItemYoureLookingFor.png"):
            click("ThisIsTheItemYoureLookingFor.png"), wait(1)
            click(InventoryClose)
        if not exists("ThisIsTheItemYoureLookingFor.png"):
            click(InventoryClose), wait(1)
            BuyItemFromGameStore(a) #separate subroutine that buys items from store