Package: richxerox
Rich text cut/copy/paste for macOS (née Mac OS X).Usagefromrichxeroximport*printavailable()# what kind of data is on the clipboard?printpaste()# get data in the default format ('text')printpaste(format='text')# get text (Unicode)printpaste(format='rtf')# get RTFprintpaste(format='html')# get HTMLprint"ALL CONTENTS:\n",pasteall()clear()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 ofxerox,
with simplecopy()andpaste()operations.Think ofpaste()as pastingintoyour program andcopy()as copyingfromyour 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 APIIf you prefer an object-oriented API:fromrichxeroximport*printpasteboard.get_contents(format='html')# pastepasteboard.clear()# clearpasteboard.set_contents(text="this is good!",# copyhtml=h,rtf=r)printpasteboard.get_all_contents()# pasteallBackgroundI searched long and hard but couldn’t find a simple Python module that madecopyandpasteoperations on macOS easy and straightforward.xeroxworks 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 accessthatdata?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’sFoundationandAppKit. This module is the result.Descent Into RTFEven in this HTML-everywhere age, Apple and macOS apps are unfortunatelyRTF-centric.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 isextremelyverbose. Microsoft Word, for instance, emits 29,807
characters as the copy/cut representation of “This isgood!”
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 runningpasteall()to get your own taste of this
madness.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
clipboard.While Mac apps occasionally put HTML contents on the pasteboard, RTF seems
to be the most commonlingua franca. I’ve not found any particularly good,
robust, or up-to-date Python tools for parsing and transforming RTF. The handytextutiltool will, however, convert an RTF file into quite clean HTML, like so:textutil -convert html temp.rtfyieldingtemp.html. This can be parsed and manipulated withlxmlor your favorite HTML/XML library.NotesVersion 1.0.1 withdraws the wheel packaging from distribution.
Wheels are in general great, butit was reportedto 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 installingpyobjcmanually before
installingrichxerox.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](http://semver.org/) 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 foundationpyobjcmodule if necessary. (pyobjcauto-install only works reliably
on Python 2.7 and above,
so official support for Python 2.6 has been withdrawn.)If the underlyingpyobjclibrary needs to be installed, the process
will take alongtime. 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’sReading URLs from OS X clipboard with PyObjCand Carl M. Johnson’scopy_paste.pySee alsoNSPasteboard docs,a discussion on UTIs, and
John Siracusa’sdiscussion of the evolution of Mac OS typesThe author,Jonathan Euniceor@jeunice on Twitterwelcomes your comments and suggestions.InstallationTo install the latest version:pip install -U richxeroxIn some cases with multiple Python installations,pipand 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 withsudoto authorize installation.)