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

Go from lowly lizard to wicked Wyvern in...
Do you like questing, and do you like dragons? If not then boy is this not the announcement for you, as Loongcheer Game has unveiled Quest Dragon: Idle Mobile Game. Yes, it is amazing Square Enix hasn’t sued them for copyright infringement, but... | Read more »
Aether Gazer unveils Chapter 16 of its m...
After a bit of maintenance, Aether Gazer has released Chapter 16 of its main storyline, titled Night Parade of the Beasts. This big update brings a new character, a special outfit, some special limited-time events, and, of course, an engaging... | Read more »
Challenge those pesky wyverns to a dance...
After recently having you do battle against your foes by wildly flailing Hello Kitty and friends at them, GungHo Online has whipped out another surprising collaboration for Puzzle & Dragons. It is now time to beat your opponents by cha-cha... | Read more »
Pack a magnifying glass and practice you...
Somehow it has already been a year since Torchlight: Infinite launched, and XD Games is celebrating by blending in what sounds like a truly fantastic new update. Fans of Cthulhu rejoice, as Whispering Mist brings some horror elements, and tests... | Read more »
Summon your guild and prepare for war in...
Netmarble is making some pretty big moves with their latest update for Seven Knights Idle Adventure, with a bunch of interesting additions. Two new heroes enter the battle, there are events and bosses abound, and perhaps most interesting, a huge... | Read more »
Make the passage of time your plaything...
While some of us are still waiting for a chance to get our hands on Ash Prime - yes, don’t remind me I could currently buy him this month I’m barely hanging on - Digital Extremes has announced its next anticipated Prime Form for Warframe. Starting... | Read more »
If you can find it and fit through the d...
The holy trinity of amazing company names have come together, to release their equally amazing and adorable mobile game, Hamster Inn. Published by HyperBeard Games, and co-developed by Mum Not Proud and Little Sasquatch Studios, it's time to... | Read more »
Amikin Survival opens for pre-orders on...
Join me on the wonderful trip down the inspiration rabbit hole; much as Palworld seemingly “borrowed” many aspects from the hit Pokemon franchise, it is time for the heavily armed animal survival to also spawn some illegitimate children as Helio... | Read more »
PUBG Mobile teams up with global phenome...
Since launching in 2019, SpyxFamily has exploded to damn near catastrophic popularity, so it was only a matter of time before a mobile game snapped up a collaboration. Enter PUBG Mobile. Until May 12th, players will be able to collect a host of... | Read more »
Embark into the frozen tundra of certain...
Chucklefish, developers of hit action-adventure sandbox game Starbound and owner of one of the cutest logos in gaming, has released their roguelike deck-builder Wildfrost. Created alongside developers Gaziter and Deadpan Games, Wildfrost will... | Read more »

Price Scanner via MacPrices.net

13-inch M2 MacBook Airs in stock today at App...
Apple has 13″ M2 MacBook Airs available for only $849 today in their Certified Refurbished store. These are the cheapest M2-powered MacBooks for sale at Apple. Apple’s one-year warranty is included,... Read more
New today at Apple: Series 9 Watches availabl...
Apple is now offering Certified Refurbished Apple Watch Series 9 models on their online store for up to $80 off MSRP, starting at $339. Each Watch includes Apple’s standard one-year warranty, a new... Read more
The latest Apple iPhone deals from wireless c...
We’ve updated our iPhone Price Tracker with the latest carrier deals on Apple’s iPhone 15 family of smartphones as well as previous models including the iPhone 14, 13, 12, 11, and SE. Use our price... Read more
Boost Mobile will sell you an iPhone 11 for $...
Boost Mobile, an MVNO using AT&T and T-Mobile’s networks, is offering an iPhone 11 for $149.99 when purchased with their $40 Unlimited service plan (12GB of premium data). No trade-in is required... Read more
Free iPhone 15 plus Unlimited service for $60...
Boost Infinite, part of MVNO Boost Mobile using AT&T and T-Mobile’s networks, is offering a free 128GB iPhone 15 for $60 per month including their Unlimited service plan (30GB of premium data).... Read more
$300 off any new iPhone with service at Red P...
Red Pocket Mobile has new Apple iPhones on sale for $300 off MSRP when you switch and open up a new line of service. Red Pocket Mobile is a nationwide MVNO using all the major wireless carrier... Read more
Clearance 13-inch M1 MacBook Airs available a...
Apple has clearance 13″ M1 MacBook Airs, Certified Refurbished, available for $759 for 8-Core CPU/7-Core GPU/256GB models and $929 for 8-Core CPU/8-Core GPU/512GB models. Apple’s one-year warranty is... Read more
Updated Apple MacBook Price Trackers
Our Apple award-winning MacBook Price Trackers are continually updated with the latest information on prices, bundles, and availability for 16″ and 14″ MacBook Pros along with 13″ and 15″ MacBook... Read more
Every model of Apple’s 13-inch M3 MacBook Air...
Best Buy has Apple 13″ MacBook Airs with M3 CPUs in stock and on sale today for $100 off MSRP. Prices start at $999. Their prices are the lowest currently available for new 13″ M3 MacBook Airs among... Read more
Sunday Sale: Apple iPad Magic Keyboards for 1...
Walmart has Apple Magic Keyboards for 12.9″ iPad Pros, in Black, on sale for $150 off MSRP on their online store. Sale price for online orders only, in-store price may vary. Order online and choose... Read more

Jobs Board

Solutions Engineer - *Apple* - SHI (United...
**Job Summary** An Apple Solution Engineer's primary role is tosupport SHI customers in their efforts to select, deploy, and manage Apple operating systems and Read more
DMR Technician - *Apple* /iOS Systems - Haml...
…relevant point-of-need technology self-help aids are available as appropriate. ** Apple Systems Administration** **:** Develops solutions for supporting, deploying, Read more
Omnichannel Associate - *Apple* Blossom Mal...
Omnichannel Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple Read more
Operations Associate - *Apple* Blossom Mall...
Operations Associate - Apple Blossom Mall Location:Winchester, VA, United States (https://jobs.jcp.com/jobs/location/191170/winchester-va-united-states) - Apple 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.