Pasteboard - Python interface for reading from NSPasteboard (macOS clipboard).
Project description
Pasteboard exposes Python bindings for reading and writing macOS’ AppKit NSPasteboard. This allows retrieving different formats (HTML/RTF fragments, PDF/PNG/TIFF) and efficient polling of the pasteboard.
Installation
Obviously, this module will only compile on macOS:
pip install pasteboard
Usage
Getting the contents
>>> 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. Not all formats of NSPasteboardType are implemented.
diff - Defaults to False. When True, only get and return the contents if it has changed since the last call. Otherwise, None is returned. This can be used to efficiently monitor the pasteboard for changes, which must be done by polling (there is no option to subscribe to changes).
get_contents will return the appropriate type, so str for string types, and bytes for binary types. None is returned when:
There is no data of the requested type (e.g. an image was copied but a string was requested).
diff is True, and the contents has not changed since the last call.
An error occurred.
Setting the contents
>>> import pasteboard
>>> pb = pasteboard.Pasteboard()
>>> pb.set_contents('pasteboard')
True
>>>
Analogously, set_contents sets the contents of the pasteboard. This method takes two arguments:
data - str or bytes-like object, required. There is no type checking. So if type indicates a string type and data is bytes-like but not UTF-8 encoded, the behaviour is undefined.
type - The format to set. 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. Not all formats of NSPasteboardType are implemented.
set_contents will return True if the pasteboard was successfully set; otherwise, False. It may also throw RuntimeError if data can’t be converted to an AppKit type.
Development
(You don’t need to know this if you’re not changing pasteboard.m code.)
In the repository, I’ve included a Pipfile that can be used with pipenv to install all dependencies for testing (pasteboard has no Python dependencies itself). There are some integration tests in tests.py to check the module works as designed (using pytest and hypothesis).
$ pipenv install
$ pipenv run pytest tests.py --hypothesis-show-statistics
To clean up:
$ pipenv --rm
$ rm -rf \
.hypothesis/ \
.pytest_cache/ \
build/ \
pasteboard.egg-info/ \
__pycache__/ \
pasteboard.*.so \
Pipfile.lock
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for pasteboard-0.2.0-cp36-cp36m-macosx_10_12_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2ffbcd7f89a5dc9a8b4f8fde387ae1176a8ad379cca1ae1627381fc08b3074a9 |
|
MD5 | eb5f18deeea43baced0ca59a6732ef5b |
|
BLAKE2b-256 | a86f57783ecbeae3aa65d88585875d97a048371927f78d28833283cea91b4f78 |