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


Click here to return to the 'Consider "TextExtras"' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
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 | # ]