Filter GUI text input in place

Jun 30, '04 09:25:00AM

Contributed by: gshenaut

It is commonplace in UNIX to filter sections of text files through arbitrary pipelines. For example, in vi, to reformat a paragraph with standard newlines on each screen line, you can use !}fmt. There are other uses too, such as reordering columns, doing certain kinds of arithmetic, and so on. I don't know how to do this in a GUI, but here is a way to mimic it using the pbcopy and pbpaste utilities:

In the GUI app, select a region of text you wish to filter, and copy it into the paste buffer using Command-C. Then run the pipeline pbpaste | xyz | pbcopy in a Terminal or xterm window (where xyz is the filtering command, such as fmt(1) or par(1)). Then foreground your original GUI window (with the text region still selected) and hit Command-V. The filtered text will replace the original text. I think this adds a lot of power to all programs that have access to the Command-C / Command-V paste buffer functions. To save typing, I have shell scripts such as pbp, which filters the paste buffer's contents through the par utility.

Maybe some kind soul could take this to the next step, which would be to use AppleScript's do script command to map some key combination to one or more of these filters. This would allow some of the more common filters to be applied without switching to a Terminal window.

Comments (10)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20040628132255813