Code Monkey home page Code Monkey logo

pasteboard's Introduction

Pasteboard

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. There are some integration tests in tests.py to check the module works as designed (using pytest and hypothesis, via tox).

$ tox

To clean up (make sure you have committed all work!):

$ git clean -xdf

pasteboard's People

Contributors

tobywf avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.