Search code examples
observer-patternsikuli

Confusion in behaviour of onAppear() in sikuli


def stop(event):
    popup("Done")
    event.region.stopObserver()
    pass
wmp = App("C:\\Program Files\\Windows Media Player\\wmplayer")
wmp.open()
wait(2)
click(find(Pattern("play_button.png").exact()))
popup("Started Playing")
wait(3)
onAppear(Pattern("stop_button.png").exact(),stop)
observe(FOREVER)

In the above code snippet I am try in to play the a mp3 file in windows media player and observing it till the stop button appears, after it appears, it would display the popup message "Done". When I run this program for first time, the popup message displays for one time, when I run it again, it displays the popup message two times and on the third time, it displays it thrice. Could someone explain me why is it happening, though I have give only one popup("Done") in the code snippet??


Solution

  • My guess is, that this is some kind of caching problem in the IDE: It seems like the default Region is not reset after/before a script is executed, so everytime you run the script, the onAppear listener is added again and this leads to multiple events at the same time.

    This is just a guess but if I'm right, you can workaround this by using

    Region screenRegion = Region.create(Screen(0).x, Screen(0).y, Screen(0).w, Screen(0).h)
    screenRegion.onAppear(Pattern("stop_button.png").exact(),stop)
    screenRegion.observe(FOREVER)
    

    this will create a new Region with every script run and not apply the onAppear listeners to the default Region.