Skip to content
Advertisement

How to take Screenshot in Mac OS X from inside of Python: aka Command-Control-Shift-4

With Command-Control-Shift-4 the screenshot area can be taken/then saved to a clipboard. I wonder if same would be possible from inside of Python. Ideally an image file format could be specified as well as to where to save it all programmatically within Python. Any ideas?

edited later 1

I’ve located applescript that does almost what’s needed except this script captures an entire screen. I would like to limit it to a region instead.

tell application "System Events"
    keystroke (ASCII character 36) using {command down}
    delay 1
    keystroke space
end tell

edited later 2

The apple script below does a region screen capture quite well (there is still no control on to where exactly the captured image is saved (it is saved by default to the user’s Desktop).

tell application "System Events"
    keystroke (ASCII character 36) using {command down}
    key code 4 using {command down, shift down}
end tell

The question is how to customize the output file path location…

Advertisement

Answer

There seems to be a standard utility called screencapture on the Mac, you could use subprocess to call that, like so:

from subprocess import call
call(["screencapture", "screenshot.jpg"])

There are options to screencapture to specify a specific rectangle as well:

$ screencapture -h
. . . 
  -R<x,y,w,h> capture screen rect
Advertisement