MacTech Magazine Article Archives
Volume Number: 19 (2003)
Issue Number: 5
Column Tag: Mac OS X Programming Secrets
Mac OS X: Wading In
by Scott Knaster
This is the first episode of Mac OS X Programming Secrets, and there are about 12 billion possible topics to write about in this rich, largely unexplored operating system that's gradually but inevitably taking over the Macintosh world. Most months, this column will focus on cool programming things, especially the nifty newest ones that Apple tosses our way. Rather than present a programming topic this month, we're going to discuss a collection of tips and schticks you can use to improve your daily OS X work and play, whether writing code or just nerding out. Some of these might be familiar to you, but I'm guessing you'll find at least one or two that will make you say "cool!" (or at least "oh").
Before we start in with the tips, let's take a moment to establish some common ground. Mac OS X is a strange and wonderful aggregation of very different technologies: Macintosh meets Unix, by way of NeXT. Apple performed a remarkable engineering feat in lifting up Mac OS and shoving Unix into place underneath. Not only does this affect the tools and applications we use, it's also reflected in the human community of Mac programmers. Some of us, including me, grew up with the "classic" Mac, while other OS X devotees come from the land of Unix. Perhaps the luckiest/smartest OS X programmers are those who stuck with NeXTSTEP through all these years. And the better OS X gets, the more Windows programmers are peeking into the tent to see what all the fuss is about.
Since the Mac was introduced, one of its biggest attractions has been that it avoided the command line goofiness that used to rule the world, so a lot of us have spent the past 20 years mostly staying away from Unixy things. Of course, the user interface for Mac programming tools has always been a source of controversy: sure (says the argument), pretty and easy user interfaces are good enough for mere users, but real programmers are different. They need the power and control of command-line tools. This debate has resulted in divergent philosophies in programming tools, with slick integrated development environments (IDEs) on one side and command-line shells on the other. Most modern programmers end up with some of each, with graphical user interfaces on some tools and command line connections to others, although there are purists who live and die in the shell and won't go near a pretty IDE even if you offer them unlimited Jolt Cola.
I'm an old Mac guy who saw the GUI light in the early '80s. I prefer the boulevard ride that Aqua provides, but I've spent many an hour careening through command lines and I'm not averse to going there when power, control, or necessity demands, as it often does in these early days of OS X. Plus, figuring out something nerdy thrills me as much as it does the next geek. So while I'll usually be looking for an easy Aqua solution to problems in this column, we'll have plenty of fun under the hood in the command line too. The great thing about Mac OS X is that it lets us mix and match Aqua apps with command line tools, and it even provides some bridges between the worlds.
The Terminal application is the trap door that gets you from nice Aqualand, where all the natives know how to point and click, to the shell, that place of exposed wiring and ductwork, absolute power, and little protective fencing. As you likely know by now, Apple puts Terminal in the Utilities folder of your Applications folder, where your mother won't stumble upon it. In a Unix pathname, you would refer to this location as:
Terminal starts a session with a Unix shell, an interactive program that lets you issue commands to your computer, which it then obeys precisely and without question. Of course, your idea of the command might not always be the same as the shell's interpretation - but it always wins. By default, Terminal uses a shell called tsch, one of several that come with your Mac.
You'll spend a lot of your Terminal time typing pathnames of files and directories. For example, if you wanted to see a list of Terminal's fellow utilities, you could type
ls is the shell command for listing files in a directory, and /Applications/Utilities/ is the argument, or the directory we want to see listed. (I don't want to get off on a rant here, but many shell command names aren't just non-mnemonic, they're wildly inconsistent. But there are zillions of programmers who know them by heart and have been using them since Reagan was president, so things are not likely to change. If you haven't grown up using Unix, you will likely never memorize most of them, so the best thing to do is get used to it and consider adding a sticky note next to your computer until you learn the ones you need.)
To make your pathname-typing life easier, the shell includes a shortcut for autocompleting pathnames. If you press Tab while typing a pathname, the shell will kindly fill out the rest of the pathname, up to the next slash, according to the files in the directory you're addressing. We'll illustrate this valuable trick by getting a listing of /Applications/Utilities/ so we can see what other nerdy goodies are contained inside. First, let's take a look at the root directory:
[neb:~] scott% ls /
Applications (Mac OS 9) Volumes
Cleanup At Startup automount
Desktop (Mac OS 9) bin
Desktop DB cores
Desktop DF dev
Desktop Folder etc
System Folder sbin
Temporary Items tmp
As you probably already knew, the shell shortcut for your root folder is a slash, and if you didn't know that, you just picked up a handy tip. To try the Tab autocomplete feature, open a new Terminal window and type the following:
and then press Tab (don't press return). You'll see that the shell completes the pathname up to the next slash and makes the command ls /Library/ , saving us from typing the rest of the file name. Thanks, shell! We could have gotten the same result by typing ls /L and then pressing Tab. After pressing Tab, the insertion point is now at the end of the second slash, so we could keep typing from there, but we'll just press return to see the contents of /Library/ . Thanks to my buddy ZZ for the clue ticket on this one.
Now that we've seen how the Tab shortcut works, let's get that listing of the Utilities directory. We can reuse the Tab trick at every subdirectory level if we want. Start out by typing ls /App and then pressing Tab. The shell fills out the line to read:
and the Mac beeps at us! So rude! What's going on? Take a look at the listing of the root directory above. Note that there are two entries that start with "App": "Applications" and "Applications (Mac OS 9)". When this happens, the autocomplete feature can't be sure which one we want, so it completes as much as possible, until the names start to differ, and it beeps to let us know that it couldn't finish the job. In this case, the names are the same up until the end of the word "Applications", so that much gets filled in for us. In this case, that happens to be just what we want at this directory level. So we continue our quest to get a listing of /Applications/Utilities/ by typing a little more, until the line reads:
We can then press Tab and the rest of the line is filled out: ls /Applications/Utilities/ . From there, we just press Return and we finally see the listing of Terminal and the Utilities (which would make a great band name, by the way).
[neb:~] scott% ls /Applications/Utilities/
AirPort Admin Utility.app DigitalColor Meter.app
Keychain Access.app AirPort Setup Assistant.app
Directory Access.app NetInfo Manager.app
Apple System Profiler.app Disk Copy.app
Network Utility.app Asia Text Extras
Disk Utility.app ODBC Admin.app
Audio MIDI Setup.app Display Calibrator.app
Print Center.app Bluetooth File Exchange.app
EarthLink Process Viewer.app
Bluetooth Serial Utility.app Grab.app
Software Restore.app Bluetooth Setup Assistant.app
Installer.app StuffIt Expander.app
CPU Monitor.app IntelliPoint UnInstaller.app
Terminal.app ColorSync Utility.app
Java iPod 1.2.6 Updater
Console.app Key Caps.app
The thoughtful shell keeps track of all the commands you issue, just in case you ever need them again. If you want to repeat the last command you gave, just press the up-arrow. You'll see the previous command, along with all its options and arguments, retyped on the command line. Press return to make it so.
There's a lot more to this trick. You can keep pressing up-arrow to get older and older commands restored to the command line, all the way back to the start of your shell session. This is incredibly handy if you have to type a long, complicated command: with judicious use of the arrow keys (down-arrow moves forward through your old commands), you'll never have to type it more than once per shell session.
If you don't want to use the arrow keys to move through commands one at a time, you can use the history command see every darn command (up to 100) you've issued during this shell session. Here's what it looks like:
[neb:~] scott% history 30
50 14:41 ls /
51 14:41 cd %
52 14:41 cd ~
53 14:41 ls /
54 14:43 ls ~
55 14:43 ls /
56 14:51 ls /Applications ( Mac OS 9 ) /Cleanup
57 14:53 ls /Applications\ \(Mac\ OS\ 9\)/
58 14:53 ls
59 14:54 ls /Library/
60 14:54 ls /Library/
61 15:11 ls /Library
62 15:14 ls /Applications/Utilities/
63 15:14 ls /Applications/
64 15:17 ls /Applications/Utilities/
65 15:18 man ls
66 15:19 ls /
67 15:19 ls /Applications/Utilities/
68 15:25 history
69 15:30 man history
70 15:30 man history
71 15:30 history
72 15:30 history -a
73 15:31 history -h
74 15:31 history -r
75 15:33 cat video
76 15:33 set
77 15:34 history
78 15:35 history -30
79 15:35 history 30
Wow, talk about Big Brother watching you! In this listing, we've actually used one of history's command line options to limit its output to the last 30 commands rather than showing the whole tedious spew. The history command has several other interesting options, including -r to show the list in reverse order (oldest commands first), and various options for saving commands to files for easy recycling.
The history output is good for more than just amusement and quiet contemplation. Each line starts with a number, and you can repeat that line's command just by typing an exclamation point (which, if you want to be taken seriously as a Unix geek, you should call "bang") followed by the line number. For example, to repeat the command that history shows as line 67, you can type !67 and press return.
Cruising in Comfort
As you do your Terminal business, you'll find yourself moving from one directory to another. You probably know that every active shell session has a working directory, which is the directory that's assumed for commands if no directory is specified. For example, if you type ls without any arguments, you'll get a listing of your working directory. You can find out your working directory by using the pwd (print working directory) command.
Whenever you type a command that operates on a file, you have two choices for how to specify the file's directory: you can either give a complete pathname to the file, or you can change your working directory, and then commands will look for files in that directory. Compare these two ways of deleting a file, using the shell command rm (remove):
Both produce the same result, deleting the file Dec02.doc. Changing working directories is a good strategy if you're going to be living in that directory for awhile. It's also useful when you're just getting started with the shell, just to make sure you're working in the directory you think you are. This is especially true if you're doing destructive things like deleting files. When you're just learning to find your way around, you can add to your comfort level by setting the working directory before operating on files. For further comfort, you can use ls after changing working directories as a sort of reality check that you're in the right directory. Don't worry about taking an extra step or two to make sure you're doing the right thing, especially before you do something drastic like deleting files.
Don't Forget: It's a Mac
Mac OS X is a marriage of the Mac and Unix, but a lot of folks tend to forget that you can take advantage of standard Mac stuff when you're using Terminal. For example, you can open as many windows as you want. Every window is a separate shell session. This is useful if you have a shell session or two that's running a lengthy command, but you still want to be able to interact with the shell for directory listings and so on. You can even use different shells in various windows. In Figure 1, we have three shell windows open, each running a different shell. As a Mac guy, a shell is pretty much a shell to me, but I know that each one has its own quirks, charms, and accursed blemishes, so you should choose the one you like the best.
Figure 1. Each shell window is running a different shell: tcsh, bash, and zsh.
Copying and pasting text works just dandy in Terminal windows. You can copy text in any application and paste it into a shell window - the text appears at the insertion point. Drag and drop is another Mac-like feature you can use with the shell. You can select text in a window and drag it to the insertion point, and it will be inserted. This works whether the text comes from a Terminal window - even the same one as the destination - or another application. What's more - and this is really cool - if you drag a file from the Finder and drop it into a shell window, the file's name appears at the insertion point. Somebody was really thinkin' there!
Of course, pure fun is one of the best Macintosh features. Even though Terminal is perhaps the most utilitarian, plain application you can imagine, it's got some fun built in, too. One way to have a good time with Terminal is by customizing the appearance of its windows. In Terminal, choose Terminal --> Window Settings, then pick Color from the Popup at the top of the dialog. This dialog lets you choose different colors for regular, bold, and selected text, the background, and the cursor. You can set color combinations that are gaudy, bizarre, and completely illegible if that's your desire. For even more joy, use the Transparency slider to set how opaque you want the Terminal window to be. The more transparent it gets, the more the window beneath it shines through. This has a very high cool factor indeed, although it's easy to go a little overboard on the transparency. On the other hand, cranking up the transparency is a great way to have some fun with the company Unix god who wants to try out your Mac. (For best results, combine this with the Desktop Preferences setting that changes the desktop picture every 5 seconds.) See Figure 2 to get an idea of how this looks.
Figure 2. Maybe this is a little too transparent.
Go Forth and Nerdify
As you continue on your way toward mastering OS X, you can use the tips in this column and discover more of your own. The more time you spend in Terminal, the more comfortable you'll feel driving around. Remember that for some operations, you have the option of working in the shell or in an Aqua application. For example, when you need to create or edit a text file, you can use a Mac app like TextEdit or BBEdit, you can dive into a command line editor like Pico or vi, or you can switch between all of the above. Which is better? That depends largely on what you're comfortable with. If you come from the Unix tradition, real live Unix editors are there for you. If you're a Mac kinda person, you can use apps that are familiar to you and you won't have to worry about learning vi commands unless you're looking for something interesting to do on a rainy day.
During your Terminal touring, you should become good friends with the man (manual) command, which provides built-in documentation for shell commands. To use it, type man followed by the command you want to learn about, such as man ls or man rm. (Using man occasionally produces some fascinating commands, such as man set, man machine, man top, and even man man. Who says Unix has no sense of humor?)
While you're fooling with Terminal, try lots of stuff that you read about in man pages, but be careful about changing things you didn't really want to change. Play around, experiment, take it slow, and hey, let's be careful out there - but don't forget to have fun!
Scott Knaster has been writing about Macs for as long as there have been Macs. Scott's books How To Write Macintosh Software and Macintosh Programming Secrets were required reading for Mac programmers for more than a decade. Scott wrote developer books for General Magic and worked on Mac software for Microsoft. Scott's books have been translated into Japanese and Pascal. Scott has every issue of Mad magazine, which explains a lot.