TweetFollow Us on Twitter

MacTech Magazine Article Archives

Volume Number: 19 (2003)
Issue Number: 6
Column Tag: Mac OS X Programming Secrets

Mac OS X Programming Secrets

Mac OS X: Wading In Deeper

by Scott Knaster

This month we'll continue our peeking and poking around the edges of Mac OS X as we prepare for deeper voyages into OS X programming. In our last thrilling installment, we were messing around with Terminal and finding out about how it could enhance and enrich the lives of even those who spend most of their time above the GUI line. After cranking up Terminal, we discussed a few shell commands and introduced some useful tips for being more productive. We'll continue along those lines this time, exploring some more features of Terminal and its shell windows, then looking at a couple of lesser-known but useful shell commands.

Terminal Condition

Apple has done a good job of hiding most of the scars where Mac OS and Unix have been Frankensteined together, but there are still some design choices in which, shall we say, Mac user interface standards have not yet won the day. As you use Terminal, you probably noticed that things are just a little wacky here and there - Terminal hasn't quite completed the transition to becoming a full Mac application. For example, take a look in the Edit menu: there's no Undo command. What a giveaway! Let's take a look at some of the other cool tricks and odd quirks of Terminal.

Every shell window in Terminal includes a little picture of a torn square just above the scroll bar. See it? Hey, that's right where the split window control is many Mac apps. Could it be the same here? Yes, and no. It's a sort of mutant splitter. If you click on it, your window will indeed split into two panes (see Figure 1). However, only the top pane gets a scroll bar. The idea is that the bottom pane always shows the latest interaction between you and the shell, while you can scroll back in the top pane to any part of the shell session. You can even select text in the top pane, then drag and drop it into the bottom, where it shows up at the insertion point. To put the window back together in one piece, click the now-whole square that appears at the top of the lower pane.


Figure 1. Split Terminal window with one scroll bar and unsplit command.

Although window splitting is not done in the familiar Mac way in Terminal, there is a standard technique for fooling with colors. Last month, you might recall that we discussed changing the colors for the text and background in your shell windows by using the Color window. There are a couple of additional features worth mentioning for changing colors in shell windows. Take a look at the Color window in Figure 2. The Color window is a standard OS X component that you see in lots of apps, and you can even use it in your own creations, so everything we discuss here is also democratically available to you. Take a look at the tiny magnifying glass at the left side, near the top. You might not guess it, but that's a button. You can use it to pick a color you like from anywhere on the screen and use it in your shell windows.


Figure 2. Color window from Terminal

When you click the magnifying glass, the cursor changes to a bigger magnifier, and when you click elsewhere, the color under the magnifier's crosshair is sucked into the horizontal box next to the magnifier control in the Color window. Once the color is in that box, you can drag out of the box and drop the color into your shell windows to change the appearance of various things, depending on where you drop it. You can set the color of text, the background, bold text, or even the cursor rectangle. Try to be careful not to set the test and background to the same color: green on green (for example) text is hard to read. If that happens to you, just get another color into the box and drag it into the window. Either the text or the background will change color, and your text will be happily visible again.

Take a look at the bottom of the color window. You'll see a row of small white boxes. These boxes are sort of a scratch palette for your favorite colors. Each box can hold one color. To try it out, drag out of the long color box at the top of the window, and drop into a little square at the bottom. The color you dragged fills up the palette square you chose. You can repeat this for the other squares in the palette, putting other colors in the color box at the top and then storing them in the palette for safekeeping. And, the palette has a secret: it's actually much bigger than what you see in the color window. If you stretch the window to its maximum size, then drag the grabber at the bottom-center of the window down, you'll see that the palette can accommodate up to 300 colors. That should be enough for coloring shell windows, or even redecorating your house.

The last Color window feature we'll point out is the bar at the top that lets you select different methods of choosing your colors, including a color wheel, sliders that let you specify colors as RGB or other values, a set of palettes such as web safe colors, an image that contains colors you want to copy, or even a set of crayons. Figure 3 has a picture of the crayon picker, with some colors dropped into the palette at the bottom.


Figure 3. You can pick colors using crayons, and you can save your favorites in the palette at the bottom.

Entitlement Issues

Window title bars in Terminal are another departure from standard Mac behavior. While most apps have already decided what they want to put in the title bar (usually the name of the document you're looking at), Terminal goes to great geeky lengths to let you customize the text in title bars. Using the File --> Show Info menu item to display the Terminal Inspector window (see Figure 4), you can choose from several options for your shell window titles, including these:


Figure 4. Terminal Inspector window lets you decide what goes in each shell window's title bar, which is an awesome responsibility you should not take lightly.

"Active Process Name" puts the name of the command being executed into the title bar. So, for example, if run an editor like vi in a window, Terminal will put vi into the window title.

"Shell Command Name" displays the name of the shell, such as bash or tcsh, that's running in the window.

"Dimensions" tells the number of character rows and columns displayed by the window, which lets you continue the fantasy that you're computing in 1972.

When you have multiple Terminal windows open, you can select each one with a command key shortcut: command-1, command-2, and so on. Checking "Command key" shows the window's shortcut in its title bar.

Changing any of these settings affects the frontmost Terminal window immediately. In fact, if you resize the window and you have the dimensions displayed, you'll see the number change live as you drag. Pretty slick for some old terminal program.

If you decide you like the settings so much that you want all Terminal windows to appear that way, click the "Use Settings as Defaults" button at the bottom of the window.

At Your Command

Now that we know how to customize our Terminal windows to a fine degree, let's take a look at a couple of shell commands and other tools that are useful both for using and programming with OS X. We'll start with a command that helps you learn more about what's going on in the vast active culture inside your Mac. The top command examines all the things running on your OS X Mac and provides information about them. To try it out, go to a Terminal window and type top and return. Here's what happened when I did it:

Processes:   63 total, 2 running, 61 sleeping..376 threads        14:56:20

Load Avg:   0.77, 0.57, 0.40  CPU usage:  64.9% user, 14.4% sys, 20.7% idle

SharedLibs:   num =    7, resident = 2.03M code, 184K data, 520K LinkEdit

MemRegions:   num = 7135, resident =  167M + 8.39M private,  116M shared

PhysMem:   58.3M wired, 284M active, 115M inactive, 457M used, 54.9M free

VM:   3.76G + 3.62M   13990(0) pageins, 12653(0) pageouts

    PID   COMMAND      %CPU    TIME    #TH   #PRTS   #MREGS   RPRVT   RSHRD   RSIZE   VSIZE
   1206   top          8.1%   0:06.02    1      14     18      300K    248K    596K   13.6M
   1200   tcsh         0.0%   0:00.03    1       9     16      340K    492K    784K   5.73M
   1140   tcsh         0.0%   0:00.03    1      10     16      348K    492K    792K   5.73M
   1139   login        0.0%   0:00.28    1      12     33      248K    300K    572K   13.7M
   1059   SystemUISe   0.0%   0:01.41    1      86     142    1.18M   4.77M   3.77M   99.7M
   1058   Snapz Pro    0.9%   0:22.54    2     113     132    2.35M   8.23M   6.49M    107M
   903    tcsh         0.0%   0:00.10    1      10     15      372K    492K    828K   5.73M
   902    login        0.0%   0:00.36    1      12     33      236K    300K    568K   13.7M
   827    slpd         0.0%   0:01.68    8      35     31      180K    364K    568K   17.8M
   812    lookupd      0.0%   0:01.23    2      32     46      476K    384K    932K   14.9M
   772    Microsoft    0.0%   2:10.00   12     148     296    15.2M   23.6M   11.5M    154M
   768    Terminal    53.2%   1:48.60    5     102     437    4.55M-  14.6M   13.8M    116M   
   425    Microsoft    9.9%   18:43.86   5     100     448    33.1M   39.3M   47.1M    210M
   424    SecurityAg   0.0%   0:00.79    1      53     96     1.11M   4.11M   2.73M   99.5M
   423    Safari       0.0%   7:21.19    9     418     735    32.1M   18.1M   32.3M    232M
   422    iTunes       0.0%   22:55.44  12     185     282    11.7M   12.9M   20.6M    135M
   421    AppleSpell   0.0%   0:00.47    1      18     22      440K    848K   1.13M   15.0M
   417    iChatAgent   0.0%   0:04.10    5      71     67      844K   1.69M   2.37M   81.8M
   416    Fax Assist   0.0%   0:00.25    1      51     68      560K   2.75M   1.27M   95.2M
   415    MSN Messen   0.9%   4:02.51    5      81     108     876K   4.47M   2.94M   99.5M
   414    Fax Server   0.0%   0:00.28    1      53     71      600K   3.25M   1.68M   96.2M
   413    MicrosoftM   0.0%   0:02.21    1      43     33      216K    788K    708K   78.7M
   411    MSN Messen   1.8%   21:02.39   5     134     169    3.41M   11.6M   8.82M    121M
   410    iChat        0.0%   2:37.36    4     144     284    5.47M   11.8M   10.2M    125M   
   409    NetNewsWir   0.0%   12:05.59   2    1884     231    21.1M   12.2M   29.6M    133M
   408    OmniOutlin   0.0%   1:56.60    3      87     209    8.61M   8.56M   13.7M    121M
   406    Microsoft    0.9%   0:45.08    2      69     135    4.76M   6.38M   6.87M    104M
   404    Finder       0.0%   0:15.61    1      84     147    3.29M   13.1M   8.92M    129M
   402    Dock         0.0%   4:35.44    2     127     168    1.58M   11.6M   8.43M    154M
   396    pbs          0.0%   0:01.43    2      30     27      400K    856K   1.01M   14.4M
   391    httpd        0.0%   0:00.00    1       8     74        4K    796K     80K   15.6M
   390    httpd        0.0%   0:02.00    1      19     72       32K    796K    664K   15.1M   
   389    AppleFileS   0.0%   0:04.69    2      31     24      592K    852K    824K   18.4M
   364    cron         0.0%   0:00.09    1       8     15       40K    232K    108K   13.5M
   347    DirectoryS   0.0%   0:01.58    3      57     152     548K   1.40M   1.54M   21.4M
   343    cupsd        0.0%   0:07.58    1       8     25     1.36M    604K   1.63M   16.3M
   334    xinetd       0.0%   0:00.02    1      10     15       36K    212K     76K   1.45M
   332    inetd        0.0%   0:00.00    1       8     13       24K    208K     64K   1.28M
   325    ntpd         0.0%   0:01.64    1       8     15       96K    288K    244K   1.52M
   323    loginwindo   0.0%   0:04.25    4     159     150    2.25M   5.18M   4.00M   97.0M
   314    smbd         0.0%   0:00.01    1       8     21      328K    212K     76K   15.5M
   312    automount    0.0%   0:00.05    2      22     21      128K    268K    392K   14.7M
   311    sshd         0.0%   0:00.13    1       8     16       40K    212K     80K   1.83M
   309    coreservic   0.0%   0:02.01    3     104     92     1.06M   6.82M   6.77M   36.6M
   294    nfsiod       0.0%   0:00.00    1       8     12        0K    220K     52K   1.27M
   293    nfsiod       0.0%   0:00.00    1       8     12        0K    220K     52K   1.27M
   292    nfsiod       0.0%   0:00.00    1       8     12        0K    220K     52K   1.27M
   291    nfsiod       0.0%   0:00.00    1       8     12        0K    220K     48K   1.27M
   261    netinfod     0.0%   0:01.22    1       7     30      124K    296K    284K   1.66M
   243    mDNSRespon   0.0%   0:00.57    2      40     29      904K    260K    676K   14.4M
   240    crashrepor   0.0%   0:00.00    1      14     14       32K    208K     72K   13.3M
   229    syslogd      0.0%   0:00.30    1       7     13       64K    220K    140K   1.28M
   189    autodiskmo   0.0%   0:02.29  191      59     410    1.13M    624K   1.31M    113M
   185    Window Man  11.8%   29:55.34   2     323     870    13.2M+  59.7M   59.7M+   249M+
   181    ATSServer    0.0%   5:45.23    2      47     230     824K   6.03M   2.63M   47.0M
   164    SecuritySe   0.0%   0:03.61    1      58     23      220K    792K    732K   14.9M
   111    configd      0.0%   0:52.11    3     132     150     548K    900K   1.02M   16.3M
   77     dynamic_pa   0.0%   0:00.00    1      10     15       48K    216K     92K   1.29M
   73     update       0.0%   0:04.89    1       7     13       32K    212K     84K   1.26M
   51     kextd        0.0%   0:01.95    2      18     22      280K    524K    340K   15.3M
   2      mach_init    0.0%   0:00.53    2     203     15       88K    224K    176K   1.80M
   1      init         0.0%   0:00.02    1       9     12       52K    216K    232K   1.28M
   0      kernel_tas   0.0%   6:24.29   28       0     -          -       -   49.2M    475M
   0      idle_threa  26.3%   11:53:50

Gosh, that's a lot of stuff. I sure hope there's nothing embarrassing in there.

top gives you an ever-updating list of all the processes running in your Mac, with lots of juicy information about each of them. If you keep an eye on the top display, you'll notice that it keeps changing, producing new numbers every second. If you launch any other apps, or even if you open a new shell window or do anything else that starts a process, you'll see a new entry in the list. Similarly, processes drop off the list when they're stopped, as when you quit an application or close a Terminal window.

Processes are listed in reverse of their launch order, newest first. The numbers in the first column are the process IDs, followed by the name of the process or application. Obviously, there's an enormous amount of information here. We'll go over some of the most interesting items here, and refer you to the man entry for full details.

If you're playing along at home, and your don't seem to have as many processes listed on your computer, you might not be getting the full picture. To make sure you see as much of the top display as possible, stretch your Terminal window out to its maximum size, and then use the Font menu to make the window's font small enough that you see all the processes. You know you're seeing it all if you can see process 0, idle_thread, listed at the bottom.

Coming Out On Top

Now let's make some sense of this display, starting with the header that appears before the list of processes. The first line of output tells you the number of processes and threads, while the value at the end of the line is simply the time of day, so you can use top as a super-complicated clock if you like. Another interesting value in the header is the CPU usage in the second line. In this example, the CPU is 20.7% idle, which is actually quite busy - usually, you'll find the idle value much higher than that. For fun, you can quickly whip the mouse pointer around the screen, and you'll see the idle value go down as the CPU sweats to keep track of it.

The meaty part of the top display is the big list of processes. As we mentioned earlier, the first two columns are the process ID and the "command" name, which usually includes an entry for every running application. We can also see that several shell commands have their own listings, even though they aren't applications. These include several tcsh shells and top itself. The first value after the command name is the percentage of CPU time being hogged by that process. For example, you can see that Terminal was grabbing a whopping 53.2% of the CPU's time when this list was printed. Even for the active application, that's quite a large fraction. You can use top to help determine if any particular app, including one you're programming, is taking up more than its share of CPU time.

What else is interesting in this list? Note that the next item under Terminal is using 9.9% of the CPU time. The name of that process is...Microsoft? Microsoft what? Word, Excel, MSN? Unfortunately, there is no way to tell. A regrettable artifact of Mac OS X's Unixness is that there isn't enough room for process names that are as long as some Mac application names. One way to figure out which Microsoft application is which is to bring one to the front, then start typing. One of the apps will suddenly start taking a lot more CPU time: that's the active one. On the other hand, if you see any application that takes a lot of CPU time even when it's not frontmost, you might be able to improve your Mac's performance by replacing that app.

top includes a couple of command line options that you might find useful. The -u option keeps the list of processes constantly sorted according to usage, rather than launch order. This makes you a little crazy but is kind of fun to watch and ensures you know which tasks are using the most CPU time. You might also like to try the -s option, which takes a value after the s to indicate how many seconds should pass before updating the display.

That's A Man, Baby

To become more adept at Unix commands, I'll repeat that you should make frequent use of the man command. This command gives you pages from the Unix online "manual", which was a really clever idea back in 1906 when Unix was invented and is now sometimes barely adequate to help you figure out what's going on. The man documentation wasn't written by the literate folks at Apple, but you'll need it sometimes to get you through the night.

Welcome To The Machine

We'll finish by talking about one more little shell command of interest: machine. This command works under Mac OS X to tell you which CPU your computer is using: if the Mac has a G3, you get ppc750, and a G4 returns ppc7450. These are Motorola's part numbers for the chips, useful if you ever find yourself wanting to order CPU manuals from Motorola.

Finally, be sure you check this command's man page. If you do, you'll find the less-than-enlightening guidance that "The machine command is currently under development." But the real fun is that you get to type man machine.


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.