Skip to main content

Pasteboard

License: MPL 2.0 CI

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

Python 3.8+ is required. 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

To install development dependencies, use:

python3 -m venv env
source env/bin/activate
pip install .[dev]

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

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.

Metadata

Release files for pasteboard 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pasteboard 0.4.0
File Size Uploaded
pasteboard-0.4.0.tar.gz 28.2 kB Details

Built distributions (wheels)

Table of built distributions (wheels) for pasteboard 0.4.0
File
pasteboard-0.4.0-cp312-cp312-macosx_11_0_universal2.whl CPython 3.12 CPython 3.12 macOS 11.0+ universal2 (ARM64, x86-64) Details
pasteboard-0.4.0-cp311-cp311-macosx_11_0_universal2.whl CPython 3.11 CPython 3.11 macOS 11.0+ universal2 (ARM64, x86-64) Details
pasteboard-0.4.0-cp310-cp310-macosx_11_0_x86_64.whl CPython 3.10 CPython 3.10 macOS 11.0+ x86-64 Details
pasteboard-0.4.0-cp39-cp39-macosx_11_0_x86_64.whl CPython 3.9 CPython 3.9 macOS 11.0+ x86-64 Details
pasteboard-0.4.0-cp38-cp38-macosx_11_0_x86_64.whl CPython 3.8 CPython 3.8 macOS 11.0+ x86-64 Details

Total release size: 163.0 kB

Release files / pasteboard-0.4.0.tar.gz

Download URL pasteboard-0.4.0.tar.gz
Size 28.2 kB
Tags Source
SHA-256 checksum
How to use checksums
dd332253c431ada32074dd5693a231261f2bbd78465de4da27a3469f30d0f84d
BLAKE2b-256 checksum
How to use checksums
beedd21829c72b2d7de85a3ef8214639e60611e92676c5559a4e879c5711b3ac
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.2 CPython/3.11.7

Release files / pasteboard-0.4.0-cp312-cp312-macosx_11_0_universal2.whl

Download URL pasteboard-0.4.0-cp312-cp312-macosx_11_0_universal2.whl
Size 30.7 kB
Tags CPython 3.12 macOS 11.0+ universal2 (ARM64, x86-64)
SHA-256 checksum
How to use checksums
d7e749dafc1fe75fe7dde3464e81059b1735f3de67c0edc035e5fcef3410afb8
BLAKE2b-256 checksum
How to use checksums
59c1f488fbf6e0ab046d208155bccfa8d0bda737c66ce531bc64015238e38f02
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.2 CPython/3.11.7

Release files / pasteboard-0.4.0-cp311-cp311-macosx_11_0_universal2.whl

Download URL pasteboard-0.4.0-cp311-cp311-macosx_11_0_universal2.whl
Size 30.6 kB
Tags CPython 3.11 macOS 11.0+ universal2 (ARM64, x86-64)
SHA-256 checksum
How to use checksums
da509dc8c038240779b08301d53049e3b0c37229080d8010114ac351c4281d5c
BLAKE2b-256 checksum
How to use checksums
b71d65ceb23d0df4003a7357fdcbea5026285065ac964449109dcdcf75bcd6c0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.2 CPython/3.11.7

Release files / pasteboard-0.4.0-cp310-cp310-macosx_11_0_x86_64.whl

Download URL pasteboard-0.4.0-cp310-cp310-macosx_11_0_x86_64.whl
Size 24.5 kB
Tags CPython 3.10 macOS 11.0+ x86-64
SHA-256 checksum
How to use checksums
bdcbfbcbf16513cf14455e3cccdd390dadcb550f6296d35b542b1238d1ea0ece
BLAKE2b-256 checksum
How to use checksums
fe523da2b9e9d08af1ceee29de831c306b43524217bb5e78058215e85c7a8c2b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.2 CPython/3.11.7

Release files / pasteboard-0.4.0-cp39-cp39-macosx_11_0_x86_64.whl

Download URL pasteboard-0.4.0-cp39-cp39-macosx_11_0_x86_64.whl
Size 24.5 kB
Tags CPython 3.9 macOS 11.0+ x86-64
SHA-256 checksum
How to use checksums
247ff0e087128299dc3d8b776a6f01b67f85f26a833a504b754b661521101851
BLAKE2b-256 checksum
How to use checksums
a9c14f0544483502bdab170b7bc67cc592dba4ebbd32687f9173a25b667f7d7e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.2 CPython/3.11.7

Release files / pasteboard-0.4.0-cp38-cp38-macosx_11_0_x86_64.whl

Download URL pasteboard-0.4.0-cp38-cp38-macosx_11_0_x86_64.whl
Size 24.5 kB
Tags CPython 3.8 macOS 11.0+ x86-64
SHA-256 checksum
How to use checksums
d375e5718a22cf87eceb465c1e9d40706acdb16e75d663b64b318a599c08b02d
BLAKE2b-256 checksum
How to use checksums
b2b48de22275633ab823cd16ace03672b431f087af78486a7b2e12dbb14bc4ae
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/4.0.2 CPython/3.11.7

Release history Release notifications | RSS feed

This release

0.4.0 This release

6 release files

0.3.3

5 release files

0.3.2

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page