18

How can I, using Python (2.7) get the contents of the Mac clipboard. Is there a better way than making a wrapper around pbpaste?

Thanks!

6 Answers 6

20

PyObjC is the way to go:

#!/usr/bin/python

from AppKit import NSPasteboard, NSStringPboardType

pb = NSPasteboard.generalPasteboard()
pbstring = pb.stringForType_(NSStringPboardType)
print u"Pastboard string: %s".encode("utf-8") % repr(pbstring)

This only supports text and will return None otherwise. You can extend it to support other data types as well, see NSPastboard Class Reference.

1
  • 1
    On Py3, OS 11.1: print("Pastboard string: %s" % pbstring) Dec 23, 2020 at 11:28
16

Have you looked at the xerox module?
It is supposed to support windows, OS X and Linux


Usage is as follows:

xerox.copy(u'some string')

And to paste:

>>> xerox.paste()
u'some string'

1
  • 1
    Maybe a little too light: On OS X, the xerox module is just a wrapper around pbcopy and pbpaste.
    – alexis
    Jun 19, 2016 at 23:51
6

If you have installed pandas, you can use the function in pandas as follows:

from pandas.io.clipboard import clipboard_get
text = clipboard_get()                                                     
5

The problem with the xerox module and most code samples I've found for "get the contents of the Mac clipboard" is that they return plain text only. They don't support hyperlinks, styles, and such, so they're not really able to access the full contents provided by apps like Microsoft Word and Google Chrome.

Standing on the shoulders of others, I finally figured out how to do this. The resulting richxerox module is available on PyPI and Bitbucket.

Though this question is old, I'm leaving breadcrumbs here because I consistently re-found this page via Google while searching for the answer.

5
  • richxerox looks great! I've been looking for something like this. Annoying glitch: though it installs on Python 3, it's Python 2 code. Why? It's easily upgraded.
    – alexis
    Jun 19, 2016 at 23:53
  • I mean that the version that pip installed (0.129) has print statements without parentheses, it has iteritems(), etc. It's not compatible with python 3, and errors out on import richxerox. Is there a version I can ask pip for what will work? Which one? (I'm asking this because I saw a months-old bug report about recent versions being broken).
    – alexis
    Jun 20, 2016 at 20:20
  • Ha, I guess it's a semantics problem: 129 > 6 :-) I'll fetch the correct version, thanks. (Unfortunately pip failed to build PyObjC on my El Capitan box, so I won't be able to actually test it with python 3.)
    – alexis
    Jun 20, 2016 at 20:52
  • Let us continue this discussion in chat.
    – alexis
    Jun 20, 2016 at 20:56
  • Also consider using jaraco.clipboard, which on Mac uses richxerox (and excludes the old versions like 0.129) and provides the same interface on Windows. Aug 8, 2016 at 13:13
3

Do you know PyObjC? I guess you could use it to write a Py wrapper which interfaces with NSPasteboard. This might be more "elegant" than shelling out to pbpaste.

1
  • 1
    If I knew PyObjC, I wouldn't be writing Mac applications in Python ;)
    – user825286
    Aug 16, 2011 at 18:49
0

You can grab the clipboard (and the screen) with PIL/Pillow on a Mac like this:

from PIL import ImageGrab, Image

# Grab clipboard and save to disk
clip = ImageGrab.grabclipboard()
clip.save("clip.png")

Just for completeness, you can grab the screen like this:

screen = ImageGrab.grab()

# That results in this:
# <PIL.PngImagePlugin.PngImageFile image mode=RGBA size=5120x2880 at 0x110BB7748>

# Save to disk
screen.save("screen.png")

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.