← Back to team overview

sikuli-driver team mailing list archive

Re: [Question #146922]: [HowTo] use getLastMatch()

 

Question #146922 on Sikuli changed:
https://answers.launchpad.net/sikuli/+question/146922

    Status: Open => Answered

RaiMan proposed the following answer:
always a pleasure to try to answer your questions :-)

I suppose you are still on 10.2 :-(

from the guide-10.x:
http://sikuli.org/trac/wiki/reference-0.10#lastMatch

All successful find operations ( explicit like find() or implicit like click() ), store the best match into lastMatch of the region that was searched. findAll() store all found matches into lastMatches of the region that was searched as an iterator.
To access these attributes use region.getLastMatch() or region.getLastMatches() respectively.

Think this is rather simple and clear. And it should be clear, that
getLastMatch() does not accept any parameter - it is getter method that
returns an stored attribute of a Region instance and this is of class
Match.

your trials:

hover( )
getLastMatch(click)
-- cannot work: parameter given

hover( )
click getLastMatch()
-- cannot work - not Python syntax

--- step by step:

hover(<some image>)
# searches SCREEN, since not qualified by a region
# if successful, the Region object SCREEN has now stored the match, 
# that was produced by the implicit find() behind hover(<some image>)
# so it should be accessible with
lastMatch = SCREEN.getLastMatch()
# or shorter
lastMatch = getLastMatch() # uses implicit Region object SCREEN

now lastMatch can be used like any other Match or Region object
click(lastMatch)

--- back to your trial - this should work
hover(<some image>)
click(getLastMatch())

--- the typical usage is in all cases where you want to reuse the last
match in a region without repeating the find() operation.

if exists(<some image>):
   click(getLastMatch())

without a getLastMatch you wold have to write (performance equivalent):

m = exists(<some image>)
if m:
   click(m)

or (additional find())

if exists(<some image>):
   click(<some image>)

--- one more thing:

I sometimes write

m = find(<some image>)
if m.exists(<some other image>)
   click(getLastMatch))
else:
   print "no success"
   exit(1)

and wonder why it does not click AND does not print "no success"

Uuups, we have to write

   click(m.getLastMatch)

since the respective last match is stored in Region object m and not in
SCREEN, that is referenced by the "naked" getLastMatch()

--- Hope it helps ;-)

You received this question notification because you are a member of
Sikuli Drivers, which is an answer contact for Sikuli.