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.

Answer from MagerValp on Stack Overflow
Discussions

macos - What's the quickest way to get a graphic on the clipboard saved to disk? - Ask Different
I use a script that saves any graphic on the clipboard to a new page in a PDF document, thus sort-of replicating the old 'Scrapbook' app on Classic MacOS. The 'quickest way' bit is that I save the script to /Library/Scripts, so I can execute it from the Scripts menulet in the menubar. (which you can enable in the AppleScript Script Editor.app's Preferences dialog). The images can be copy-and-pasted for other purposes or exported as images. #!/usr/bin/python ... More on apple.stackexchange.com
🌐 apple.stackexchange.com
How do I read text from the Windows clipboard in Python? - Stack Overflow
Yes it should work on Python 3.6, and I just tested with Python 3.7.4 (64-bit). Looking the setup.py of the package it should have no dependencies to Pyside or any other packages. Are you sure that the paste command is trying to use Pyside? 2020-03-30T08:03:06.157Z+00:00 ... I used pip to install clipboard package which only has one line from pyperclip import copy, paste LOL. pyperclib is the perfect solution. 2021-01-09T09:07:21.45Z+00:00 ... pyperclip also works on Mac ... More on stackoverflow.com
🌐 stackoverflow.com
copykitten: the missing clipboard library for Python
“Now I hate windows” 😂🤣 same. I grew up using only Windows machines, never knowing how green the grass can be with other options. More on reddit.com
🌐 r/Python
35
120
February 21, 2024
macos - How to Copy a File to Clipboard in Python (Mac) - Stack Overflow
I am trying to find out an answer to the above question - I have already tried shutil, which to my knowledge, does not work because I do not want to copy the file to a destination. Currently using More on stackoverflow.com
🌐 stackoverflow.com
🌐
Macdrifter
macdrifter.com › 2011 › 12 › python-and-the-mac-clipboard.html
Python and the Mac Clipboard - Macdrifter
December 2, 2011 - After some reading and fiddling[1], I ended up with a version of the above script that uses the NSPasteboard class to get and set the clipboard instead of a using the shell command. :::Python import urllib2 from Foundation import * from AppKit import # Sets the clipboard to a string def pbcopy(s): pb = NSPasteboard.generalPasteboard() pb.declareTypes_owner_([NSStringPboardType], None) newStr = NSString.stringWithString_(s) newData = newStr.nsstring().dataUsingEncoding_(NSUTF8StringEncoding) pb.setData_forType_(newData, NSStringPboardType) # Gets the clipboard contents def pbpaste(): pb = NSPasteboard.generalPasteboard() content = pb.stringForType_(NSStringPboardType) return content f = urllib2.urlopen(pbpaste()) source = f.read() f.close() newurl = f.geturl() newurl = newurl.split('?')[0] pbcopy(newurl)
🌐
PyPI
pypi.org › project › clipboard
clipboard · PyPI
A cross platform clipboard operation library of Python. Works for Windows, Mac and Linux.
      » pip install clipboard
    
Published   May 22, 2014
Version   0.0.4
🌐
GitHub
github.com › tobywf › pasteboard
GitHub - tobywf/pasteboard: UNSUPPORTED Pasteboard - Python interface for NSPasteboard (macOS clipboard)
Obviously, this module will only compile on macOS: ... >>> import pasteboard >>> pb = pasteboard.Pasteboard() >>> pb.get_contents() 'pasteboard' >>> pb.get_contents(diff=True) >>> Unsurprisingly, get_contents gets the contents of the pasteboard. This method takes two optional arguments: type - The format to get. Defaults to pasteboard.String, which corresponds to NSPasteboardTypeString. See the pasteboard module members for other options such as HTML fragment, RTF, PDF, PNG, and TIFF.
Starred by 35 users
Forked by 7 users
Languages   Objective-C 56.5% | Python 42.3% | Shell 1.2% | Objective-C 56.5% | Python 42.3% | Shell 1.2%
🌐
AskPython
askpython.com › home › how to read text from a clipboard using read_clipboard?
How to Read Text From a Clipboard Using read_clipboard? - AskPython
February 16, 2023 - The read_clipboard() method takes the text from the clipboard as input and converts it into a string, which is then passed as the input to the read_csv() function. ... The default value is ‘s+’, which denotes the presence of one or more ...
🌐
PyPI
pypi.org › project › pasteboard
pasteboard
JavaScript is disabled in your browser. Please enable JavaScript to proceed · A required part of this site couldn’t load. This may be due to a browser extension, network issues, or browser settings. Please check your connection, disable any ad blockers, or try using a different browser
Find elsewhere
🌐
AskPython
askpython.com › home › how do i read text from the clipboard?
How do I read text from the clipboard? - AskPython
February 27, 2023 - pyperclip is a Python module that provides a simple and cross-platform way to access the clipboard in order to read or write text content. It allows developers to easily copy and paste text between different applications or within the same ...
🌐
The Building Coder
thebuildingcoder.typepad.com › blog › 2013 › 06 › python-mac-pasteboard-access.html
Python Mac Pasteboard Access - The Building Coder - TypePad
June 2, 2013 - Thanks to the platform independence provided by Python, the only thing that needs changing is to replace the Windows clipboard access by Mac pasteboard read and write operations.
🌐
ActiveState
code.activestate.com › recipes › 410615-manipulate-mac-os-clipboard
Manipulate Mac OS clipboard « Python recipes « ActiveState Code
April 20, 2005 - It can double as a Unix-style command-line tool that prints the clipboard contents to stdout or, if specified, copies its stdin to the clipboard, although pbcopy(1) and pbpaste(1) are better suited for that. ... By default, the most reasonable choice of the 'TEXT' flavour is used. Other flavors for special uses can be specified. If there is no "scrap" of a given flavor, Mac OS API report this as an error.
🌐
Pandas
pandas.pydata.org › docs › reference › api › pandas.read_clipboard.html
pandas.read_clipboard — pandas 3.0.2 documentation
Read a table of fixed-width formatted lines into DataFrame. ... >>> df = pd.DataFrame([[1, 2, 3], [4, 5, 6]], columns=["A", "B", "C"]) >>> df.to_clipboard() >>> pd.read_clipboard() A B C 0 1 2 3 1 4 5 6
🌐
Reddit
reddit.com › r/python › copykitten: the missing clipboard library for python
r/Python on Reddit: copykitten: the missing clipboard library for Python
February 21, 2024 -

What My Project Does

copykitten is a clipboard library with support for text and images and a simple API. It's built around Rust arboard library. Thanks to this, it's multiplatform and doesn't require any dependencies.

Target Audience

Developers building CLI/GUI/TUI applications. The library has beta status on PyPI, but the underlying Rust library is pretty stable, being used in commercial projects like Bitwarden and 1Password.

Comparison

There are lots of other clipboard libraries for Python: pyperclip, jaraco.clipboard, pyclip, just to name a few. However, most of them are not maintained for years and require the presence of additional libraries or tools in the operating system. copykitten doesn't suffer from these shortcomings.

A bit of history

Throughout my years with Python there were several times when I needed to use the clipboard in my applications and every time I had to fall back to some shaky methods like asking the end user to install xclip and calling subprocess.run. This never felt great.

Right now I'm making a multiplayer TUI game (maybe I’ll showcase it later too :) ), where users can copy join game codes into the clipboard to easily share it (much like Among Us). This is how I came to the idea of making such a library. I also wanted to try Rust for a long time, and so this all just clicked in my head instantly.

I had fun building it and definitely had some pain too and learned a bit of nitty-gritty details about how clipboards work in different operating systems. Now I hate Windows.

With this post I hope to gain some attention to the project so that I can receive feedback about the issues and maybe feature requests and spread the word that there's a modern, convenient alternative to the existing packages.

Feel free to try it out: https://github.com/Klavionik/copykitten

🌐
Readthedocs
pyperclip.readthedocs.io › en › latest
Welcome to Pyperclip’s documentation! — Pyperclip 1.5 documentation
Pyperclip provides a cross-platform Python module for copying and pasting text to the clipboard.
🌐
Python.org
discuss.python.org › python help
Copy to system clipboard - Python Help - Discussions on Python.org
June 2, 2022 - I’m working on a an app (my 2nd GUI app) that generates a hash value (AKA: checksum) based on a selected file. Right now, the hash value is displayed in a Text Widget, which is has the ‘state’ set to ‘disabled’, so that …
🌐
Programmersought
programmersought.com › article › 17002780044
mac on python to read and write clipboard pictures, text - Programmer Sought
# Read clipboard data format type data_type = pb.types() print data_type Read clipboard string # if NSPasteboardTypeString in data_type: str = pb.dataForType_(NSPasteboardTypeString) print str Read clipboard screenshot # if "public.png" in data_type: img = pb.dataForType_("public.png") # Read the clipboard to copy the image file name if NSPasteboardTypePNG in data_type: img = pb.dataForType_(NSPasteboardTypePNG) print img # Read the clipboard to copy the picture file path url if "public.file-url" in data_type: img = pb.dataForType_("public.file-url") print img