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

10.6: Service to filter selected text using a shell command System 10.6
Snow Leopard only hintHere's a little OS X service to filter selected text (from any application) through a shell command.

This can be handy for performing quick operations, like replacing text with regular expressions, sorting lists or swapping fields around.

Grab the Filter through Shell Command service, extract the zip file and put the service into /Library/Services. You may need to authenticate to do this. It should then appear in the Services menu.

When triggered, the service requests a command to use for filtering, then runs the command and replaces the selected text with the result.

Some sample operations:
  • Sort lines alphabetically/numerically: sort or sort -n
  • Change to lowercase: tr "[:upper:]" "[:lower:]"
  • Replace a spelling mistake, taking care of case: sed -E 's/([tT])eh/\1he/g'
  • Re-order elements in a tab- or comma-separated list: awk '{print $2 $1}' or awk -F, '{print $2 "," $1}'

[crarko adds: I tested this, and it works as described in TextEdit. BBEdit did not accept the command for me.]
  • Currently 2.50 / 5
  You rated: 3 / 5 (20 votes cast)

10.6: Service to filter selected text using a shell command | 2 comments | Create New Account
Click here to return to the '10.6: Service to filter selected text using a shell command' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.6: Service to filter selected text using a shell command
Authored by: MJCube on May 24, '10 01:33:02PM
A couple of notes:

Case changing and many other simple text operations in Cocoa apps can be done by simply changing the default key bindings, as described in this hint from 2006.

BBEdit (and TextWrangler) fully support unix scripts and filters, but only via their own menus and palette windows.

[ Reply to This | # ]

10.6: Service to filter selected text using a shell command
Authored by: BlackPenguin on Jun 05, '10 07:51:17AM
As MJCube noted, this will not work in BBEdit or TextWrangler, but those apps do support AppleScript and shell filters. For those who are interested, here is a quick AppleScript that will perform essentially the same task in TextWrangler that this service performs elsewhere:

tell application "TextWrangler"
	set selText to selection of window 1 as string
	set theResult to display dialog "Enter shell command to filter selected text through:" default answer "sort"
end tell

if button returned of theResult is "OK" then
	set theRes to do shell script "echo " & quoted form of selText & " | tr '\\r' '\\n' | " & text returned of theResult
end if

tell application "TextWrangler"
	set selection to theRes
end tell
This was thrown together just as a proof-of-concept, and it leaves something to be desired with respect to error checking; that's left as an exercise to the user. Hope this helps!

[ Reply to This | # ]