# richxerox

Bitbucket is a code hosting site with unlimited public and private repositories. We're also free for small teams!

Close

Rich text cut/copy/paste for Mac OS X.

## Usage

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()

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 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:

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


## Background

I searched long and hard but couldn't find a simple Python module that made copy and paste operations on Mac OS X 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 Mac OS X apps are unfortunately RTF-centric. I say unfortunately because:

• 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 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 common lingua franca. I've not found any particularly good, robust, or up-to-date Python tools for parsing and transforming RTF. The handy textutil 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.

## Notes

• As of version 0.6, much more robust handling of Unicode characters. Better auto-install, including installing foundation pyobjc module if necessary. (NB, if this needs to be installed, the install process will take a long time. Don't just get coffee while it's installing. Take lunch. A long, languorous lunch. And then maybe have a nap. By the by, this auto-install only works reliably on Python 2.7 and above, so official support for Python 2.6 has been withdrawn.)
• 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 copy_paste.py
• See also NSPasteboard docs, a discussion on UTIs, and John Siracusa's discussion of the evolution of Mac OS types

## Installation

pip install -U richxerox


To easy_install under a specific Python version (3.3 in this example):

python3.3 -m easy_install --upgrade richxerox


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

# Recent activity

Jonathan Eunice pushed 1 commit to jeunice/richxerox

fac09b7 - The 0.5 release had a serious error in over-quoting Unicode charactes coming from real apps, even though it passed all tests. Fixed.

Jonathan Eunice pushed 3 commits to jeunice/richxerox

6045ac2 - Ready to publish 0.5
81c59ec - Cleaned up considerably. Much better setup, including pyobjc dependency. More robust Unicode handling. Python 3 and PyPy support added. Tested across all versions. Docs tweaked.
79bd5b6 - reorganizing tests
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.