Script example of using an observer

Asked by surfdork

Are there any script examples of using the observer? I'm not understanding the documentation e.g. how to do it.
 What I want to do is have an observer running while a script is executing
That observer is looking for a group of popup error messages that may appear anytime during the scripts execution
I've seen some code snippets but they don't make sense to me (FAQ 1770).

I have a series of Defs containig if exists statements and elif for program flow

How do I create an insert an observer function.

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

The code snippet from faq 1770 is exactly, how you can handle these situations (since it was setup, to show the effect the scan rate, it contains some extra coding).

--- your situation
supposing for an example
- you have a popup with fixed content (captured image imgPop)
- you know the region on the screen, where it should appear (regPop)
- in the main script, we want to know, that the popup handler is active

# the script template

def handler(e):
     regPop = e.region
     regPop.isHandling = True #starting
     # do something to make the popup vanish
     regPop.isHandling = False #finished

# main workflow
regPop = Region(x,y,w,h) # evaluated somehow
imgPop = "captured-image-of-popup.png"

regPop.isHandling = False # initialize to False

regPop.appear(imgPop, handler)
regPop.observe(FOREVER, background=True) # starts observing

# main workflow continues
click(something)
wait(something_else)
while regPop.isHandling: wait(1) # pause while popup handler is active
# more main workflow

Be aware: the mouse handling is exclusive inside the handler, while it is active. So try to make the handler as short and fast as possible, to reduce interferences with your main script to a minimum.
Very critical parts of your main workflow might be scripted using the above regPop.isHandling, so they only run, while the handler is not active.

Revision history for this message
surfdork (surfdork) said :
#2

Thanks RaiMan, that solved my question.