Saturday, March 13, 2010

A useful script: copytoclipboard

Another quick tool I figure I'd share. Made this one a while ago. When you're doing stuff on the command line and you need to copy something for a desktop app, you end up having to select text on your terminal screen. Well if it's a big text file, you have to copy, paste, scroll to the next part, copy (make sure you didn't copy any part twice), paste, etc.

Well, copytoclipboard takes standard input and puts it into the gtk clipboard (haven't tried this on KDE).

#!/usr/bin/env python

import sys

import pygtk
pygtk.require('2.0')
import gtk

c = gtk.Clipboard()
c.set_text(''.join(sys.stdin.read()))
c.store()

For instance, to paste this code, I went into my terminal and typed:

cat copytoclipboard | copytoclipboard

and pasted the results here. For a less confusing example:

uptime | copytoclipboard

lets me paste this:

17:42:14 up 3:23, 2 users, load average: 0.35, 0.39, 0.41

EDIT: Or I could just do a google search and find that xclip already exists. Oh well.

EDIT2: No, xclip seems to be its own thing. I just tried it, doesn't seem to work with gtk. I guess I still win!