Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Filter GUI text input in place UNIX
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.
    •    
  • Currently 1.50 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[7,619 views]  

Filter GUI text input in place | 10 comments | Create New Account
Click here to return to the 'Filter GUI text input in place' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
use the services menu
Authored by: SOX on Jun 30, '04 12:02:49PM

What you are talking about is exactly what the apple services menu is for. look under the menu item ajacent to the apple menu (i.e. the menu titles wit the name of the app). See the services menu. that's how those work.



[ Reply to This | # ]
use the services menu?
Authored by: osxpounder on Jun 30, '04 04:25:51PM

I read this hint, and SOX's comment, but I have to confess I still don't know what the heck either of you are talking about. It's not important -- I guess if I didn't know about it, and I still don't understand, I probably don't have a pressing need to know. Just curious, especially by SOX's comment, because I see nothing on the Services menu that seems related to the hint.

---
--
osxpounder



[ Reply to This | # ]
use the services menu?
Authored by: gshenaut on Jun 30, '04 07:29:47PM

Yes, this is an esoteric thing. Lets take a trivial example. Suppose I
have typed in a bit of text which has a newline after each line:

The weapons of mass destruction
are buried under my statue.

and I discover I want to encrypt it. I can use my hint (copying it into
the paste buffer, processing it, and pasting it back). In this case, I'll
use "pbpaste | tr A-MN-Za-mn-z N-ZA-Mn-za-m | pbcopy" on a copy of
it to get this:

Gur jrncbaf bs znff qrfgehpgvba
ner ohevrq haqre zl fgnghr.

I suppose there are more impressive examples.

Unix is a very filtery operating system, and I think it's hand to have a
fairly easy to integrate that functionality with the GUI.

And I don't understand the Services comment either.

Greg Shenaut



[ Reply to This | # ]
use the services menu?
Authored by: Lankhmart on Jun 30, '04 08:54:53PM

With SilverService, you can turn any arbitrary shell command or script into a system-wide service.

For instance: I entered your "tr" command into SilverService, and now I can highlight some text in any Service-aware application, go to Services-->SilverService-->ROT13 and the text will be transformed right in place without any need to pipe from and to the clipboard.



[ Reply to This | # ]
use the services menu?
Authored by: gshenaut on Jun 30, '04 10:13:11PM
Yeah, I tried that with Platypus but it didn't work. It was as if the paste buffer was cleared when the embedded script started up. Maybe SilverService is smarter about that than Platypus.

And, I like the way goes into the services menu. Very interesting.

I'm going to give it a try, thanks.

Greg Shenaut

[ Reply to This | # ]

Text Wielder
Authored by: zacht on Jul 01, '04 06:19:10AM

Versiontracker has some beta software called "Text Wielder" which seems to consist mainly of a bunch of text-manipulating scripts to be used as Services. You can modify the included scripts or add your own.

For my part, I decided that it was interesting, but I don't use any of these things enough to justify trying to get used to using them. For those who use text filters and scripts more often, I hope this might be an interesting pointer.

zach



[ Reply to This | # ]
Consider "TextExtras"
Authored by: lsequeir on Jul 01, '04 06:57:01AM

I also loved old "vi"'s ability to use any filter, in the manner gshenaut described. I used to miss that on the GUI editors.

Someone directed me to a free software called "TextExtras". This is just wonderful. It has a lot of features, but let just mention the one that is most relevant for this thread:

Select any text in *any* Cocoa application, hit Command-| (that's a vertical bar - very appropriate!) and up comes a dialog that let's you enter what command to filter through and what to do with the result (for example, "replace the selection" mimics the vi behavior).
So, for example, you can use the cli "sort" or, for that matter, any awk program or whatever, in TextEdit (or SubEthaEdit or TeXShop), just like you could in good'ole vi.


---
Luís



[ Reply to This | # ]
Consider "TextExtras"
Authored by: gshenaut on Jul 01, '04 09:54:56PM
OK, I really like TextExtras. But there are a couple issues I have with it: first, the Command-| hotkey didn't work, I don't know why. I finally figured out how to make a "User Scripts" menu to show up on the menu bar, and that's OK. Second, I didn't really like their pipeline command too much--there are too many options on it, it won't use the user's default shell, and it doesn't display error messages. So, here's my version of the pipeline command, called "aaafilter.sh" (the "aaa" is so it goes at the top of the menu):
#! /bin/sh
#
# filter.sh - uses cocoadialog to get a command to filter selection or file
#
# -- TextExtras User Script Info --
# %%%{TEName=Filter Selection}%%%
# %%%{TEInput=Selection}%%%
# %%%{TEOutput=ReplaceSelection}%%%
# %%%{TEKeyEquivalent=}%%%
#
# %%%{TENewScript}%%%
# %%%{TEName=Filter File}%%%
# %%%{TEInput=AllText}%%%
# %%%{TEOutput=ReplaceAllText}%%%
# %%%{TEKeyEquivalent=}%%%

CoDi=$HOME/Applications/CocoaDialog.app/Contents/MacOS/CocoaDialog
Title="Enter command or pipeline"
Info="Filter text through command or send text to command"
T=/tmp/filt$$
trap "rm -f $T.*" EXIT

x=`$CoDi inputbox --no-newline --title "$Title" --informative-text "$Info" --button1 Filter --button2 Send --button3 Cancel | tr '\n' ' '`
y=${x#? }
code=${x% $y}

case $code in
	1 )
		pipeline=$y
	;;
	2 )
		pipeline=$y
	;;
	* )
		exit 0
	;;
esac

# use the user's shell to run the filter
$SHELL -c "$pipeline" > $T.out 2> $T.err

# if there was any error output, display it
if [ -s $T.err ] ; then
	msg="`cat < $T.err`"
	x=`$CoDi ok-msgbox --no-newline --style warning --title "Error message from $pipeline" --text "$msg"`
	if [ "$x" != "1" ] ; then
		rm -f $T.*
		exit 0
	fi
fi

# if there is any output from a send, show it in a window
if [ $code != 1 ] ; then
	if [ -s $T.out ] ; then
		$CoDi textbox --text-from-file $T.out --button1 OK --title "Output from $pipeline" > /dev/null
	fi
	rm -f $T.*
	exit 0
fi

# if there is any output from a filter, use it to replace
# the text, otherwise leave it unchanged.
if [ -s $T.out ] ; then
	echo -n "%%%{TESelection}%%%"
	cat $T.out
	echo -n "%%%{TESelection}%%%"
fi

rm -f $T.*
exit 0
It does more or less the same thing as the built-in pipeline, but to me it seems more convenient. You have to have CocoaDialog for it to work.

Greg Shenaut

[ Reply to This | # ]

Full circle
Authored by: gshenaut on Jul 02, '04 12:35:45AM
OK, this is great stuff. First, on the aaafilter.sh script I posted previously, it may be necessary to insert code to set the PATH to something other than the default, or otherwise initialize the shell, to let it see all the possible filter commands you want to run. On my system, I can do ". /etc/profile" in the script for this, but YMMV.

Second, I made this little script, which I call "xvi":

#!/bin/sh

T=/tmp/xvi$$
trap "rm -f $T" EXIT
trap "rm -f $T;exit" INT

cat > $T

xterm -e vi $T

cat $T
exit 0
which, if typed into the filter window will start up vi on the selected text in an X window. When you write it out, the result will replace the selected text. You could use your favorite editor instead of vi if you want.

Bottom line: with this trick, you can now use any editor you want to edit text in GUI programs like Mail.app, TextEdit, and so on, and you can do all the text filtering you want.

Cheers,

Greg Shenaut

[ Reply to This | # ]

Filter GUI text input in place
Authored by: osxpounder on Jul 01, '04 12:46:35PM

Thanks, folks, for the kind explanations. I think I have a much better idea of the meaning and power of this hint now. Great!

---
--
osxpounder



[ Reply to This | # ]