Can python send text to the Mac clipboard

前端 未结 7 1226
渐次进展
渐次进展 2020-12-04 18:00

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

Is this possible?

相关标签:
7条回答
  • 2020-12-04 18:50

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

    0 讨论(0)
提交回复
热议问题