diff --git a/PIL/ImageGrab.py b/PIL/ImageGrab.py index febdb2310..982abfebb 100644 --- a/PIL/ImageGrab.py +++ b/PIL/ImageGrab.py @@ -51,11 +51,31 @@ def grab(bbox=None): def grabclipboard(): if sys.platform == "darwin": - raise NotImplementedError("Method is not implemented on OS X") - debug = 0 # temporary interface - data = Image.core.grabclipboard(debug) - if isinstance(data, bytes): - from PIL import BmpImagePlugin - import io - return BmpImagePlugin.DibImageFile(io.BytesIO(data)) - return data + f, file = tempfile.mkstemp('.jpg') + os.close(f) + commands = [ + "set theFile to (open for access POSIX file \""+file+"\" with write permission)", + "try", + "write (the clipboard as JPEG picture) to theFile", + "end try", + "close access theFile" + ] + script = ["osascript"] + for command in commands: + script += ["-e", command] + subprocess.call(script) + + im = None + if os.stat(file).st_size != 0: + im = Image.open(file) + im.load() + os.unlink(file) + return im + else: + debug = 0 # temporary interface + data = Image.core.grabclipboard(debug) + if isinstance(data, bytes): + from PIL import BmpImagePlugin + import io + return BmpImagePlugin.DibImageFile(io.BytesIO(data)) + return data diff --git a/docs/reference/ImageGrab.rst b/docs/reference/ImageGrab.rst index ec7c42082..7691ed6cc 100644 --- a/docs/reference/ImageGrab.rst +++ b/docs/reference/ImageGrab.rst @@ -7,7 +7,7 @@ The :py:mod:`ImageGrab` module can be used to copy the contents of the screen or the clipboard to a PIL image memory. -.. note:: The current version works on OS X and Windows only. OS X support was added in 3.0.0. +.. note:: The current version works on OS X and Windows only. .. versionadded:: 1.1.3 @@ -17,7 +17,7 @@ or the clipboard to a PIL image memory. returned as an "RGB" image on Windows or "RGBA" on OS X. If the bounding box is omitted, the entire screen is copied. - .. versionadded:: 1.1.3 + .. versionadded:: 1.1.3 (Windows), 3.0.0 (OS X) :param bbox: What region to copy. Default is the entire screen. :return: An image @@ -26,10 +26,11 @@ or the clipboard to a PIL image memory. Take a snapshot of the clipboard image, if any. - .. versionadded:: 1.1.4 + .. versionadded:: 1.1.4 (Windows), 3.3.0 (OS X) :return: On Windows, an image, a list of filenames, or None if the clipboard does not contain image data or filenames. Note that if a list is returned, the filenames may not represent image files. - On Mac, this is not currently supported. + On Mac, an image, + or None if the clipboard does not contain image data.