This depends a lot on the OS. On Linux, due to X's bizarre selection model, the easiest way is to use popen('xsel -pi')
, and write the text to that pipe.
For example: (I think)
def select_xsel(text):
import subprocess
xsel_proc = subprocess.Popen(['xsel', '-pi'], stdin=subprocess.PIPE)
xsel_proc.communicate(some_text)
As pointed out in the comments, on a Mac, you can use the /usr/bin/pbcopy
command, like this:
xsel_proc = subprocess.Popen(['pbcopy'], stdin=subprocess.PIPE)
If you want to support different OSes, you could combine different solutions with os.name
to determine which method to use:
import os, subprocess
def select_text(text):
if os.name == "posix":
# try Mac first
try:
xsel_proc = subprocess.Popen(['pbcopy'], stdin=subprocess.PIPE)
except:
# try Linux version
xsel_proc = subprocess.Popen(['xsel', '-pi'], stdin=subprocess.PIPE)
elif os.name == "nt":
# Windows...