[Pythonmac-SIG] Copying to clipboard (pasteboard)

Bob Ippolito bob at redivi.com
Tue Mar 22 21:59:42 CET 2005

On Mar 22, 2005, at 3:30 PM, Jeremy Conlin wrote:

> I have a little python script that reads a text file and prints what 
> it finds to the screen.  Not everything it finds, of course, just the 
> important stuff.  Instead of printing to the screen, or in addition to 
> printing to the screen, I would also like to copy the text to the 
> clipboard so I can paste it in another application.  Does anyone know 
> how to do this?
> In my searching, I have found things like Carbon.Scrap, but it has 
> very little documentation and so is of no help to me.

One easy way is to just use the pbcopy command line tool:

#!/usr/bin/env python
import os
out = os.popen('/usr/bin/pbcopy', 'w')
out.write('my text to put on clipboard')

Another way would be to use PyObjC, NSPasteboard, etc.  /usr/bin/pbcopy 
and pbpaste are written in Objective-C using this class.  Using that 
method, you could do things other than ascii, rtf, or postscript.


More information about the Pythonmac-SIG mailing list