If it won't be simple, it simply won't be. [Hire me, source code] by Miki Tebeka, CEO, 353Solutions

Thursday, September 04, 2008

putclip

A "cross platform" command line utility to place things in the clipboard.
(On linux uses xsel)
#!/usr/bin/env python
'''Place stuff in clipboard - multi platform'''

__author__ = "Miki Tebeka <miki.tebeka@gmail.com>"

from os import popen
from sys import platform

COMMDANDS = { # platform -> command
"darwin" : "pbcopy",
"linux2" : "xsel -i",
"cygwin" : "/bin/putclip",
}

def putclip(text):
command = COMMDANDS[platform]
popen(command, "w").write(text)

def main(argv=None):
if argv is None:
import sys
argv = sys.argv

from optparse import OptionParser
from sys import stdin

parser = OptionParser("%prog [PATH]")

opts, args = parser.parse_args(argv[1:])

if len(args) not in (0, 1):
parser.error("wrong number of arguments") # Will exit

if platform not in COMMDANDS:
message = "error: don't know how to handle clipboard on %s" % platform
raise SystemExit(message)

if (not args) or (args[0] == "-"):
info = stdin
else:
try:
infile = args[0]
info = open(infile)
except IOError, e:
raise SystemExit("error: can't open %s - %s" % (infile, e))

try:
putclip(info.read())
except OSError, e:
raise SystemExit("error: %s" % e)

if __name__ == "__main__":
main()

1 comment:

Dotan Dimet said...

I love xsel, interesting to know there's a cygwin equivalent.

Blog Archive