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

Tokkun Studio unveils alpha trailer for...
We are back on the MMORPG news train, and this time it comes from the sort of international developers Tokkun Studio. They are based in France and Japan, so it counts. Anyway, semantics aside, they have released an alpha trailer for the upcoming... | Read more »
Win a host of exclusive in-game Honor of...
To celebrate its latest Jujutsu Kaisen crossover event, Honor of Kings is offering a bounty of login and achievement rewards kicking off the holiday season early. [Read more] | Read more »
Miraibo GO comes out swinging hard as it...
Having just launched what feels like yesterday, Dreamcube Studio is wasting no time adding events to their open-world survival Miraibo GO. Abyssal Souls arrives relatively in time for the spooky season and brings with it horrifying new partners to... | Read more »
Ditch the heavy binders and high price t...
As fun as the real-world equivalent and the very old Game Boy version are, the Pokemon Trading Card games have historically been received poorly on mobile. It is a very strange and confusing trend, but one that The Pokemon Company is determined to... | Read more »
Peace amongst mobile gamers is now shatt...
Some of the crazy folk tales from gaming have undoubtedly come from the EVE universe. Stories of spying, betrayal, and epic battles have entered history, and now the franchise expands as CCP Games launches EVE Galaxy Conquest, a free-to-play 4x... | Read more »
Lord of Nazarick, the turn-based RPG bas...
Crunchyroll and A PLUS JAPAN have just confirmed that Lord of Nazarick, their turn-based RPG based on the popular OVERLORD anime, is now available for iOS and Android. Starting today at 2PM CET, fans can download the game from Google Play and the... | Read more »
Digital Extremes' recent Devstream...
If you are anything like me you are impatiently waiting for Warframe: 1999 whilst simultaneously cursing the fact Excalibur Prime is permanently Vault locked. To keep us fed during our wait, Digital Extremes hosted a Double Devstream to dish out a... | Read more »
The Frozen Canvas adds a splash of colou...
It is time to grab your gloves and layer up, as Torchlight: Infinite is diving into the frozen tundra in its sixth season. The Frozen Canvas is a colourful new update that brings a stylish flair to the Netherrealm and puts creativity in the... | Read more »
Back When AOL WAS the Internet – The Tou...
In Episode 606 of The TouchArcade Show we kick things off talking about my plans for this weekend, which has resulted in this week’s show being a bit shorter than normal. We also go over some more updates on our Patreon situation, which has been... | Read more »
Creative Assembly's latest mobile p...
The Total War series has been slowly trickling onto mobile, which is a fantastic thing because most, if not all, of them are incredibly great fun. Creative Assembly's latest to get the Feral Interactive treatment into portable form is Total War:... | Read more »

Price Scanner via MacPrices.net

Early Black Friday Deal: Apple’s newly upgrad...
Amazon has Apple 13″ MacBook Airs with M2 CPUs and 16GB of RAM on early Black Friday sale for $200 off MSRP, only $799. Their prices are the lowest currently available for these newly upgraded 13″ M2... Read more
13-inch 8GB M2 MacBook Airs for $749, $250 of...
Best Buy has Apple 13″ MacBook Airs with M2 CPUs and 8GB of RAM in stock and on sale on their online store for $250 off MSRP. Prices start at $749. Their prices are the lowest currently available for... Read more
Amazon is offering an early Black Friday $100...
Amazon is offering early Black Friday discounts on Apple’s new 2024 WiFi iPad minis ranging up to $100 off MSRP, each with free shipping. These are the lowest prices available for new minis anywhere... Read more
Price Drop! Clearance 14-inch M3 MacBook Pros...
Best Buy is offering a $500 discount on clearance 14″ M3 MacBook Pros on their online store this week with prices available starting at only $1099. Prices valid for online orders only, in-store... Read more
Apple AirPods Pro with USB-C on early Black F...
A couple of Apple retailers are offering $70 (28%) discounts on Apple’s AirPods Pro with USB-C (and hearing aid capabilities) this weekend. These are early AirPods Black Friday discounts if you’re... Read more
Price drop! 13-inch M3 MacBook Airs now avail...
With yesterday’s across-the-board MacBook Air upgrade to 16GB of RAM standard, Apple has dropped prices on clearance 13″ 8GB M3 MacBook Airs, Certified Refurbished, to a new low starting at only $829... Read more
Price drop! Apple 15-inch M3 MacBook Airs now...
With yesterday’s release of 15-inch M3 MacBook Airs with 16GB of RAM standard, Apple has dropped prices on clearance Certified Refurbished 15″ 8GB M3 MacBook Airs to a new low starting at only $999.... Read more
Apple has clearance 15-inch M2 MacBook Airs a...
Apple has clearance, Certified Refurbished, 15″ M2 MacBook Airs now available starting at $929 and ranging up to $410 off original MSRP. These are the cheapest 15″ MacBook Airs for sale today at... Read more
Apple drops prices on 13-inch M2 MacBook Airs...
Apple has dropped prices on 13″ M2 MacBook Airs to a new low of only $749 in their Certified Refurbished store. These are the cheapest M2-powered MacBooks for sale at Apple. Apple’s one-year warranty... Read more
Clearance 13-inch M1 MacBook Airs available a...
Apple has clearance 13″ M1 MacBook Airs, Certified Refurbished, now available for $679 for 8-Core CPU/7-Core GPU/256GB models. Apple’s one-year warranty is included, shipping is free, and each... Read more

Jobs Board

Seasonal Cashier - *Apple* Blossom Mall - J...
Seasonal Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Seasonal Fine Jewelry Commission Associate -...
…Fine Jewelry Commission Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) Read more
Seasonal Operations Associate - *Apple* Blo...
Seasonal Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Read more
Hair Stylist - *Apple* Blossom Mall - JCPen...
Hair Stylist - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Read more
Cashier - *Apple* Blossom Mall - JCPenney (...
Cashier - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Blossom Mall Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.