Skip to content

Commit

Permalink
Merge pull request python-pillow#8630 from radarhere/imagegrab_macos
Browse files Browse the repository at this point in the history
Do not use temporary file in ImageGrab.grabclipboard() on macOS
  • Loading branch information
radarhere authored Dec 28, 2024
2 parents d008d12 + f10e9f4 commit 4e82f2b
Showing 1 changed file with 10 additions and 21 deletions.
31 changes: 10 additions & 21 deletions src/PIL/ImageGrab.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,28 +104,17 @@ def grab(

def grabclipboard() -> Image.Image | list[str] | None:
if sys.platform == "darwin":
fh, filepath = tempfile.mkstemp(".png")
os.close(fh)
commands = [
'set theFile to (open for access POSIX file "'
+ filepath
+ '" with write permission)',
"try",
" write (the clipboard as «class PNGf») to theFile",
"end try",
"close access theFile",
]
script = ["osascript"]
for command in commands:
script += ["-e", command]
subprocess.call(script)
p = subprocess.run(
["osascript", "-e", "get the clipboard as «class PNGf»"],
capture_output=True,
)
if p.returncode != 0:
return None

im = None
if os.stat(filepath).st_size != 0:
im = Image.open(filepath)
im.load()
os.unlink(filepath)
return im
import binascii

data = io.BytesIO(binascii.unhexlify(p.stdout[11:-3]))
return Image.open(data)
elif sys.platform == "win32":
fmt, data = Image.core.grabclipboard_win32()
if fmt == "file": # CF_HDROP
Expand Down

0 comments on commit 4e82f2b

Please sign in to comment.