Anton's MiniApples Page
This page was inspired by a presentation on getting started with MacOS programming
that I gave at a MiniApples SIG meeting.
If you find it useful, let me know, and perhaps I'll expand it in the future.
You may want to skip to the list of other online MacOS references.
Approaches To Programming MacOS
Different people have different reasons to look into programming. Two key decisions to make
early on are which type of program you want to write, and which type of programming environment
you want to use. Which combination of these is the best fit depends on the problem you're trying
to solve.
Types of Programs
MacOS supports several different types of programs. Generally, a program will fit neatly into
one of these categories. Occasionally, the best solution to a problem may involve a combination
of two or more. Some of the more important (and well-defined) are listed here.
- Applications
-
Applications are the familiar programs that you see as you work on your Macintosh. They
usually have menus and windows, and are the best solution for any program which involves
a user interface and is explicitly run by the user.
- Background-only Applications
-
Background-only applications have no user interface. They are often used when a program must
run without user interaction. The support software for desktop printers, which sends printed
documents to a printer while the user works on other tasks, is an example. (Desktop printers
have a user interface which is provided by the Finder; similarly, other background applications
can be paired with applications to provide a user interface.)
- Extensions
-
Extensions "patch" system software to change behavior. These tend to be very complex, as they
must interact with both system software and any application which might use the extension; but
they are very powerful, as they can modify the behavior of any application.
- Plug-ins
-
Plug-ins also modify the behavior of an application, but work with particular applications
which were designed to support plug-ins. The "host application" provides services which the
plug-in can use. For instance, a PhotoShop plugin can retrieve part of an image, modify it
in some way, and save the modified image. Developing a plug-in requires the use of an "SDK"
(Software Development Kit) provided by the application developer.
- Drivers
-
Drivers communicate (directly or indirectly) with hardware devices. They don't have a user
interface, but work behind the scenes. For instance, a disk driver enables a Macintosh
computer to work with a particular type of disk drive; a video driver helps an application
to draw on the screen of a monitor attached to a particular graphics card.
Types of Programming Environments
There are a variety of programming "environments" (applications that enable programming) available
for the MacOS. More important than the particular application chosen is the type of
environment, the approach it takes to programming. I've identified a few types, listed here with
some advantages and disadvantages. (Yes, these are very broad categories.)
- Raw Toolbox
-
These environments, the most traditional in some sense, require the programmer to write all
of the code making up their program. The programmer is responsible for calling all of the
MacOS toolbox routines as necessary, as well as designing the program's interface and writing
the code to accomplish its task. Advantages of this approach include the very detailed control
available to the programmer, and that the programmer can work with very small pieces of code;
the primary disadvantage is that it's a lot of work, since the programmer is forced to address
many small details. Examples are C and Pascal compilers.
- Frameworks
-
These environments, generally built on top of a raw-toolbox compiler, handle many of the
common tasks (like setting up menus or responding to standard events) for you. Advantages
of this approach include that much of the "busywork" is done for the programmer; the disadvantages
are that the frameworks are usually harder to learn, and that the framework adds some overhead
(in size and speed) to the finished program. Examples are MacApp and PowerPlant.
- Higher-Level Tools
-
These environments, sometimes called "Rapid Application Development", are designed to let the
programmer focus on only the task that the program needs to accomplish; they handle all of the
common tasks, and provide tools to quickly build menus and windows and connect them with the
appropriate code. The main advantage is that it takes very little time to build a program;
the disadvantages include somewhat less flexibility and a lot of added overhead. Examples
include REALbasic.
- Scripting
-
While scripting may not be thought of as a programming environment, it does allow quite complex
tasks to be solved, and may be the best solution if existing software can be combined in new
ways to solve a task. The main advantages of scripting are that there's no low-level work at
all for the programmer, and that other applications can be easily controlled; the main
disadvantage is that it's only possible to build on functionality that has already been written
as part of another application or part of the scripting environment. Examples include
AppleScript and Frontier.
The MacOS Operating System
MacOS is a complex system which has evolved over the past fifteen years. It can be broken
up into two main pieces. The operating system itself provides services to applications, such as
memory and file management. The toolbox provides the familiar user interface, such as windows
and menus. Most programs written for MacOS will use both the operating system and toolbox.
Key Concepts
Here are a few key concepts which you should understand when writing your software. (Perhaps
the references below will help.)
- Pointer
-
A pointer is a reference to an object which doesn't move in memory. Programs often use pointers
to work with data whose size is not known when the program is written. The MacOS toolbox uses
pointers for some objects.
- Handle
-
A handle is a reference to an object which may move in memory. The MacOS toolbox uses handles
for many objects; this allows the toolbox to rearrange memory when needed to allow larger objects
to be allocated. Handles can also be used to reference resources, whether located in memory or
on disk.
- Resource
-
A resource is an object stored in a file on disk. Each resource has a type, identified by a
four-letter code. Resources are used extensively in the toolbox to describe user interface
elements such as windows or dialogs. Resources are also used for localization; by storing
all strings in resources, a program can be translated into a new language without changing
any of its code.
- GrafPort
-
A GrafPort is an object that can be drawn into. Many programs don't use GrafPorts explicitly,
but use windows (which are a special type of GrafPort visible on the screen) instead.
- Window
-
A window is a type of GrafPort which is located on the screen and has a surrounding frame.
Almost all drawing takes place inside a window, and the user interacts primarily with windows.
- Control
-
A control is an element in a window which responds to user actions such as clicking the mouse.
Buttons and scroll bars are examples of controls.
- Dialog
-
A dialog is a window which contains text and controls. A special manager in the toolbox
handles user interaction with the items in a dialog, allowing complex windows to be designed
while writing very little code.
- Menu
-
A menu is an element that a user can select items from. There are three primary types of menus.
Standard menus appear as part of the menu bar at the top of the screen; pop-up menus are displayed
as controls inside a window; and contextual menus appear when the user control-clicks on an
object.
- Event
-
An event is a significant occurrence, usually a user's action such as clicking the mouse or
typing a key. Other types of events include AppleEvents, which are sent from one program to another
and used for scripting, and update events, which indicate when a window needs to be redrawn.
- Event Loop
-
Most programs in MacOS are written using an event loop, which repeatedly gets an event from the
user, decodes the event, and responds to it. This provides the Macintosh experience; the computer
is always responding directly to the user's actions.
Important Operating System Managers
Here are some of the more important operating system managers.
- Memory Manager
-
The Memory Manager handles allocating memory for objects needed by a program, managing pointers
and handles.
- File Manager
-
The File Manager handles reading from and writing to the files on disks.
- Resource Manager
-
The Resource Manager provides access to the resources stored in a file.
- Process Manager
-
The Process Manager shares the computer between programs.
- Device Manager
-
The Device Manager provides access to hardware devices, such as the keyboard.
- OpenTransport
-
OpenTransport provides access to networking, including the internet's TCP/IP protocol.
Important Toolbox Managers
Here are some of the toolbox managers which are important for most programs.
- QuickDraw
-
All drawing is handled by QuickDraw, which can draw shapes or text onto the screen.
- Window Manager
-
Implements windowing, including support for overlapping windows.
- Control Manager
-
Draws and handles manipulation of common user interface elements in a window.
- Dialog Manager
-
Draws and handles a collection of controls in a window.
- Menu Manager
-
Draws and handles user clicks on menus.
- Appearance Manager
-
Supplements the window, control, dialog, and menu manager with new features added in MacOS 8.
- Event Manager
-
Receives events from the user and makes them available to applications for processing.
On-line materials
Books
- Inside Macintosh (volumes I-VI)
- Inside Macintosh (the new series)
- Macintosh Revealed
- (more to be added as I get a chance to look at them...)
Sample Code
This sample code is for a very simple application which graphs quadratic equations.
Download a sample application written in C using CodeWarrior.
Download a sample application written in BASIC using RealBASIC.
(To Anton's main page.)