Multiple events in one script

Asked by Kerz on 2018-10-07

Hi There,

Basically I have a script of around 200 lines (mostly actions, click this click that etc.), however I want to have a condition that if an event happens, i.e. in this Region this image appears, then type this key.

I have the code working and doing this, however I need it to work alongside the 200 lines of code, and if it hits the event, it types the key and then continues from the point it's at in the main script.

I suppose my main question is, is this possible and if so how

Question information

English Edit question
Sikuli Edit question
No assignee Edit question
Solved by:
Last query:
Last reply:
RaiMan (raimund-hocke) said : #1

you have to dive a little into the Python language and the features of SikuliX.

More complex scripts needs some basic insights into scripting/programming workflows lie you mentioned.

The basic language elements are if...else, for/while loops and handling exceptions using try...except.

Kerz (kerz22) said : #2

Hi RaiMan,

Thanks for the reply, I have the following below as the event to react to -

H1 = Region(x,y,wh)
 While H1.exists(image)

This works fine and I can break the script of 100 lines to use this however it doesnt restart the loop drom the same point it broke at, just wondering if you could point me in the right direction, no worries if not

RaiMan (raimund-hocke) said : #3

--- I can break the script of 100 lines
I do not understand, what that should mean.

... and the same goes for
it doesnt restart the loop drom the same point it broke at

Kerz (kerz22) said : #4

Sorry probably didn't explain myself properly at all -

At the moment the code is as below as an example;

H1 = Region(x,y,w,h)

1while exists(image1): #I'm using this purely to keep the code looping
2 while exists(image2):
3 type(Key.ADD)
5 while H1.exists(image3):
6 type(Key.F1)
7 else:
8 type(Key.F10)
9 wait(1)
10 type(Key.F10)

I've numbered the lines to make it easier to try and explain, I want lines 5 and 6 to be continually checked whilst running through the other lines, 2-3, 7-10 etc. and if the image is found in the Region H1 then type Key F1 and continue from the point it was at. For example if it's on line 8, and the image appears in Region H1, type Key F1 and then continue from line 8.

Hopefully this makes a little more sense

Best RaiMan (raimund-hocke) said : #5

If the observe feature (H1.onAppear(image3, handler)) does not help, it is not possible with normal usage of while/if/else.

You would need some parallel processing with locking/messaging.

There is no feature, that allows to restart a script from a specific line, if something happens.

Generally the features of SikuliX are not atomic, hence asynchronous events might come up somewhere during processing of a feature.

So a workflow with asynchronous events always needs some more or less complex synchronisation of the workflow elements.

Kerz (kerz22) said : #6

Thanks a lot this worked perfectly!

Kerz (kerz22) said : #7

Thanks RaiMan, that solved my question.