BBEdit has great support for running scripts from a file, be in Applescript or any shell scripting language. All these scripts live in the Scripts or Text Filters folder in the BBEdit preferences folder (or packaged inside BBEdit Packages).
However, you may find yourself wanting to write a one-off (ad hoc) script - just a one time use script you may never use again.
There are two ways to run a script like this:
The Unix Worksheet Way
I love the Unix Worksheet (Window -> Unix Worksheet). It’s a logical extension to the command line interface: instead of “one shot and you’re done” command input, the Unix Worksheet lets you interact with your command input and output as a changable document. What’s best is that this changable document gives you all the power you expect from BBedit’s tools: find and replace, Prefix/Suffix Lines, etc.
So, my favorite way to run an arbitrary Unix command on a chunk of text is to:
- Copy the text I want to act on to the clipboard
- In the Unix Worksheet:
pbpaste | wc -w
pbpaste is an Apple provided tool which outputs the contents of the clipboard. In English, this command say, “Take the output of the clipboard and run it through wc (word count) returning only the number of words”.
The Unix Worksheet gives me a lot of power: I can quickly run another unix command or two based on the result of my first command.
In The Editor Way
Maybe you don’t like using pbpaste and going over to the Unix Worksheet: you just want to enter a simple unix command and be done with it!
Bare Bones Software used to distribute a Filter Text Through Shell Command script, but it’s currently not available from their website.
However, I’ve rewritten the script, and here it is:
Copy the script into Applescript Editor, and save it to your Scripts folder. Select some text and off you go!
This script has some improvements over the original script: it can replace the current selection with the result of the script or it can simply report the result in a dialog. (The latter is useful for operations like running a word count, where you really don’t want to replace all your hard work with a number.)