TweetFollow Us on Twitter

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").

Our Heritage

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.

Terminal Condition

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:

   /Applications/Utilities/

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 /Applications/Utilities/ 

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                                Users
Applications (Mac OS 9)                     Volumes
Cleanup At Startup                          automount
Desktop (Mac OS 9)                          bin
Desktop DB                                  cores
Desktop DF                                  dev
Desktop Folder                              etc
Developer                                   mach
Documents                                   mach.sym
Library                                     mach_kernel
Network                                     private
System                                      radiohead
System Folder                               sbin
Temporary Items                             tmp
TheVolumeSettingsFolder                     usr
Trash                                       var

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:

ls /Lib

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:

ls /Applications

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:

ls /Applications/Util

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

We're History

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):

rm ~/Documents/Work/invoices/Dec02.doc

vs.

cd ~/Documents/Work/invoices/
rm Dec02.doc

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.

 

Community Search:
MacTech Search:

Software Updates via MacUpdate

Latest Forum Discussions

See All

Top Mobile Game Discounts
Every day, we pick out a curated list of the best mobile discounts on the App Store and post them here. This list won't be comprehensive, but it every game on it is recommended. Feel free to check out the coverage we did on them in the links... | Read more »
Price of Glory unleashes its 1.4 Alpha u...
As much as we all probably dislike Maths as a subject, we do have to hand it to geometry for giving us the good old Hexgrid, home of some of the best strategy games. One such example, Price of Glory, has dropped its 1.4 Alpha update, stocked full... | Read more »
The SLC 2025 kicks off this month to cro...
Ever since the Solo Leveling: Arise Championship 2025 was announced, I have been looking forward to it. The promotional clip they released a month or two back showed crowds going absolutely nuts for the previous competitions, so imagine the... | Read more »
Dive into some early Magicpunk fun as Cr...
Excellent news for fans of steampunk and magic; the Precursor Test for Magicpunk MMORPG Crystal of Atlan opens today. This rather fancy way of saying beta test will remain open until March 5th and is available for PC - boo - and Android devices -... | Read more »
Prepare to get your mind melted as Evang...
If you are a fan of sci-fi shooters and incredibly weird, mind-bending anime series, then you are in for a treat, as Goddess of Victory: Nikke is gearing up for its second collaboration with Evangelion. We were also treated to an upcoming... | Read more »
Square Enix gives with one hand and slap...
We have something of a mixed bag coming over from Square Enix HQ today. Two of their mobile games are revelling in life with new events keeping them alive, whilst another has been thrown onto the ever-growing discard pile Square is building. I... | Read more »
Let the world burn as you have some fest...
It is time to leave the world burning once again as you take a much-needed break from that whole “hero” lark and enjoy some celebrations in Genshin Impact. Version 5.4, Moonlight Amidst Dreams, will see you in Inazuma to attend the Mikawa Flower... | Read more »
Full Moon Over the Abyssal Sea lands on...
Aether Gazer has announced its latest major update, and it is one of the loveliest event names I have ever heard. Full Moon Over the Abyssal Sea is an amazing name, and it comes loaded with two side stories, a new S-grade Modifier, and some fancy... | Read more »
Open your own eatery for all the forest...
Very important question; when you read the title Zoo Restaurant, do you also immediately think of running a restaurant in which you cook Zoo animals as the course? I will just assume yes. Anyway, come June 23rd we will all be able to start up our... | Read more »
Crystal of Atlan opens registration for...
Nuverse was prominently featured in the last month for all the wrong reasons with the USA TikTok debacle, but now it is putting all that behind it and preparing for the Crystal of Atlan beta test. Taking place between February 18th and March 5th,... | Read more »

Price Scanner via MacPrices.net

AT&T is offering a 65% discount on the ne...
AT&T is offering the new iPhone 16e for up to 65% off their monthly finance fee with 36-months of service. No trade-in is required. Discount is applied via monthly bill credits over the 36 month... Read more
Use this code to get a free iPhone 13 at Visi...
For a limited time, use code SWEETDEAL to get a free 128GB iPhone 13 Visible, Verizon’s low-cost wireless cell service, Visible. Deal is valid when you purchase the Visible+ annual plan. Free... Read more
M4 Mac minis on sale for $50-$80 off MSRP at...
B&H Photo has M4 Mac minis in stock and on sale right now for $50 to $80 off Apple’s MSRP, each including free 1-2 day shipping to most US addresses: – M4 Mac mini (16GB/256GB): $549, $50 off... Read more
Buy an iPhone 16 at Boost Mobile and get one...
Boost Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering one year of free Unlimited service with the purchase of any iPhone 16. Purchase the iPhone at standard MSRP, and then choose... Read more
Get an iPhone 15 for only $299 at Boost Mobil...
Boost Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering the 128GB iPhone 15 for $299.99 including service with their Unlimited Premium plan (50GB of premium data, $60/month), or $20... Read more
Unreal Mobile is offering $100 off any new iP...
Unreal Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering a $100 discount on any new iPhone with service. This includes new iPhone 16 models as well as iPhone 15, 14, 13, and SE... Read more
Apple drops prices on clearance iPhone 14 mod...
With today’s introduction of the new iPhone 16e, Apple has discontinued the iPhone 14, 14 Pro, and SE. In response, Apple has dropped prices on unlocked, Certified Refurbished, iPhone 14 models to a... Read more
B&H has 16-inch M4 Max MacBook Pros on sa...
B&H Photo is offering a $360-$410 discount on new 16-inch MacBook Pros with M4 Max CPUs right now. B&H offers free 1-2 day shipping to most US addresses: – 16″ M4 Max MacBook Pro (36GB/1TB/... Read more
Amazon is offering a $100 discount on the M4...
Amazon has the M4 Pro Mac mini discounted $100 off MSRP right now. Shipping is free. Their price is the lowest currently available for this popular mini: – Mac mini M4 Pro (24GB/512GB): $1299, $100... Read more
B&H continues to offer $150-$220 discount...
B&H Photo has 14-inch M4 MacBook Pros on sale for $150-$220 off MSRP. B&H offers free 1-2 day shipping to most US addresses: – 14″ M4 MacBook Pro (16GB/512GB): $1449, $150 off MSRP – 14″ M4... Read more

Jobs Board

All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.