The series continues in Learning the Terminal in Mac OS X, Automating Mail from the Mac OS X Terminal, Configuring Email from the Mac OS X Terminal, Customizing the Mac OS X Terminal, and Synchronizing Drives with Cron and the Mac OS X Terminal.
In the first three parts of this series, you got your hands good and dirty learning to work the Unix command line. Now it's time to have a bit of fun with a very cool feature of the Terminal application, term files.
Before you get started on this tutorial, you'll need to have the Apple Developer Tools installed, if you don't already. You'll know if they're installed if you have a /Developer directory on your disk. If you don't, and don't have a copy of the installer CD, you can download a disk image free from Apple after signing up for an Apple Developer Connection (ADC) membership here.
As you might have already discovered, Terminal provides you with lots of ways to customize its windows. You can set text and background colors, window size, title bar text, and much more.
These changes become your global defaults (applying to all new windows) when you use the Preferences palette (Terminal --> Preferences), or you can apply them to individual windows by using the Inspector palette (Shell --> Inspector).
Term files are the third method for saving window settings. They allow you to save these individual window settings to a file, which when opened, creates a new window with all of your pre-configured settings. Using term files you can quickly open several new windows, each with different, pre-defined settings.
Things really become interesting, however, when you take advantage of a little known feature of term files, the ability to define a command line to run when the window opens. Doing this allows you to create your own double-clickable run files that open in Terminal within a window of your exact specifications and run any command line (or script file) you can come up with.
This tutorial will walk you through the simple process of creating just such a term file. Along the way, you'll also learn about editing plist-like XML files, installing Unix applications, and even a bit about Internet MP3 streams.
To use as a good example for this tutorial, I've chosen Streamripper, the open source Unix application that allows you to "rip" MP3 files to your hard disk from some streaming MP3 "Internet radio stations," including many of those listed in the iTunes Radio Tuner.
This article introduces the command line version of Streamripper to demonstrate the use of term files. However, if you would like to check out a full Aqua GUI version of Streamripper, you can find one called StreamCatcher here.
You'll learn more about Streamripper in a bit, but first let's configure your Terminal window and save the settings to a term file. Of course you can set up your window to look however you like, but for purposes of the tutorial I'm going to try to make a window that looks something like an LCD display.
To start, open Terminal and its Inspector palette. From the Inspector's pop-up menu select Window. The default width of 80 columns will do nicely, but you'll only need 5 rows of text. Check only the Custom Title checkbox, and in its field enter: "Type Control + C to Stop." These instructions will remind you how to stop Streamripper.
Next, you'll want to pick a nice looking font and color-scheme for the window. I kind of like the look of the iTunes LCD-like window, so we'll try to duplicate that with our Terminal window settings. First, select Text from the Inspector pop-up. I've set the cursor to be as invisible as possible (an underline), and unchecked everything else.
The iTunes window uses 10-point Lucida Grande, but since it isn't a mono-spaced font it doesn't look so good in a Terminal window. So instead, I chose one of the mono-spaced fonts, VT100 12-point, which is close enough.
To set the colors, select Colors from the Inspector's pop-up. Double-click the Background color swatch to open the color palette. To duplicate the iTunes color, I used the magnifying glass tool found at the bottom of the Color palette to grab the color from an iTools window. Do the same with the Cursor color swatch as well.
Finally, to remove the scroll bars from the Terminal window, select Buffer from the Inspector and check to disable the buffer, uncheck line wrapping, and check to scroll to bottom. Your window should now look something like this:
You're almost ready to save these setting as a term file, but before you do, you should first place the window where you would like it to open within your display since this setting is saved in the file as well.
When you're ready to save the file, select Save from the Shell menu. You'll need to save it in the
~/Library/Application Support/Terminal directory. However, it's likely you don't yet have a Terminal folder in
/Library/Application Support, so you'll first need to create one from within the Save dialog box. Name the file "streamripper.term" and save it there.
|Post your Terminal questions and comments right here!|
Notice also the two options at the bottom of that Save dialog box. While they won't apply to this procedure, they can be very useful for other term files you might want to create. The first option allows you to choose either to save the settings for only the "Main Window," that is, the active window, or save the settings for all open windows to a single term file.
The possibilities are indeed endless when selecting the later choice. You could create a term file to open three or four or more windows at once, all laid out neatly across your monitor, each with its own size, color schemes, etc. and each even running a different command! As you get more familiar with Unix, you might consider setting up multiple windows, some logged into different
ssh sessions, another window running
top, and maybe another running Streamripper, for example.
The second option in the Save dialog box allows you to choose that the term file opens when Terminal starts up. You can also make this choice using Terminal's Preference palette, from the Startup pane.
Once the file is saved, you'll see it listed under the Shell-->Library menu item. Each time you select it from the Library, a new window will open with the exact same settings specified in the term file. Try it! Remember, the placement of each window is the same, so they will open stacked on top of each other. (This is but one way to quickly access your term files.)
Before installing Streamripper, you'll first need to make sure the directory
it will go into actually exists. The
mkdir command with its
flag will create the
bin directory and any intermediate
directories if they don't already exist. Use sudo since these need to be
[localhost:~] chris% sudo mkdir -p /usr/local/bin
It's now time to download and install Streamripper. Clicking this link will download the source code as a zip-compressed "tarball":http://streamripper.sourceforge.net/dl/streamripper-1.0.5.tar.gz
A tarball is a single archive file the tar utility produces by combining the many files contained in the typical Unix source code distribution. Since the files are only combined, not compressed, tarballs are then usually zip-compressed as well, thus the ".tar.gz" extensions.
The latest version of Aladdin Expander will happily post-process this file (and might have done so automatically after download). You can also unpack zipped tarballs from the command line with this command, which will both decompress the zip file, and recreate the full directory structure from the tar archive, using
[localhost:~/Desktop] chris% tar xzvf streamripper-1.0.5.tar.gz
Whether Expander does the job, or
tar, you will end up with a
streamripper-1.0.5 directory in the same directory as the downloaded file. (Remember, of course, that the version number in the name could be newer.)
Now step into that folder from the command line:
[localhost:~/Desktop] chris% cd streamripper-1.0.5
As you install more Unix software, you'll find the next three commands to be a familiar part of the process. There's quite a bit behind these commands, but I can give you some of the basics of what's going on.
Run this command first:
[localhost:~/Desktop/streamripper-1.0.5] chris% ./configure
What you're doing is executing a shell script that discovers your machine and OS properties (remember this is the same portable source code that will work on most other Unix machines) in preparation for actually compiling the code in the next step, which you're ready to perform once you get a new prompt:
[localhost:~/Desktop/streamripper-1.0.5] chris% make
The compile will take a few moments while you watch the lines of output from each step roll by. Once your prompt returns, you might want to have a new look in the directory; you'll see the newly hatched Streamripper binary, the actual application file that now just needs to be placed in the proper directory. You'll do this with the next command, which requires
sudo since the file is going into /
usr/local/bin/ (a directory only writable by root):
[localhost:~/Desktop/streamripper-1.0.5] chris% sudo make install - Installing streamripper
And that's it! Streamripper is now installed, and you can take it for a quick spin. Entering just its name gets you some usage information:
[localhost:~/Desktop/streamripper-1.0.5] chris% streamripper Usage: streamripper URL [OPTIONS] Options: -d <dir> - The destination directory -s - Don't create a directory for each stream -r <base port> - Create a relay server on base port, defaults to port 8000 -z - Don't scan for free ports if base port is not avail -p <url> - Use HTTP proxy server at <url> -o - Write over tracks from incomplete -c - Don't auto-reconnect -v - Print version info and quite -l <seconds> - number of seconds to run, otherwise runs forever -q - add sequence number to output file -i - dont add ID3V1 Tags to output file [localhost:~/Desktop/streamripper-1.0.5] chris%
streamripper instead results in
found, run the
rehash command, which will force the
shell to rebuild its list of known executables:
[localhost:~] chris% streamripper streamripper: Command not found. [localhost:~] chris% rehash [localhost:~] chris% streamripper Usage: streamripper URL [OPTIONS] ...
There are a few options, as you can see, but for our purposes only a URL and target directory are necessary. In fact, finding a URL might be the hardest part. The easiest way to find one is by going to SHOUTcast (a popular portal for streaming MP3 stations), finding a station, and control-clicking on its "Tune In!" button. Choose to copy the link to the clipboard, paste that text somewhere, and copy out just the part of it containing an IP and port number. For example, from this URL:
you would want to extract
22.214.171.124:8046. Once you have it, you can use it in the Streamripper command like this:
[localhost:~] chris% streamripper 126.96.36.199:8046 -d ~/Music/
The directory following the
-d flag is where the ripped files will end up, in this case my home directory's Music folder.
This is what you'll see after pressing Return:
Connecting... stream: Japan-A-Radio - Anime Music & Japanese Pop (JPOP / Anime) server name: SHOUTcast/SolarisSparc v1.8.8 bitrate: 128 meta interval: 24576 [ripping... ] SES - Love isDay By Day [ 120kb]
To stop the rip, press Control + C:
shuting down bye.. [localhost:~] chris%
If you let Streamripper run long enough, you'll see the song files start to fill up your selected destination directory, nicely named with the song titles. You'll learn lots more about Streamripper just by playing with it, from its README file in its source directory, and also from its home page.
But it's now time to return to our term file and add the Streamripper command (and one other nice touch). As you might know, the official preference file format for Mac OS X is the XML plist. I won't go too much into XML here, but what's important to note is that all of these files are plain text, editable from any text editor. Even more relevant is the fact that term files are also written in XML, exactly like plist files.
Now while it's true we can perform the next step using any text editor, since you have the Developer Tools installed you have an even better tool for the job, the PropertyListEditor application, which you'll find in
/Developer/Applications. So go ahead and open PropertyListEditor.
From the File menu, select Open, navigate to
~/Library/Application Support/Terminal, and open the
streamripper.term file. Like plist files, the data in term files are arranged in a hierarchy with a "Root" level at the top. Clicking the Root level's disclosure triangle will expose the next level down, "WindowSettings." Disclose that level, and the "0" level below it and you'll then see the complete list of the term file’s properties, most of which will look familiar.
Next, scroll down to the "ExecutionString" line and double-click on its field in the "Value" column, which will select it for text entry.
Enter this string:
streamripper 188.8.131.52:8046 -d ~/Music/;open ~/Music/Japan-*;exit
And press Return to close that field. The string you entered is a single command line made of three commands, each separated by a semicolon. Joining commands in this way allows you to run commands sequentially (the next one running only when the previous has finished), just as if you entered them on three different lines. Therefore, the line you entered is equivalent to you first running Streamripper:
streamripper 184.108.40.206:8046 -d ~/Music/
and then once you've stopped the ripping, issuing this command:
open command opens the destination folder in the finder, allowing you to see your freshly ripped files. Since the destination folder is named for the "station" providing the stream, the pathname will, of course, be different for different stations. The
* in the pathname is a wildcard character, letting you designate the directory by only its first few characters. Finally, the
exit command terminates your shell session (always a good idea when you're done).
Now for the finishing touch. Scroll down to the "TerminalOpaqueness" line. This hidden setting allows you to set the window's transparency, which will give our window a realistic, glass-like appearance. The value you need to enter is the decimal equivalent of the desired percentage of opaqueness. The default setting of 1.0, then, results in a 100% opaque window. A good setting for our window will be .85, which will be just a bit transparent. Enter ".85" in the value field and press Return.
We're done with the term file and now only need to save it. From the File menu, select Save As and save the file with an appropriate new name (still appended with ".term") in the same directory as the original.
You might need to restart Terminal to see the file in your Library menu. Once it's there, select it and see your creation:
Finally, here are a few more points regarding term files and Streamripper that will help you make your own version of this file (and others):
Previously in the Series
crontabcommand line can reference a shell script, so can the command line you add to your term file, allowing you to do just about anything with a term file.
.commandat the end. Opening a command file in Terminal will open a new default window in which to run the command. This differs from a term file, which specifies the properties of the window. You'll find more about command files here.
-r(relay) flag to the Streamripper command, like this:
streamripper 220.127.116.11:8046 -r -d ~/Music/
With the procedures you've learned in this tutorial, you'll be able to take even more advantage of Mac OS X's Unix roots, as well as Terminal's unique capabilities. If you come up with any interesting term files of your own, feel free to share them in TalkBack.
Copyright © 2009 O'Reilly Media, Inc.