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

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 »
MoreFun Studios has announced Season 4,...
Tension has escalated in the ever-volatile world of Arena Breakout, as your old pal Randall Fisher and bosses Fred and Perrero continue to lob insults and explosives at each other, bringing us to a new phase of warfare. Season 4, Into The Fog of... | Read more »
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 below... | Read more »
Marvel Future Fight celebrates nine year...
Announced alongside an advertising image I can only assume was aimed squarely at myself with the prominent Deadpool and Odin featured on it, Netmarble has revealed their celebrations for the 9th anniversary of Marvel Future Fight. The Countdown... | Read more »
HoYoFair 2024 prepares to showcase over...
To say Genshin Impact took the world by storm when it was released would be an understatement. However, I think the most surprising part of the launch was just how much further it went than gaming. There have been concerts, art shows, massive... | Read more »
Explore some of BBCs' most iconic s...
Despite your personal opinion on the BBC at a managerial level, it is undeniable that it has overseen some fantastic British shows in the past, and now thanks to a partnership with Roblox, players will be able to interact with some of these... | Read more »

Price Scanner via MacPrices.net

You can save $300-$480 on a 14-inch M3 Pro/Ma...
Apple has 14″ M3 Pro and M3 Max MacBook Pros in stock today and available, Certified Refurbished, starting at $1699 and ranging up to $480 off MSRP. Each model features a new outer case, shipping is... Read more
24-inch M1 iMacs available at Apple starting...
Apple has clearance M1 iMacs available in their Certified Refurbished store starting at $1049 and ranging up to $300 off original MSRP. Each iMac is in like-new condition and comes with Apple’s... Read more
Walmart continues to offer $699 13-inch M1 Ma...
Walmart continues to offer new Apple 13″ M1 MacBook Airs (8GB RAM, 256GB SSD) online for $699, $300 off original MSRP, in Space Gray, Silver, and Gold colors. These are new MacBook for sale by... Read more
B&H has 13-inch M2 MacBook Airs with 16GB...
B&H Photo has 13″ MacBook Airs with M2 CPUs, 16GB of memory, and 256GB of storage in stock and on sale for $1099, $100 off Apple’s MSRP for this configuration. Free 1-2 day delivery is available... Read more
14-inch M3 MacBook Pro with 16GB of RAM avail...
Apple has the 14″ M3 MacBook Pro with 16GB of RAM and 1TB of storage, Certified Refurbished, available for $300 off MSRP. Each MacBook Pro features a new outer case, shipping is free, and an Apple 1-... Read more
Apple M2 Mac minis on sale for up to $150 off...
Amazon has Apple’s M2-powered Mac minis in stock and on sale for $100-$150 off MSRP, each including free delivery: – Mac mini M2/256GB SSD: $499, save $100 – Mac mini M2/512GB SSD: $699, save $100 –... Read more
Amazon is offering a $200 discount on 14-inch...
Amazon has 14-inch M3 MacBook Pros in stock and on sale for $200 off MSRP. Shipping is free. Note that Amazon’s stock tends to come and go: – 14″ M3 MacBook Pro (8GB RAM/512GB SSD): $1399.99, $200... Read more
Sunday Sale: 13-inch M3 MacBook Air for $999,...
Several Apple retailers have the new 13″ MacBook Air with an M3 CPU in stock and on sale today for only $999 in Midnight. These are the lowest prices currently available for new 13″ M3 MacBook Airs... Read more
Multiple Apple retailers are offering 13-inch...
Several Apple retailers have 13″ MacBook Airs with M2 CPUs in stock and on sale this weekend starting at only $849 in Space Gray, Silver, Starlight, and Midnight colors. These are the lowest prices... Read more
Roundup of Verizon’s April Apple iPhone Promo...
Verizon is offering a number of iPhone deals for the month of April. Switch, and open a new of service, and you can qualify for a free iPhone 15 or heavy monthly discounts on other models: – 128GB... Read more

Jobs Board

IN6728 Optometrist- *Apple* Valley, CA- Tar...
Date: Apr 9, 2024 Brand: Target Optical Location: Apple Valley, CA, US, 92308 **Requisition ID:** 824398 At Target Optical, we help people see and look great - and Read more
Medical Assistant - Orthopedics *Apple* Hil...
Medical Assistant - Orthopedics Apple Hill York Location: WellSpan Medical Group, York, PA Schedule: Full Time Sign-On Bonus Eligible Remote/Hybrid Regular Apply Now Read more
*Apple* Systems Administrator - JAMF - Activ...
…**Public Trust/Other Required:** None **Job Family:** Systems Administration **Skills:** Apple Platforms,Computer Servers,Jamf Pro **Experience:** 3 + years of Read more
Liquor Stock Clerk - S. *Apple* St. - Idaho...
Liquor Stock Clerk - S. Apple St. Boise Posting Begin Date: 2023/10/10 Posting End Date: 2024/10/14 Category: Retail Sub Category: Customer Service Work Type: Part Read more
Top Secret *Apple* System Admin - Insight G...
Job Description Day to Day: * Configure and maintain the client's Apple Device Management (ADM) solution. The current solution is JAMF supporting 250-500 end points, Read more
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.