Do something with selected text? 

Newsgroups: comp.os.linux.x
Date: Mon, 27 Jan 2003 02:05:49 GMT
: Is there a way to execute a script/program on selected text?

There are several ways. Most trivially, if you have a URL selected, you can simply point at a netscape window and middle-click, and netscape will open that URL. Unix/linux netscapes are set up to do that as-is.

More complicated, note that if the current X selection has the general appearance of a URL, KDE normally offers to open that URL for you with mozilla/konqueror or whatnot. As soon as you stop dragging to select, if you selected something starting with http://, it'll pop up a dialogue to ask you what to do with it. You can customize this. If you aren't already using KDE, adopting it just for this is a bit overkill, but the facility is there, and scriptable (which netscape is far less so).

The two ways I most often manipulate the X selection are with use tcl/tk (or other bindings of the tk toolkit, like perl/tk), and with use of a program like xselection.

In tcl/tk, there's a "selection" command, which can be used to return the current X selection inside tcl scripts. Or perl, or whatever. See http://tcl.sourceforge.net/faqs/ A shell command to demonstrate it minimally might be

echo 'puts -nonewline [selection get]; exit' | wish -

or as a separate tcl script in linux

#!/usr/bin/wish -f
puts -nonewline [selection get]
exit

These both will write the value of the current X selection to stdout and then exit. Therefore, you can write shell (or tcl) scripts to do whatever you want to the X selection, and most window managers have a way to execute arbitrary shell scripts via pull-down menu or buttonclick on the background, or whatever. If you select a URL, and then in a wish session (ie, the tcl/tk script interpreter, the (wi)ndowing (sh)ell) you can type

exec netscape -remote "openURL([selection get]"

or if you have the above script stored in file "selectionget", a normal shell command

netscape -remote "openURL(`selectionget`)"

and either of these will open the appropriate page. Or consider a wish script like this one

#!/usr/bin/wish -f
pack [button .openURL -text openURL -command \
      {exec netscape -remote "openURL([selection get])"}]

It will put up a button on which you can click to open the the current X selection as a URL with netscape.

In short, tcl/tk is a very effective way to script all kinds of X windows interactions, and integrate them into unix-style pipelines, or into normal unix scripting methods.

Next consider xselection.

ftp://ftp.x.org/R5contrib/xselection.tar.Z

If you download and compile it (in the usay way for contributed X apps), you use it from the shell to both get and set the X named selections, and cutbuffers. For example, the shell case above can be done like so

netscape -remote "openURL(`xselection PRIMARY`)"

Not as powerful or general-purpose as tcl/tk, but it does a good job of manipulating the various ways programs can cut/paste/select in X in normal unix scripts or pipelines.

Wayne Throop