Inside Application Bundles
Reprinted from Hacking Mac OS X Tiger, copyright 2005, Wiley Publishing, Inc.
Mac OS X uses some slick technology to present its beautiful Aqua face to the world while keeping everything running smoothly behind the scenes. In previous chapters, you've seen a few glimpses of how OS X handles applications and their related bits. In this chapter, you go a lot deeper as you examine what really makes up an OS X application.
What You See Is Not Always What You Get
When you look at the contents of a disk in the Finder, almost every item you see corresponds to a single file. But sometimes, the Finder is lying to you (even though it's for a good cause). Mac OS X provides a way to collect related files into a single entity called a bundle, and OS X conspires with the Finder to present bundles as one icon. Applications are the most common examples of bundles. That's right-all those applications on your Mac that look like single files in the Finder. They're really living secret lives as bundles, hiding all sorts of interesting stuff inside.
Applications are the most prominent kinds of bundles, but they're far from the only bundles on your Mac. Bundles provide a handy general mechanism for keeping a bunch of stuff behind a single icon. Mac OS X uses bundles in various situations that require code to be kept together with images, property lists, and other flotsam. Screen savers, preference panes, Dashboard widgets, frameworks, and application plug-ins are all bundles, each one with some files that have code and others containing data.
A bundle is basically a folder that appears in the Finder as a single file. This convenient fiction allows users to easily install and move applications without having to worry about keeping their support files around, and it also prevents casual users from messing up their applications by trashing a necessary file.
Although Mac OS X uses bundles primarily as a way to hide collections of code plus other resources, no rule says that bundles must include pieces of code. A database developer might choose to store databases and indices together in a bundle, for example.
The Mac OS X Installer uses a bundle technology called packages to gather all the files needed for installing software into a unit, an installation package. The Installer then extracts the files it needs from the package and puts them where they belong.
Looking at Bundles
Bundles are folders in disguise, but it's easy for anyone to look behind the mask and see what a bundle contains. To see inside a bundle, you don't have to go any farther than the Finder.
1. In the Finder, choose a bundle to inspect. You can pick any application, screen saver, preference pane, Dashboard widget, or other bundle you like. For this example, use the Spotlight preference pane. You can find it in /System/Library/PreferencePanes/Spotlight.prefPane.
2. Pull down the Action menu in the Finder window (the one with a picture of a gear) and choose Show Package Contents. This opens a new window with a Contents folder, as shown in Figure 11-1.
3. Look inside the Contents folder to see all the other stuff hidden by the bundle (Figure 11-2).
Apple is a little schizophrenic about the terms bundle and package. Although Apple's developer documentation defines a bundle as the fundamental mechanism behind applications, screen savers, and the like, note that the Finder's menu item uses the term package instead of bundle.
Mac OS X and the Finder collaborate on the bundle illusion to show you a single icon instead of the full bundle contents. But the command line isn't fooled. If you use Terminal, bundles appear like the folders they truly are.
To see how this works, go to a Terminal window and type the following:
That command results in this output:
Those are exactly the same contents you saw in the Finder using the Show Package Contents command. So, you can examine what's inside a bundle using either the Finder or Terminal, whichever you prefer.
A Tour of Bundle Contents
When you open an application bundle, or most any other kind of bundle, you see a Contents folder at the first level inside. Let's take a look at what's inside the Contents folder.
In this example, you investigate the TextEdit application. You start by seeing (almost) everything there is to see.
1. In the Finder, go to the Applications folder and select TextEdit.
2. Choose Show Package Contents from the Action menu.
3. Click the List View button in the Finder window.
4. Click the triangle next to the Contents folder to see what's inside. The result should look like Figure 11-3. The Contents folder has five items: Info.plist, MacOS folder, PkgInfo, Resources folder, and version.plist. I talk about these in more detail later.
5. Click the triangle next to MacOS to see what it contains. There should be a single object, a file named TextEdit. This is a file containing the executable code for TextEdit. When you double-click the TextEdit icon to launch it, Mac OS X runs the code in this file.
6. Click the triangle to open the Resources folder. There's a lot of stuff in there! You see a bunch of folders with the names of various languages (English.lproj, Dutch.lproj, and so on), some files with recognizable file types (Edit.icns contains icons and DocumentWindow.nib comes from the Interface Builder development tool), and a few others (Figure 11-4).
Now that you have an idea of all the files that are hanging around inside application bundles, I discuss what all that stuff is for.