Sign inDemoInstall


Package Overview
File Explorer

Install Socket

Protect your apps from supply chain attacks



copy/paste for Mac OS X for rich text (HTML/RTF) rather than plain text




|  |version| |versions|

.. |version| image::
    :alt: PyPI Package latest release

.. |versions| image::
    :alt: Supported versions

Rich text cut/copy/paste for macOS (née Mac OS X).


.. code-block:: python

    from richxerox import *

    print available() # what kind of data is on the clipboard?

    print paste()     # get data in the default format ('text')
    print paste(format='text')  # get text (Unicode)
    print paste(format='rtf')   # get RTF
    print paste(format='html')  # get HTML

    print "ALL CONTENTS:\n", pasteall()

    print "ALL CONTENTS AFTER CLEAR:\n", pasteall()

    r = "{\\rtf1\\ansi\\ansicpg1252\\cocoartf1187\\cocoasubrtf390\n" \
        "{\\fonttbl\\f0\\froman\\fcharset0 Times-Roman;}\n{\\colortbl;" \
        "\\red255\\green255\\blue255;}\n\\deftab720\n\\pard\\pardeftab720" \
        "\n\n\\f0\\fs24 \\cf0 This is \n\\b good\n\\b0 !}"
    h = "this is <strong>good</strong>!"
    copy(text="this is good!", html=h, rtf=r)

    print "ALL CONTENTS AFTER COPY:\n", pasteall()

The API is modeled on that of `xerox <>`_,
with simple ``copy()`` and ``paste()`` operations.

Think of ``paste()`` as pasting *into* your program and ``copy()`` as copying
*from* your program.

The main difference in the API is that, given the different formats used in rich
text, one must specify the format provided or needed if it is not plain text.
This is done through keyword-style arguments.

Alternative API

If you prefer an object-oriented API:

.. code-block:: python

    from richxerox import *

    print pasteboard.get_contents(format='html')    # paste
    pasteboard.clear()                              # clear
    pasteboard.set_contents(text="this is good!",   # copy
                            html=h, rtf=r)

    print pasteboard.get_all_contents()       # pasteall


I searched long and hard but couldn't find a simple Python module that made
``copy`` and ``paste`` operations on macOS easy and straightforward. `xerox
<>`_ works well, but it only supports plain
text. What about browsers and word processors that export rich text with
hyperlinks, styles, and so on? How can you access *that* data?

After banging my head against this a few times, I eventually found code samples
I could adapt and make work without understanding the entirety of Apple's
``Foundation`` and ``AppKit``. This module is the result.

Descent Into RTF

Even in this HTML-everywhere age, Apple and macOS apps are unfortunately `RTF

* In my experience, RTF is often not robustly passed between applications.
  Different apps interpret or render the same RTF differently, so font
  sizes and other characteristics change.

* RTF is *extremely* verbose. Microsoft Word, for instance, emits 29,807
  characters as the copy/cut representation of "This is **good**!"
  Microsoft is known for verbose exports, and RTF itself attempts to
  represent whole documents rather than individual snippets. Still, that's
  roughly 1,000x (a.k.a. three decimal orders of magnitude) as verbose as HTML.
  Try copying existing text in some
  application, then running ``pasteall()`` to get your own taste of this

* If you put multiple forms of text on the clipboard, you don't have much if
  any control which one an application will use when you ask it to "paste"
  data. If you want a single format, better to just put that one format on the

While Mac apps occasionally put HTML contents on the pasteboard, RTF seems
to be the most common *lingua franca*. I've not found any particularly good,
robust, or up-to-date Python tools for parsing and transforming RTF. The handy
tool will, however, convert an RTF file into quite clean HTML, like so::

    textutil -convert html temp.rtf

yielding ``temp.html``. This can be parsed and manipulated with `lxml
<>`_ or your favorite HTML/XML library.


* Version 1.0.1 withdraws the wheel packaging from distribution.
  Wheels are in general great, but
  `it was reported <>`_
  to be causing installability breakage depending on the local install
  status of Mac foundation libraries, given that wheels are a preferred
  and somewhat canned install format. Withdrawing wheels should force a
  more local-system-centered set of dependencies to be installed.
  If you still have trouble, try installing
  `pyobjc <>`_ manually before
  installing ``richxerox``.

* Version 1.0.0 updates the testing matrix. Latest versions of 2.7, 3.3,
  3.4, 3.5, and 3.6 are confirmed working. Old,
  pre-[SemVer]( versions have been removed from PyPI;
  they were causing some install problems. Python 3.2 has been withdrawn
  from support as both ancient and no longer being properly supported in
  my local test rig.)

* As of version 0.6, much more robust handling of Unicode characters.
  Better auto-install, including installing foundation ``pyobjc``
  module if necessary. (`pyobjc` auto-install only works reliably
  on Python 2.7 and above,
  so official support for Python 2.6 has been withdrawn.)

* If the underlying `pyobjc` library needs to be installed, the process
  will take a *long* time. For example, 4 hours 7 minutes. Don'ty just get
  coffee while it's installing. Take lunch. A long, languorous lunch. And
  then maybe have a nap.

* Version 0.5 had a mistake in Unicode handling. Even though it passed all
  tests, it over-quoted Unicode coming from real apps. Fixed.

* Code inspired by and/or based on Genba's `Reading URLs from OS X clipboard with PyObjC <>`_
  and Carl M. Johnson's ` <>`_

* See also `NSPasteboard docs <>`_,
  `a discussion on UTIs <>`_, and
  John Siracusa's `discussion of the evolution of Mac OS types <>`_

* The author, `Jonathan Eunice <>`_ or
  `@jeunice on Twitter <>`_
  welcomes your comments and suggestions.


To install the latest version::

    pip install -U richxerox

In some cases with multiple Python installations, ``pip`` and the Python
ininstallation can become disconnected. If the above does not work, try
installing through Python directly. You can also specify a particular
version of Python to install under.::

    python -m pip install -U richxerox

(You may need to prefix these with ``sudo`` to authorize installation.)



Did you know?

Socket installs a GitHub app to automatically flag issues on every pull request and report the health of your dependencies. Find out what is inside your node modules and prevent malicious activity before you update the dependencies.


Related posts

SocketSocket SOC 2 Logo


  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.

  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc