Alex Rivera | Logout

Can python send text to the Mac clipboard

Asked 2009-12-01T11:51:49.887
57

I'd like my python program to place some text in the Mac clipboard.

Is this possible?

Edit
Report

2 Answers

19

The following code use PyObjC (https://pyobjc.readthedocs.io)

from AppKit import NSPasteboard, NSArray

pb = NSPasteboard.generalPasteboard()
pb.clearContents()
a = NSArray.arrayWithObject_("hello world")
pb.writeObjects_(a)

As explained in Cocoa documentation, copying requires three step :

  • get the pasteboard
  • clear it
  • fill it

You fill the pasteboard with an array of object (here a contains only one string).

answered 2010-08-24T10:55:48.540
1

if you just wanted to put text into the mac clipboard, you could use the shell's pbcopy command.

answered 2011-07-31T08:21:01.950

Your Answer