Package: pasteboard
PasteboardPasteboardexposes Python bindings for reading and writing macOS' AppKitNSPasteboard. This allows retrieving different formats (HTML/RTF fragments, PDF/PNG/TIFF) and efficient polling of the pasteboard.Now with type hints!InstallationPython 3.8+ is required. Obviously, this module will only compile onmacOS:pipinstallpasteboardUsageGetting the contents>>>importpasteboard>>>pb=pasteboard.Pasteboard()>>>pb.get_contents()'pasteboard'>>>pb.get_contents(diff=True)>>>Unsurprisingly,get_contentsgets the contents of the pasteboard. This method
takes two optional arguments:type- The format to get. Defaults topasteboard.String, which corresponds
toNSPasteboardTypeString. See thepasteboardmodule members for other
options such as HTML fragment, RTF, PDF, PNG, and TIFF. Not all formats ofNSPasteboardTypeare implemented.diff- Defaults toFalse. WhenTrue, only get and return the contents if it has changed since the last call. Otherwise,Noneis 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_contentswill return the appropriate type, sostrfor string types,
andbytesfor binary types.Noneis returned when:There is no data of the requested type (e.g. an image was copied but a string was requested)diffisTrue, and the contents has not changed since the last callAn error occurredSetting the contents>>>importpasteboard>>>pb=pasteboard.Pasteboard()>>>pb.set_contents('pasteboard')True>>>Analogously,set_contentssets the contents of the pasteboard. This method
takes two arguments:data-strorbytes-like object, required. There is no type checking. So iftypeindicates a string type anddatais bytes-like but not UTF-8 encoded, the behaviour is undefined.type- The format to set. Defaults topasteboard.String, which corresponds toNSPasteboardTypeString. See thepasteboardmodule members for other options such as HTML fragment, RTF, PDF, PNG, and TIFF. Not all formats ofNSPasteboardTypeare implemented.set_contentswill returnTrueif the pasteboard was successfully set; otherwise,False. It may also throwRuntimeErrorifdatacan't be converted to an AppKit type.Getting file URLs>>>importpasteboard>>>pb=pasteboard.Pasteboard()>>>pb.get_file_urls()('/Users/<user>/Documents/foo.txt', '/Users/<user>/Documents/bar.txt')WarningThis API is new, and may change in future.Returns aTupleof strings, orNone. Also supports thediffparameter analogue toget_contents.DevelopmentYou don't need to know this if you're not changingpasteboard.mcode. There are some integration tests intests.pyto check the module works as designed (usingpytestandhypothesis).This project usespre-committo run some linting hooks when committing. When you first clone the repo, please run:pre-commit installYou may also run the hooks at any time:pre-commit run --all-filesTo 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 --verboseLicenseFrom version 0.3.0 and forwards, this library is licensed under the Mozilla Public License Version 2.0. For more information, seeLICENSE.