Previous topic

Desktop Surveillance

Next topic

Can I do X or Y or Z in Sikuli?

This Page

Resizing a WindowΒΆ

This tutorial demonstrates how to use Sikuli script to resize a window by dragging its bottom-right corner. In order to do so, 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.

1def resizeApp(app, dx, dy):
2        switchApp(app)
3        corner = find(Pattern().targetOffset(3,14))
5        drop_point = corner.getTarget().offset(dx, dy)
6        dragDrop(corner, drop_point)
8resizeApp("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. There are several approaches to do this task. The simplest one is directly looking for the corner and drag it to enlarge the window.

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. The strategy is illustrated below. We want to find the top-right corner, then go to the right to find the top-right corner, then go below to find the bottom-right corner.


The script that implements this plan can be written as below:

1#setShowActions(True) # debug
2switchApp("Safari") # get the frontmost Safari window active
4(clickOffsetX, clickOffsetY)  = (3, 18) # evaluated using the preview in IDE
5mTL = find() # find top left
6#print mTL; hover(mTL)  # debug
8mTR = mTL.nearby(200).right().find() # find top right
10#print mTR; hover(mTR) # debug
11mBR = mTR.below().find() # find bottom right, the target corner
12#print mBR # debug
14# move mouse to click point
15hover(mBR.getCenter().offset(clickOffsetX, clickOffsetY))
16#exit()  # debug
18mouseDown(Button.LEFT) # press and hold left button
19# move mouse to a new location, may be repeated with other values
20mouseMove(Env.getMouseLocation().offset(10, 10))
21mouseUp() # release mouse button

The workflow and the mouse-move to the click point can be compressed to a one-liner (no check on the title in this case).


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.