2005-07-30

Book: "Hacking Mac OS X Tiger"

This is a weird book. If you look at the publisher's web site, or the synopses provided by on-line booksellers, you're likely to be confused. I heard about the book in a post by Jon Rentzsch where he mentioned that one of the chapters is about mach_override and mach_inject. A book with that kind of content sounded like an interesting book, but when I looked on the web, the details I found suggested that the book was actually about using System Preferences and iTunes playlists and other "Mac OS for Grandparents" stuff.

At this point, you might be expecting me to tell you which kind of book this actually is. Hard-core programming along the lines of injecting code into other running processes, or obvious stuff about using the Finder's "Show View Options" dialog?

The funny thing is, it's both.

The blurb describes Knaster as a programmer, but none of the programming chapters are by him, and Google gives the impression that his Mac programming days were back in the Mac dark ages, when Mac OS was a crufty home-computer OS and Mac programming was all about Pascal and 68000 assembler. More recently he seems to have written books on using applications such as iTunes. (I'm not saying he can't program. I'm saying I don't know of anything he's written.) So maybe this explains the book's schizophrenia — he wrote the chapters on applications, and his friends wrote the chapters on programming.

It doesn't explain who thought this mix would make a good book, though I have to admit that it almost works.

This book is divided into three sections: "Tips", "Mods", and "Hacks". All the way through, it helps to remind yourself that these people were Mac users when that was something to be ashamed of, and the rest of us were using the commercial Unixes of the time.

Tips
The tips section is really just about playing with the configuration of the Finder, Dashboard, Dock, et cetera. Some of the stuff is trivial: I wasn't joking when I mentioned using the Finder's "Show View Options" dialog. Other stuff is a little less obvious; stuff that's in the UI but that I'd never noticed. And some stuff is more hidden; the kind of thing you see on macosxhints.com that involves using defaults(1) to modify hidden preferences.

In places, this part of the book reads rather like Daring Fireball. Full of things I didn't know about the "option" key, for example. I wonder if the author spent a week option-clicking and option-dragging everything he could find, in an effort to come up with a catalog of uses of "option".

One of the most distressing parts of the book is where it explains how to enable root logins and – worse still – about logging in as root at the login dialog, rather than using su(1). There are two habits I've had to shake as an old-time Unix user. One is that I should use ssh(1) instead of telnet(1), which is easily fixed by not offering a telnet service. The other is using sudo(1) instead of su(1). Logging in as root from the login dialog is worse still because everything else you start will automatically be running as root. Not being able to log in as root is a great way to kick the su(1) habit, and it seems irresponsible to tell people how to have unprotected sex without explaining that there are very good reasons why they might want not want to rush out and try it. They might decide they don't care, or that they think the advantages outweigh the disadvantages, but the book doesn't encourage an educated decision.

Chapter 7, "Nifty Utilities", talks about two shareware utilities and three free utilities. A weird thing about the Mac world is that shareware is still alive and kicking. It seems a bit off to be advertising it in a book, though. And the specific applications chosen are strange until you remember that Knaster is from the Mac dark ages. He's not a Unix person, so the idea of having crappy GUI utilities to mv(1) or chmod(1) large numbers of files perhaps strikes him as useful instead of laughable.

Maybe he's sufficiently scared of the shell that he really thinks checkboxes labeled "R", "W", and "X" under headings such as "Group" are more convenient than chmod u+rw g+r o+r '/Users/scott/Pictures/Picture 1.pdf'? (Or maybe he's only ever seen old timers who insist on using octal, even in 2005?)

Chapter 8, "Running Unix Applications" was a big disappointment. It mentions Fink, DarwinPorts, and GNU-Darwin, but doesn't talk about their similarities and differences, and why and how you'd choose one over the other. He devotes more space to figure 8-3 "X11 looks like this when you start it up" showing an empty desktop with a single empty xterm(1). There follow two more half-page figures showing the same desktop but with xclock(1) and xlogo(1). The chapter picks up a little by ending with mention of X11 forwarding with ssh(1), but it still feels like a squandered opportunity.

Chapter 9, "Terminal" is a joke. One third is about bash(1) (without giving any reference to somewhere you could learn more than using the history and the existence of tab-completion), and one third is about the various games that emacs(1) offers. It doesn't even mention "Secure Keyboard Entry", or the existence of "Find", which is probably obvious to dark-age Mac users (why wouldn't an application have find functionality?) but non-obvious to Unix users. The rewrapping when you resize probably falls into this category, too, though a user's at least likely to discover that by accident.

Chapter 10, "Shell Commands" is a bit better, though he undersells Activity Monitor. It's nice to see him compare and contrast top(1), but Activity Monitor is much more functional. If this were my book, chapter 1 would have been "How Do I Get A Terminal Window?" and "Why Should I Always Have Activity Monitor Running?".

Mods
This section is about messing with existing applications in superficial ways. There's a tour of the contents of an application bundle (a Mac OS application is a directory structure rather than a single file), and an introduction to property lists and defaults. There's a chapter on Automator, Apple's latest investment in the Concorde fallacy known as AppleScript.

The last chapter, despite being called "Xcode and Other Tools", barely mentions Xcode's name before moving on to showing simple nib modification with Interface Builder, and giving an example of using Quartz Composer.

Hacks
The first hack is about putting badges on Dock icons, like Mail does for its unread count. Note that it's about putting a badge on your own application, not an arbitrary other process. (Which would have been cooler.) This is already fully covered on the web and in Joe Zobkiw's "Mac OS X Advanced Development Techniques".

Next come "Word of the Day" and "Best-Selling iTMS Songs" Dashboard widgets. Dashboard. Yawn.

Chapter 18 "Video Desktop" was something I was initially interested in, until I saw it was using the antiquated (and deprecated) QuickDraw API from the Mac dark ages. As the chapter itself admits at the end: "Try to find a way to use OpenGL instead of QuickDraw to do your processing. To accomplish this, you probably have to replace or reengineer everything in the MyQuickDrawView code".

Chapter 19 is about services, but they're much better covered in Anguish et al's "Cocoa Programming", which you must have if you're interested in Mac programming. (It's the best by far.)

Chapter 20 shows how to write an XML importer for Spotlight. It's perhaps most interesting for its coverage of the Spotlight command-line utilities. I didn't spot anything in this chapter that wasn't in the recent macdevcenter.com article. I'm still at a loss to think of a file format that needs an importer that doesn't already have one, and until Apple stop Spotlight from spinning up my iPod (and freezing the UI for those few seconds), I'm unlikely to start making any non-accidental use of it anyway.

Chapter 21 explains how to write the generalization of those crappy GUI wrappers for command-line tools that were mentioned in an earlier chapter. Despite the pointless application (I mean, who needs a window to choose ls(1) options, really?), there's a hidden gem in this chapter. Maybe I haven't been paying attention, but I think this is the first time I've seen an example of programmatically-generated UI in a Cocoa program. An odd omission, when you think that almost all real programs need to do this kind of thing. The example uses absolute layout, but that's better than a kick in the plums.

Some of the code is in Objective-C++, but it's hardly setting a good example. Memory leaks and fixed-length buffers and getline and, well, it may as well have been C but for the use of std::string.

The chapter also shows the magic for starting a GUI application from a Unix-style executable rather than an application bundle. If you wanted to write a Mac OS-only version of zenity(1), this chapter has most of what you need to get started.

And finally, the chapter on mach_override and mach_inject. Sadly, there's no more information in the book than on the web. It's a pity, because I was hoping to see a practical example for once, rather than the usual DisposeWindow+Beep example.

Summary
I found this book mildly interesting. If you're a developer and own a Mac, you probably want to take a look. It's not a good introduction to Mac OS for developers, and it's not a replacement for Anguish et al's "Cocoa Programming", either, but it's interesting to flick through.

The "tips" section doesn't talk at all about how you'd find any of the hidden preferences yourself. A true "hacks" book, I think, should talk about basic Mac reverse engineering skills. They're not hard, but they aren't all the same as traditional Unix skills.

It also doesn't show a Unix developer how to get comfortable with Mac OS. I can still remember the first time I booted Mac OS X thinking "where do I click to get a shell window?" and worrying that Mac OS was going to suck if it was going to make that most fundamental of tasks difficult. More of this kind of thing would have made the book much more interesting.

As with "Swing Hacks", I think the use of "hack" in this book's title was a mistake. In what way is writing a Dashboard widget a "hack"? How about using the Finder's "Show View Options" dialog? This is a sense of "hack" that's completely new to me.