Skip to main content

Pasteboard - Python interface for reading from NSPasteboard (macOS clipboard)

Project description

Pasteboard

License: MPL 2.0 Build

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.

Now with type hints!

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.

Getting file URLs

>>> import pasteboard
>>> pb = pasteboard.Pasteboard()
>>> pb.get_file_urls()
('/Users/<user>/Documents/foo.txt', '/Users/<user>/Documents/bar.txt')

Warning This API is new, and may change in future.

Returns a Tuple of strings, or None. Also supports the diff parameter analogue to get_contents.

Development

You don't need to know this if you're not changing pasteboard.m code. There are some integration tests in tests.py to check the module works as designed (using pytest and hypothesis).

This project uses pre-commit to run some linting hooks when committing. When you first clone the repo, please run:

pre-commit install

You may also run the hooks at any time:

pre-commit run --all-files

Dependencies are managed via poetry. To install all dependencies, use:

poetry install

This will also install development dependencies (pytest). To run the tests:

poetry run pytest tests.py --verbose

License

From version 0.3.0 and forwards, this library is licensed under the Mozilla Public License Version 2.0. For more information, see LICENSE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for pasteboard, version 0.3.3
Filename, size File type Python version Upload date Hashes
Filename, size pasteboard-0.3.3-cp36-cp36m-macosx_10_15_x86_64.whl (18.5 kB) File type Wheel Python version cp36 Upload date Hashes View
Filename, size pasteboard-0.3.3-cp37-cp37m-macosx_10_15_x86_64.whl (18.5 kB) File type Wheel Python version cp37 Upload date Hashes View
Filename, size pasteboard-0.3.3-cp38-cp38-macosx_10_15_x86_64.whl (18.5 kB) File type Wheel Python version cp38 Upload date Hashes View
Filename, size pasteboard-0.3.3-cp39-cp39-macosx_10_15_x86_64.whl (18.5 kB) File type Wheel Python version cp39 Upload date Hashes View
Filename, size pasteboard-0.3.3.tar.gz (13.5 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page