[Pythonmac-SIG] Copying to clipboard (pasteboard)

Doug Schwarz dmschwarz at urgrad.rochester.edu
Wed Mar 23 00:29:35 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.
> Thanks,
> Jeremy

I figured out how to do this with a lot of trial and error.  You can 
use Carbon or AppKit/Foundation.

import Carbon.Scrap, AppKit, Foundation

def clipcopy(arg):
	scrap = Carbon.Scrap.GetCurrentScrap()
	scrap.PutScrapFlavor('TEXT', 0, arg)

def clippaste():
	scrap = Carbon.Scrap.GetCurrentScrap()
		return scrap.GetScrapFlavorData('TEXT')
		return ''

def clipcopy1(arg):
	board = AppKit.NSPasteboard.generalPasteboard()
	board.declareTypes_owner_([AppKit.NSStringPboardType], None)
	newStr = Foundation.NSString.stringWithString_(arg)
	newData = 
	board.setData_forType_(newData, AppKit.NSStringPboardType)

def clippaste1():
	board = AppKit.NSPasteboard.generalPasteboard()
	content = board.stringForType_(AppKit.NSStringPboardType)
	return content


More information about the Pythonmac-SIG mailing list