1

I have a very simple script in python which generates a sentence.
For simplicity let's say I have :

var = "hello world"

I want to be able to paste (cmdV) the string, directly after running the script.

Any ideas?

Mundi
  • 79,884
  • 17
  • 117
  • 140
oopsi
  • 1,919
  • 3
  • 21
  • 28

2 Answers2

3

You could use NSPasteboard from AppKit

def sendToClipBoard(string):
    from AppKit import NSPasteboard,NSObject,NSStringPboardType
    pasteboard = NSPasteboard.generalPasteboard()
    emptyOwner = NSObject.alloc().init()
    pasteboard.declareTypes_owner_([NSStringPboardType], emptyOwner)
    pasteboard.setString_forType_(string, NSStringPboardType)

Code copied from Notestoself

Vortexfive
  • 1,889
  • 13
  • 19
2

You could call the pbcopy command from within your script. For example:

from subprocess import call
var = "Hello world!"
cmd = 'echo "%s" | pbcopy' % var
call([cmd], shell=True)
Matthew Adams
  • 9,426
  • 3
  • 27
  • 43