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

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 »
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 »

Price Scanner via MacPrices.net

Amazon is offering a $100 discount on every M...
Amazon is offering a $100 instant discount on each configuration of Apple’s new 13″ M3 MacBook Air, in Midnight, this weekend. These are the lowest prices currently available for new 13″ M3 MacBook... Read more
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

Jobs Board

Relationship Banker - *Apple* Valley Financ...
Relationship Banker - Apple Valley Financial Center APPLE VALLEY, Minnesota **Job Description:** At Bank of America, we are guided by a common purpose to help Read more
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
All contents are Copyright 1984-2011 by Xplain Corporation. All rights reserved. Theme designed by Icreon.