Search code examples
pythonpython-2.7sikuli

Using Sikuli to take automated screenshots of a window?


Simple question here: I'd like to use Sikuli to take a screenshot of a window on a mac, which would be done by hitting CMD+SHIFT+4 then hitting Space, then clicking a window.

For the CMD+SHIFT+4 I'm having trouble. This doesn't work:

keyDown(KEY_META)
keyDown(Key.SHIFT)
wait(1)
type("4")
wait(1)
keyUp(Key.SHIFT)
keyUp(KEY_META)

Anyone have any ideas? I'm open to other routes of hitting the key combo, for instance, I know to copy this works well:

type("c",KEY_META)

But, it doesn't accept three arguments.


Solution

  • type("4", KeyModifier.CMD+KeyModifier.SHIFT)
    

    Or, even better:

    import shutil
    import os
    screenshotsDir = "absolute-path-to-a-folder"
    img = capture(some_region)
    shutil.move(img, os.path.join(screenshotsDir, "some-name.png"))
    

    where some_region is:

    some_region = SCREEN # for whole screen
    

    or

    someRegion = App.focusedWindow() # for the frontmost window
    

    This has the advantage, that you can control the file name of the shot.