Enlarging a Window by Dragging its Corner
(For Sikuli Version 0.10)
If you want to use Sikuli script, to enlarge a window by dragging its bottom-right corner, you have to evaluate the current position of this corner on the screen, move the mouse pointer to the applicable click point and then perform one or more drag actions.
There are several approaches to do this task. The simplest one is directly looking for the corner and drag it to enlarge the window.
def resizeApp(app, dx, dy): switchApp(app) corner = find(Pattern().targetOffset(3,14)) dragDrop(corner, corner.getTarget().offset(dx, dy)) resizeApp("Safari", 50, 50)
This example defines a function "resizeApp" to enlarge a window on a Mac, and then call the function to enlarge the size of Safari by 50x50 pixels.
The code looks clearer in the IDE, because the offset of the click target is visualized as a red cross in the corner pattern.
In addition to this simplest approach, we also want to show you more possible ways to do the same thing. The following example demonstrates how to use the spatial operators to extend or restrict your searching regions.
The approach to find this corner is to first identify the most characteristic corner of the window (mostly the one with the window buttons) and then try to find the other relevant corners, to be sure you get the click point you need.
General comments:
- whenever possible the find operations are restricted to a region, that should contain the relevant match, to minimize the risk, that something else is found. On top this speeds it up.
- this example could easily be turned into a helper function, where all images and variables can be given as parameters.
- the click point was evaluated in the IDE using the preview window, but then put in the code for flexibility and readability.
- the low level mouse functions are used, so you would be more flexible with continuous movements.
- the development approach was, to first get everything running to evaluate the relevant corner. In this phase, the comments having a "# debug" were all uncommented to have some feedback. Especially setShowActions() and exit() are very helpful.
#setShowActions(True) # debug switchApp("Safari") # get the frontmost Safari window active (clickOffsetX, clickOffsetY) = (3, 18) # evaluated using the preview in IDE mTL = find() # find top left #print mTL; hover(mTL) # debug if not mTL.nearby(10).right().exists(
): # just to asure, we are there popup("Sorry, we are not where we should be!") exit() mTR = mTL.nearby(200).right().find(
) # find top right #print mTR; hover(mTR) # debug mBR = mTR.below().find(
) # find bottom right, the target corner #print mBR # debug hover(mBR.getCenter().offset(clickOffsetX, clickOffsetY)) # move mouse to click point #exit() # debug mouseDown(Button.LEFT) # press and hold left button mouseMove(Env.getMouseLocation().offset(10, 10)) # move mouse to a new location, may be repeated with other values mouseUp() # release mouse button
The find workflow and the mouse-move to the click point can be compressed to a one-liner (no check on the title in this case).
hover(find().nearby(200).right().find(
).below().find(
).getCenter().offset(3,18))
You may look for details in The Complete Guide to Sikuli Script.
Feel free, to contact me directly using my mail address on launchpad RaiMan (Raimund Hocke).
Attachments
-
SafariBR.png
(0.6 KB) - added by RaiMan
2 years ago.
-
SafariTL.png
(1.7 KB) - added by RaiMan
2 years ago.
-
SafariTR.png
(0.7 KB) - added by RaiMan
2 years ago.
-
WindowTitle.png
(2.1 KB) - added by RaiMan
2 years ago.
-
resize-app.png
(26.5 KB) - added by vgod
2 years ago.
).targetOffset(
)
):
)