Sikulix 1.1.0 Onchange-problem

Asked by Dr. Xenofon Nastos

I am trying to do the following:
find a menu, open it and using the onchange-event get the menu points using the text() function.
Unfortunately my code doesn't work as expected:

import org.sikuli.script.TextRecognizer as TR
Settings.OcrReadText = True
Settings.OcrLanguage = "deu"
TR.reset()

def changed(event):
    print "something changed in ", event.region
    for ch in event.changes:
        ch.highlight(5) # highlight all changes
        r = getLastMatch()
        menustring = r.text()
        menustring = menustring.replace(" ", "")
        print menustring.encode("utf-8")

onChange(500, changed)
observe(background=True)

try:
    z = "Fenster"
    match = findText(z)
    match.highlight(1)
    menu = match.getTarget()
    menu.click()
    pass
except FindFailed:
    pass

wait(30)
stopObserver()

Does anyone have a clue what could be wrong?

Cheers!
Xenofon

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

--1: Settings.OcrLanguage = "deu"
This only makes sense, if you have put german traineddata into the tessdata folder.

--2: does menu.click()
... open the menu?

--3: does ch.highlight(5)
... highlight the opened menu?

--4: this does not make sense
r = getLastMatch()
menustring = r.text()
according your intention:
r now is the region of what match = findText(z) returned (what should have been highlighted)

if you want to look into the menu region, you have to use the match from event.changes.

--5: you are using 1.0.1
... so I am not sure, wether findText() already works there.
in doubt use find(z) instead

Since you seem to be new to SikuliX, I recommend to switch to 1.1.0 (http://sikulix.com)

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

Uuups, just realised you mentioned 1.1.0.

please use the latest build (2015-07-20) and read (again) carefully through:
http://sikulix-2014.readthedocs.org/en/latest/region.html#observing-visual-events-in-a-region

some things have changed with 1.1.0

Revision history for this message
Dr. Xenofon Nastos (xenofon-nastos) said :
#3

--1 yes I have german traineddata in the tesseract Folder
--2 yes this opens the menu
--3 yes
--4 the Intention was that the whole menu is being recognized as a Change, using this match I wanted to get the menu text, I will try to rewrite this...

I will also try to get the last build you mentioned, at the Moment I am using the build from 1.7.2015

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

with the latest build the event.xxx have been changed to getters (see docs), so in your case event.getChanges().

--4 the Intention was that the whole menu is being recognized as a Change ...
this should be the case:
event.getChanges()[0]
should be the one and only changed region (if nothing else happened on the screen) and should be the menu region without the head.

Revision history for this message
Dr. Xenofon Nastos (xenofon-nastos) said :
#5

Looks good now, this is the working code sniplet with the build from yesterday:

import org.sikuli.script.TextRecognizer as TR
Settings.OcrReadText = True
Settings.OcrLanguage = "deu"
TR.reset()
print "Anfang"
def changed(event):
    print "something changed in ", event.getRegion()
    event.getChanges()[0].highlight(2)
    menustring = event.getChanges()[0].text()
    menustring = menustring.replace(" ", "")
    print menustring.encode("utf-8")

onChange(1000,changed)
observe(FOREVER, background=True)

try:
    z = "Datei"
    match = findText(z)
    menu = match.getTarget()
    menu.click()
    pass
except FindFailed:
    pass

wait(10)
stopObserver()

Thanx a lot Raimund!

Revision history for this message
Dr. Xenofon Nastos (xenofon-nastos) said :
#6

Thanks RaiMan, that solved my question.