Read an Excerpt
Beginning Mac OS X Programming
By Michael Trent
John Wiley & SonsISBN: 0-7645-7399-3
Chapter OneThe Mac OS X Environment
Welcome to the wonderful world of Mac OS X, the next-generation operating system from Apple Computer!
The Mac OS X operating system powers modern Macintosh computers. After many long years and a few scrapped attempts to modernize the older Mac OS operating system, Apple released Mac OS X in April of 2001. Since then, Apple has released a steady stream of upgrades and system updates. This book was written around Mac OS X version 10.4, "Tiger," the latest version.
In order to write software for Mac OS X, you need to know your way around the system. By now you may already be familiar with Mac OS X's applications and user interface style. Those things all rest on top of a number of subsystems and services that make up the Mac OS X operating system.
In this chapter you learn
How the Mac OS X operating system is structured, including what the major areas of the system are and how they work together
How to use Mac OS X's command-line interface How applications take advantage of the operating system services on Mac OS X
How Apple encourages a common look and feel for Mac OS X applications
Introducing the Mac OS X
What comes to mind when you think of Mac OS X? Is it the applications you use? Perhaps you recall Mac OS X's distinctive user interface? Or maybe you think of Mac OS X's stability? In truth, Mac OS Xembodies all these things.
The Mac OS X operating system is often described as a collection of layers, as seen in Figure 1-1. You are probably already familiar with the topmost layer: the applications that run on Mac OS X (like Mail, iTunes, Safari, and so on). These applications are all written against a collection of application frameworks. These frameworks are special libraries that provide the code and all the other resources (icons, translated strings, and so on) to perform common tasks. For example, the Cocoa framework contains a number of resources necessary to make a Cocoa application.
All Mac OS X applications use graphics to some extent, ranging from simply presenting its user interface to processing graphical data like QuickTime movies. The system provides several specialized libraries for working with graphics and graphics files.
These layers rest on the broad shoulders of the core operating system, which at the lowest level is responsible for making your Macintosh run. For example, the core OS handles reading from and writing to your hard drive and random access memory (RAM), it manages your network connections, it powers down the computer when it falls to "sleep," and so on. In fact, any program that talks to your hardware in any way ultimately goes through the core OS.
Throughout this book you examine Mac OS X in detail through Slide Master, an application that builds and displays photo slide shows. You will build Slide Master bit-by-bit as you learn more about how the elements of Mac OS X come together. The Slide Master application and its source code can be downloaded from Wiley's website; so you can check your work against our complete solution as you go.
This is a good time to take a quick tour of Slide Master. You can download Slide Master from Wiley's website, make a slide show, and view your handiwork. In doing so, you touch on all the major areas of the Mac OS X operating system.
Try It Out
1. Download the files for this chapter from wrox.com. Refer to the Introduction for instructions on finding the files you need from the Wrox website. You can search for the book by its ISBN number: 978-0-7645-7399-6. You are looking for a file named Chapter01.zip.
2. Uncompress the Chapter01.zip archive using your favorite decompression tool. (Mac OS X 10.3 and later supports uncompressing .zip files directly in the Finder.) Inside you will find the Slide Master application, a folder of pictures called Images, and a folder of source code.
3. Run the Slide Master application by double-clicking it in Finder. The application opens an untitled document window.
4. Add the pictures in the Images folder to Slide Master by choosing Slide Show [right arrow] Add Slide. You can select all the files at once from the open panel. The images appear in a drawer to the side of the document window and the main window displays the selected image, as shown in Figure 1-2. You can use the arrow keys to change the selection.
5. Export a slide show as a QuickTime movie by choosing File.Export. Slide Master writes out a QuickTime movie and opens it with QuickTime Player.
6. Save your document by choosing File.Save.
How It Works
Slide Master is a document-based application, which means that it provides a user interface for individual documents. In this case, documents are collections of slides that you can sift through and export as QuickTime movies. Slide Master documents can be opened, saved, and closed using the File menu. Other document-based applications also support printing, although Slide Master does not.
Much of the functionality you see here comes from Slide Master's application framework: Cocoa. The Cocoa application frameworks provide the implementation for the things you see on the screen: windows, pictures, menus, buttons, and so on. Cocoa also provides support for managing the document: reading and writing document files, closing the document when its window is closed, and routing menu commands to the selected document. Finally, Cocoa provides tools for storing application data, including working with user preferences and storing lists of items in memory.
Of course Slide Master uses QuickTime to generate movie files. You are probably already familiar with QuickTime, both through QuickTime Player and through web browsers that support the display of QuickTime movies. But QuickTime also makes most, if not all, of its functionality available to applications through its framework interface.
When you save a Slide Master document, the document file contains a list of image files that are part of your slide show, not the actual images themselves. As a result, these documents can be relatively small. Behind the scenes, Slide Master uses aliases to track these image files so that they can be found if the files are moved around on your disk. These aliases are the same aliases you can create in the Finder, although they are embedded in your document rather than saved separately to disk. Slide Master uses the Carbon application framework for working with aliases. Even though Slide Master is a Cocoa application, it can still access many of the services available in Carbon.
You learn more about Cocoa, Carbon, QuickTime, and other technologies later in this chapter, and as you proceed through this book.
The Core Operating System
The heart of Mac OS X is based on the Unix operating system. Unix was developed by AT&T in the early 1970s. In those days, computers were large and expensive, and Unix was intended as a way to share computing resources between multiple users at once. It was likely that an organization at that time could afford only one computer for all its members, and Unix provided a way for people to use that computer simultaneously without getting in each other's way.
Over the years, Unix development has split off into many distinct "flavors" of Unix, all headed up by different groups of people, all with somewhat different goals. BSD and Linux are two such examples. Each version of Unix shares some portion of the original vision and typically implements a common set of libraries and commands.
Unix is regarded as a robust operating system whose scalability and innate networking capability make it ideal for use as a server. In fact, most of the modern day Internet is powered by Unix servers of one version or another. It turns out that these features are also desirable in modern desktop operating systems. So it is no surprise when seeking to modernize the original Macintosh operating system, Apple turned to Unix.
Mac OS X's core operating system is a Unix flavor called Darwin. Like most Unix flavors, Darwin's source code is freely available, allowing interested parties to see exactly how the core operating system works. Apple maintains several resources for programmers interested in Darwin, including a way for people-at-large to contribute changes and bug fixes back to Apple.
Although Mac OS X tries to hide Darwin from the average user, there are some places where the Unix command-line pokes through. The most obvious example is the Terminal application, found in /Application/Utilities. You can use Terminal to work directly with Darwin's command-line tools. A more subtle example includes the way you describe file locations on Mac OS X: by using a file path. A file path is a string of text that describes a file's location.
The original Mac OS operating system abhorred file paths and tried its best to avoid them; but even so it devised a convention for describing a path to a file. Mac OS file paths are composed of a disk volume name followed by several folder names and possibly a file, all separated by colons, as in Macintosh HD:Applications:Utilities:Terminal.app.
Although there are places where this old convention still exists, Mac OS X mostly uses Unix's method of describing file paths: a series of directories from the root directory all separated by slashes, as in /Applications/Utilities/Terminal.app. The root directory contains all the files and directories on a Mac OS X system and is referred to simply as /. The path /Applications refers to a file or directory named Applications in the root directory. A path that begins with the root slash is called an absolute (or full) path because it describes a precise file location. If the root slash is not included, the path is called a relative path because it is relative to your current location.
If you look in /Applications/Utilities in the Finder, you might notice that there is no Terminal.app; instead there's just a program called Terminal. By default, Finder and other applications hide file extensions such as .app and .txt from you. So the application at /Applications/ Utilities/Terminal.app appears simply as Terminal. The Core OS makes no attempt to hide extensions from you; if you browse the file system using Mac OS X's command-line interface, you can see all these extensions. You learn more about Mac OS X's command-line interface later in this chapter.
Darwin is composed of several parts, including a kernel, a system library, and numerous commands, as illustrated in Figure 1-3.
The heart of a Unix operating system is its kernel. The kernel is the program that loads when the computer is first turned on and is responsible for managing all the hardware resources available to the computer. The kernel is also responsible for running the other programs on the system, scheduling process execution so that they can share the central processing unit (CPU) and other resources, and preventing one process from seeing what another process is doing. These last two responsibilities are more commonly known as pre-emptive multi-tasking and protected memory, respectively.
Because Unix prevents programs from accessing the computer hardware or other programs directly, it protects against the most common forms of system crashes. If a process misbehaves in one way or another, the system simply terminates the process and continues on its way. In other words, the misbehaving process crashes. In some operating systems, a misbehaving process can stomp all over other applications, or even break the operating system itself, before the system is able to terminate the process. As a result, poorly written programs can cause the entire computer to freeze or crash. Not so on Unix; because a process cannot modify other processes, including the kernel, there is virtually no risk of a bad process bringing down the entire operating system.
Although the kernel is responsible for accessing hardware, much of the knowledge of specific hardware details is delegated to device drivers. Device drivers are small programs that are loaded directly into the kernel. Whereas the kernel might know how to talk to hard disks, for example, in general, a specific device driver knows how to talk to specific makes and models of hard disks. This provides a way for third parties to add support for new devices without having to build it into Apple's kernel. Mac OS X includes default drivers for talking to a wide variety of devices, so much of the time you won't need to install separate drivers when you install new third-party hardware.
The System Library
The kernel is responsible for critical functions such as memory management and device access, so programs must ask the kernel to perform work on its behalf. Programs communicate with the kernel through an application program interface (API) provided by a special library. This library defines some common data structures for describing system operations, provides functions to request these operations, and handles shuttling data back and forth between the kernel and other programs. This library is simply called the system library.
As you might imagine, every program on Mac OS X links against this library, either directly or indirectly. Without it, a program would be unable to allocate memory, access the file system, and perform other simple tasks.
The system library takes the form of a dynamic library installed as /usr/lib/libSystem.B.dylib. Mac OS X also includes a framework called System.framework in /System/Library/Frameworks that refers to this library. The files that define the Darwin interface live in the /usr/include directory. By the way, neither of these directories is visible from Finder; Mac OS X actively hides much of the complexity of Darwin from the average Mac user.
Unix users interact with their systems using command-line tools. These tools typically perform very specialized functions, such as listing files in a directory or displaying files on-screen. The advantage of supplying many specialized tools lies in the way commands can be combined to form more sophisticated commands. For example, a command that lists the contents of a directory can be combined with a program that lists text in "pages" for easy reading.
As you have learned, you use the Terminal application to gain access to Darwin's command-line tools.
The following Try It Out looks at Darwin's command-line interface. You start by browsing files using the command-line, looking up command information in Darwin's online help system, and running a command that displays its own arguments.
Excerpted from Beginning Mac OS X Programming by Michael Trent Excerpted by permission.
All rights reserved. No part of this excerpt may be reproduced or reprinted without permission in writing from the publisher.
Excerpts are provided by Dial-A-Book Inc. solely for the personal use of visitors to this web site.