oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

BBEdit: Its Unix Support Doesn't Suck Either, Part 1
Pages: 1, 2

Shell Worksheets

If you have been doing Macintosh development for a while, you have probably heard of, or used, Apple's Macintosh Programmer's Workbench (MPW). MPW was an environment for developing applications for 68k and PowerPC machines running Mac OS. It contained all the tools you would expect from an advanced development environment including an editor, compiler, build tools, a debugger, and shell.

An interesting feature of MPW was its blending of a command-line environment with an IDE. To perform development tasks you entered commands into what was called the MPW Shell. The MPW shell was a text window that also functioned as a command executor. It did not use a Unix shell like bash or tcsh, but rather its own set of built-in commands and scripting language.

BBEdit carries on this tradition with something it calls "Shell Worksheets." A shell worksheet is similar to a MPW Shell, but uses a Unix shell for command processing. Worksheets function as free-form shells, into which you enter text as well as commands you wish to execute. When you enter a command, BBEdit executes the command and prints the result into the worksheet.

For users who are used to a shell, this can feel a bit strange at first. For example, hitting the up arrow will not recall the previous command, but rather moves the cursor up a line. But if you stay with it, you will begin to see its usefulness.

When you use worksheets, there are a few things to keep in mind. First, the working directory for a new worksheet is your home directory. Worksheet commands run under your user name, and are therefore restricted to those permissions. If you need to run a command as root, click on the padlock icon in the toolbar (at the top of the worksheet) and enter the root password. Now, commands are executed as root.

When you execute a process in a worksheet, BBEdit displays the name of the process in the Windows status area, which is located at the bottom-left of the worksheet. To exit a running process, type Control-C or Command-Period. Finally, you can save your worksheets as you would any other BBEdit document, and retrieve them for later use. This is really handy for getting an audit of a session or saving a set of commands.

Now that you understand the basics, let's take a look how to use worksheets. There are two ways to open a new worksheet. The first is by selecting File-->New-->Shell Worksheet ([your-default-shell]). BBEdit opens a new worksheet and uses your default shell as the command executer (the shell is listed in the menu item in parenthesis). The other way of creating a new worksheet is by selecting File-->New-->Shell Worksheet..., which enables you to open a new worksheet and choose a shell.

The first thing you will see when BBEdit opens a worksheet is some text being inserted into the worksheet's text area. This text comes from a file called "Default Worksheet Stationery," which is located in the "BBEdit 7.1/BBEdit Support/Stationery" folder. When BBEdit opens a worksheet, it looks for this file and, if present, loads the contents into the new worksheet. The worksheet stationery that ships with BBEdit contains a few examples of the things you can execute in a worksheet. Once you are comfortable with worksheets, you can replace the contents of the stationery file with your own text.

You execute commands in a worksheet in a few ways. One way is to type the command and press either Command-Return or the Enter key. To execute multiple commands, select the commands and press either Command-Return or the Enter key. Another way is to select the commands and click on the status area.

In all cases, the result of the command appears on the line after the executed command or commands.

As you can imagine, worksheets give you lots of options. Let's look at a few examples of what you can do with worksheets.

Imagine you are doing something in BBEdit and need to perform a quick calculation. Rather than jumping out to the Mac OS X Calculator application, use a worksheet instead. Place the cursor on an empty line of a worksheet and execute the bc program. The bc program is an arbitrary precision calculator language that you can use to perform simple, as well as complex, calculations. (The bc program comes with most Unix distributions. See its man page for more information.)

To exit bc, type Control-C.

Another thing you can do is display the contents of a man page in a worksheet. Imagine you are writing some network code and need some help with a function. For example, executing the command "man 2 socket" in a worksheet causes BBEdit to display the man page for the socket(2) function, completely formatted.

Finally, here's a hack for listing the songs on your iPod, copying songs from your iPod to iTunes, and playing a song in iTunes -- all from within BBEdit.

(NOTE: For readability, some of the following lines have extra line breaks inserted. Any indented line should be part of the preceding line.)

  # -- List all songs on your iPod.
  % find /Volumes/Kevin_iPod/iPod_Control/Music -print
  # -- Find a song on your iPod.
  % find /Volumes/Kevin_iPod/iPod_Control/Music -print | grep Piano
  # -- Find a song on your iPod and add '\' before each space in path.
  % find /Volumes/Kevin_iPod/iPod_Control/Music -print | awk '{ gsub(/ /, "\\ "); 
      print }' | grep Piano
  # -- Play/copy a song (iTunes).
  % open /Volumes/Kevin_iPod/iPod_Control/Music/F00/Concerto\ for\ Piano\ and
      \ Orch.m4a
  # -- Copy a set of files from your iPod to iTunes.
  % find /Volumes/Kevin_iPod/iPod_Control/Music -print | awk '{ gsub(/ /, "\\ "); 
      print }' | grep Piano | xargs open

For these commands to work, you will need to plug in your iPod and replace the iPod path (/Volumes/Kevin_iPod/iPod_Control/Music) with the path to your iPod. (To get the path, execute the df command in the worksheet or shell.)

Overall, worksheets are pretty useful. They do not provide you with all the features of a shell, but do give you the ability to execute many shell commands within the BBEdit environment.

Next Installment

In Part 2, this coming Friday, I'll cover Unix Filters and Scripts, and all sorts of odds and ends including CVS, CodeWarrior, Xcode, and a good list of Emacs Key Bindings. See you then!

Kevin O'Malley is a long time Macintosh and UNIX developer. His articles have appeared in Dr. Dobb's Journal, IEEE Internet Computing, and The Perl Journal, and he is the author of Programming Mac OS X: A Guide for UNIX Developers.

Return to