Thursday, August 10, 2006

commandline clipboard under gnome

http://uwstopia.nl/blog/2006/05/command-line-and-the-clipboard

A few days ago I stumbled upon a question in a newsgroup: how to put text on the Gnome clipboard from the command line?

In response, I wrote a small Python script (using PyGTK) that can be used as a pipe in your command line scripts. It puts all data from stdin on the clipboard. For this to work, you will need the correct $DISPLAY environment variable set (which will be the case most of the time, but it doesn’t work from cron for example).

CLI lovers, download copy-to-clipboard here and rejoice!

Update: Sure, there’s also tons of other solutions like xclip and of course the script could be more like a one-liner, but I like to demonstrate the ease of use Python and PyGTK offer.

---------


#!/usr/bin/env python

import sys

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

buffer = []
for line in sys.stdin:
buffer.append(line)

c = gtk.Clipboard()
c.set_text(''.join(buffer))
c.store()

No comments: