← How to Write Macintosh Software

How to Write Macintosh Software

From the originals

13 scans

Machine-transcribed text and image descriptions. Check the linked original for uncertain words or fine print.

These are transcribed pages from the documents, not the full publications. Open the original for the remaining pages.

Image 1

HAYDEN Macintosh Library Apple Press How to Write Macintosh Software™ Scott Knaster

A book cover with a blue gradient background, orange top and bottom bars, a central framed illustration of a beige Macintosh computer with keyboard and mouse on a gray cracked surface under a cloudy sky, and green wavy lines crossing the blue background.

Reading notes: The Apple Press logo and the HAYDEN Macintosh Library script wordmark are stylized; their exact rendering was transcribed as printed text with approximate spelling. The trademark symbol after Macintosh in the title is rendered as a small TM mark.

Printed product names
Macintosh
Languages
English
PDF page 1

HAYDEN Macintosh Library Apple Press How to Write Macintosh™ Software Scott Knaster

A book cover with a blue gradient background, orange top and bottom bars, a green wavy line, and a framed photo of a beige Macintosh computer with keyboard and mouse on a cracked gray surface under a cloudy sky.

Reading notes: Series name 'Macintosh Library' is partly stylized in green script and may be read with slight variation. Publisher logo text 'Apple Press' is shown beside a small colored logo.

Printed product names
Macintosh
Languages
English
PDF page 2

How to Write Macintosh TM Software Scott Knaster HAYDEN BOOK COMPANY a division of Hayden Publishing Company, Inc. Hasbrouck Heights, Ne.w Jersey /

Reading notes: Possible OCR spacing artifact in 'Ne.w Jersey' and the stray '/' line may be a scanning artifact.

Printed product names
Macintosh
Languages
English
PDF page 3

Apple believes that good books are important to successful com- puting. The Apple Press imprint is your assurance that this book has been published with the support and encouragement of Apple Computer Inc., and is the type of book we would be proud to publish ourselves. For Barbara and Jess Acquisitions Editor: BILL GROUT Developmental Editors: SCOT KAMINS/BILL GROUT Production Editor: RONNIE GROFF Composition and production: McFARLAND GRAPHICS &., DESIGN, INC. Cover art: GEORGE BAQUERO Cover design: JIM BERNARD Printer and binder: COMMAND WEB OFFSET, INC. Library of Congress Cataloging-in-Publication Data Knaster, Scott. How to write Macintosh software. Includes index. 1. Macintosh IComputerl-Programming. I. Title. QA76.8M3K68 1986 005.265 86-14825 ISBN 0-8104-6564-7 Macintosh is a trademark of Mcintosh Laboratory, Inc., licensed to Apple Computer, Inc. Lisa, Finder, Inside Macintosh, Mac\o\Tite, MacPaint, and MacDraw are trademarks of Apple Computer, Inc. TMON is a trademark of !COM Simulations, Inc. Copyright © 1986 by HAYDEN BOOK COMPANY. All rights reserved. No part of this book may be reprinted, or reproduced, or utilized in any form or by any electronic, mechanical, or other means, now known or hereafter in- vented, including photocopying and recording, or in any information stor- age and retrieval system, without permission in writing from the Publisher: Printed in the United States of America 1 2 3 4 5 6 7 8 9 86 87 88 89 90 91 92 93 94 YEAR

Reading notes: OCR-style spacing and hyphenation artifacts such as 'com-puting' and 'Mcintosh' in the copyright notice Printed trademark line 'Mac\o\Tite' appears garbled and may be misreading of MacTerm or similar name

Printed product names
Macintosh · Lisa · Finder · Inside Macintosh · MacPaint · MacDraw · TMON
Printed dates
1986 · 86-14825 · 86 · 87 · 88 · 89 · 90 · 91 · 92 · 93 · 94
Languages
English
PDF page 4

INTRODUCTION This book is for people who want to write software for the Macintosh. It's not an introductory book; you should be experienced in (or in the process of learning) a high-level language such as Pascal or C, or assembly language. It also helps to be familiar with the basics of how Macintosh programs are written. If you've read or even skimmed Macintosh Revealed by Stephen Chemicoff or Inside Macintosh from Apple, you'll benefit from this book. What This Book Will Tell You What's in this book that's not in the others just mentioned? This book explains lots of the mysteries and myths about Macintosh programming that aren't really covered anywhere else. It talks about how and where things are stored in memory. It discusses what things in memory may be moved around, and teaches you how to know exactly when they may be moved. It explains how to debug your applications using available debugging tools. It shows you how to examine your program's code to learn precisely what's going on when it runs. It also gives you dozens of facts, tips, and tricks that you can use to make your software more powerful. How This Book Was Written In my two years in the Macintosh Technical Support group at Apple, I've worked with hundreds of programmers in helping them write and debug their software. I've tried to put as much of my experience as possible into this book. I've also put in a lot of stuff that we've taught developers in our MacCollege training course. I wrote this book with several principles in mind: • No mysteries. I've tried very hard to explain things that people find confusing or puzzling about Macintosh programming and to leave nothing significant unexplained that's within the book's scope. iii

Reading notes: Source appears to be a book introduction rather than an advertisement; no primary headline is printed. Product names listed are book and software titles referenced in the text, not advertised products.

Printed product names
Macintosh · Macintosh Revealed · Inside Macintosh · MacCollege
Languages
English
PDF page 5

iv Introduction • No fuzziness. There's nothing more initating than a book that takes you to the brink of a great revelation, then wimps out with fuzzy, vague writing. This usually makes it obvious that the writer doesn't know any more and is ttying to get on to the next topic. I've tried to avoid fuzzi- ness by explaining things in depth. • No wizards. Sure, some people are smarter than others. It's not right, though, to withhold potentially useful information because you don't trust people to use it correctly. I've tried to refrain from holding back information. If it's a bad idea to use a particular feature or technique, I won't just say "don't," I'll tell you why. • All work and no fun is no fun. While we're going over all this stuft; it's a good idea to have some fun, too, so I've tried to keep the mood light enough to keep you awake, even while we work through the hea")' subjects. If you use the information presented in this book, you'll know an awful lot about the Macintosh, and your programs will be better. There's enough information presented here that you may not absorb all of it as you read it through. Don't wony about that. You can reread it as many times as you like, at no extra charge. The Macintosh has gained quite a reputation for requiring a lot from a programmer. For various reasons, it was incredibly hard to create Macintosh software when the computer first appeared in January of 1984. In the years that have passed since then, lots of things have happened to make Macintosh programming a little easier: • Dozens of programming languages and tools have been developed, most of them coming from outside of Apple. There are compilers for many languages, powerful debuggers, disk patching tools, and more. • More technical material on the Macintosh has been created. Apple has produced Inside Macintosh and technical notes, and many good (and bad) books on Macintosh programming have appeared. • The general level of knowledge in the programming community (wher- ever that may be-I think it's near Berkeley) has increased tremend- ously as Macintosh programmers everywhere have gotten smarter. • The Macintosh itself has become more powerful. The first Macintosh had 128K RAM and SOOK of disk space (if you had two disk drives). Today's Macintosh has up to 4 megabytes of RAM and as much disk storage as you want to pay for. Still, there are some things about Macintosh programming that aren't wen· known and aren't written down anywhere. In this book, I've tried to gather together lots of "mysterious" things and take all the mystexy out of them. As you read, you'll discover that things aren't so hard to understand after all-they're just not well known.

Reading notes: OCR-style damage in several words such as 'initating', 'ttying', 'stuft', 'wony', 'wen·', and 'SOOK' (likely '500K') Some hyphenated line breaks and garbled characters ('hea")'' and 'mysterxy') may affect exact reading

Printed product names
Macintosh · Inside Macintosh
Printed dates
January of 1984
Languages
English
PDF page 6

ACKNOWLEDGMENTS ~---~~-·-----------------------------------------------------------,,_--- without these people, this book would not exist, and you would now be staring at nothing and looking very silly. Jim Friedlander of Macintosh Technical Support helped im- measurably. He read the entire manuscript several times and kept me honest; he wrote most of the programs in Part Three; and he helped me keep up my energy level with his enthusiasm. Martha Steffen, Apple Publishing Evangelist, helped me figure out how to get started and what was happening to me along the way. Scot Kamins, Writeur, encouraged me, told me funny jokes, and gave me a great review. I also stole ideas from his books. Caroline Rose inspired me by being the best technical writer I've ever known, and by writing Inside Macintosh, without which no other Macintosh technical books would be possible. Cary Clark and Russ Daniels were very smart (and still are) and taught me a lot of the stuff that appears in this book. Bryan Stearns, Ginger Jernigan, Mark Baumwell, Louella Pizzuti, Bill Dawson, and Rick Blair of Apple Technical Support put up with me whenever I rambled on about this book. Chris Espinosa invented the tree-crash metaphor used in Chapter 4, which is used with his permission, whether he knows it or not. Alain Rossmann gave me valuable ideas for the "Techniques" section and provided lots of enthusiasm. Mike McGrath gave me great advice on how to write a book and get it published and loaned me his copy of Author Law. Jean-Louis Gassee has encouraged creativity and fun at Apple in the post-Steve Jobs era. Guy Kawasaki changed my life, and I have the bumper sticker to prove it. Barbara Knaster created many of the figures, constructed Appendix E, fixed lots of stuff, and, most important, told me to do it and made me do it. v

Reading notes: Header line contains garbled characters (~---~~-·---) at the start of the text Page number 'v' at the end is ambiguous

Printed product names
Macintosh · Inside Macintosh · Author Law
Languages
English
PDF page 7

ABOUT THIS BOOK

ABOUT THIS BOOK This book is divided into three parts, plus appendices. Here's what you'll find: Part One is called "How It All Works." This part of the book con- centrates on general information about the Macintosh and how to program it, with an in-depth discussion on how things in RAM behave and misbehave. Chapter 1, "Getting Started," talks about what makes Macintosh programming different and presents a brief overview of development tools. Chapter Z, "Things in Memoiy," tells about the way things are stored in memoiy, what happens to them, and how you can manage them. Chapter 3, "More about Heaps and Fragmentation," describes some common memoiy management problems and how to avoid them. Part Two is called "Debugging," which kinda tells you what it's about. Chapter 4, "Debugging Macintosh Software," introduces some general thoughts about debugging, tells about the TMON debugger, and presents information about many of the most common problems that Macintosh programmers run into. Chapter 5, "Examining Compiled Code," shows you how to look at the object code produced by your high-level language program. Chapter 6, "More about Compiled Code," continues directly from Chapter 5, but it would have made one big, obnoxious chapter if it had been all together, so it's not. Chapter 7, "Real Live Debugging," presents an example program that has bugs in it and then uses an object code debugger to find and fix the problems. vii

Reading notes: OCR-like spacing and letter errors such as 'memoiy' and 'Z' for 2 in chapter headings

Printed product names
Macintosh · TMON
Languages
English
PDF page 8

viii About This Book Part Three is called "Tips, Tricks, and Techniques, " and it's got some of each of those, including a few example programs. Chapter 8 1 "General Techniques," presents stuff that's generally use- ful if you're writing Macintosh software. Chapter 9, "Toolbox Techniques," contains information on some goodies that will help you make better use of the Macintosh's User-In- terlace Toolbox. Chapter 10, "Operating System Techniques, " covers some good things to know about various parts of the Macintosh Operating System. Appendix A, "Assembly Language OveIView," is very important if you're not familiar with 68000 assembly language. To use an object code debugger, you should be able to read 68000 code and understand what's going on. You don't have to be able to write assembly language. This appendix will tell you what all those strange instructions mean. You can read this appendix before reading Part Two or at any point that you think you need it. Even if you're already a 68000 programmer, you might want to read it to learn some fine points. Don't forget about Appendix A \>\/hen you start to come across assembly language information that's unfamiliar to you, try reading Appendix A It should help a lot. Appendix B, "Common Problems," lists some of the most common (and most fatal) errors that can cause a Macintosh programmer to lose sleep. Apppendix C, "Macintosh Plus," summarizes the new features in the Macintosh Plus computer. Although there's Macintosh Plus information throughout the book, you can use this appendix as a single source of information. Appendix D, "A Guided Tour of Macsbug, " presents detailed infor- mation about the Macsbug debugger that Apple distributes. Appendix E, "Debugging Quick Reference Guide," contains some charts that you'll find very handy when debugging. At the end of the book you'll find a glossary of whizzy terms.

Reading notes: Possible OCR errors in 'Interlace' (likely 'Interface') and 'Apppendix', and in 'IIOV'/'\>\/hen' markup-like spans Reading order of appendix list is sequential but page header 'viii' precedes body text

Printed product names
Macintosh · Macintosh Plus · Macsbug · 68000
Languages
English
PDF page 9

ix About This Book Conventions Used in This Book ~ • Whenever you see this typeface: GetNextEvent you'll know that it's the name of a call to the Macintosh User Interlace Toolbox or Operating System, or a line of a Pascal, C, or · assembly language program. All words in the text that are in boldface can be found in the glossaiy, except the ones that are names of books. Hexadecimal numbers are always preceded by a dollar sign, like this: $388 -$42 $40F7A2. Numbers not preceded by a dollar sign are decimal numbers unless otheIWise stated. Calls to routines in the Macintosh User Interlace Toolbox and Operating System are referred to as system calls, ROM calls, traps, or A-traps, depending on the context. All these terms mean the same thing. Note for C programmers: whenever you see Pascal-specific terms, you should substitute their C equivalents. Unless otheIWise noted, the infor- mation is valid for both languages. Here are some Pascal and C terms that are equivalent in this book: Pascal procedure or function local variable c function automatic variable A paragraph that looks like this is an incidental note. It contains information that's interesting and useful, but not vital to the material. Warning A paragraph marked with this icon is a warning. Imagine Robby the Robot on Lost in Space saying, "Danger, Will Robinson!"

Reading notes: Typeface-marked terms such as GetNextEvent and the line ending 'Interlace' appear as OCR-like variants of 'Interface' and may be damaged characters.

Languages
English
PDF page 10

- ·- X About This Book l Macintosh Plus This icon marks information that's specifically for the Macintosh Plus. - t:J~ i------~ -- 1.oo __ kin _ · __ g_ Ah __ e_ ad ____________________________ __. You'll find this crystal ball icon on paragraphs that discuss products that have been announced by manufacturers, but haven't yet (at the time of the writing) become available. For example, information about the Macintosh Programmer's Workshop from Apple is marked with this icon. Fact A sentence that looks like this is an important, simple truth that you should know. These sentences present facts that help to take the mystery out of Macintosh programming. Things to Remember This icon, found at the end of each chapter, marks a list of key points and concepts that were introduced in that chapter. The Amazing Colossal Disclaimer There's a lot of technical information in this book. It's been reviewed carefully by several people, and all the known problems have been fixed. Just as with software, though, there may be undiscovered bugs. Also, as the Macintosh world evolves, things change. If you find anything wrong, I would very much appreciate it if you would write to me about it in care of Hayden Book Company.

Reading notes: Dash-and-underline artifacts near the 'Looking Ahead' heading are uncertain. Reading order of the margin icon labels relative to body paragraphs is unclear.

Printed product names
Macintosh Plus · Macintosh Programmer's Workshop
Languages
English
PDF page 11

xi About This Book What You Need to Use This Book To make the most of this book, you should be somewhat familiar with Pascal or C. You should also have access to a copy of Inside Macintosh, the technical reference manual for the Macintosh computers. In Part Two, we spend a lot of time looking at programs with a debugger. The debugger that's used for the examples is TMON, published by Icom Simulations, and it's generally regarded as the most powerful debugger available for the Macintosh. If you don't have TMON, you can do some debugging with Macsbug, the debugger that Apple distributes with a variety of software, including the Macintosh 68000 Development System and the Macintosh Software Supplement. This book presents a lot of specific information, such as examples of how compilers compile code. Although this specific information is very useful, you should realize that this book's true mission is to tell you that you should obseive your program's behavior closely and figure out for yourself what's going on. In particular, the compiled code examples given in Part Two are correct for Workshop Pascal, but may be different for other Pascal and C compilers. The only way to know how your compiler does things is to look at the code it produces. How to Learn More The best way to learn more about Macintosh programming is to write programs! A good source of information is your local user's group. Many user's groups have special-interest groups for programmers, and most groups publish newsletters. You might also want to join an on-line bulletin board service. These services are frequented by Macintosh fanatics who are able to answer almost any Macintosh programming question, or at least point you in the right direction. These services also contain lots of public domain software that you can download, including the latest goodies from Apple. There are also a number of good books available, such as books specifically for Pascal, C, or assembly language programming on the Macintosh. More books are appearing constantly, so keep checking you favorite bookstore. One reason there's so much third-party software that runs on Apple computers is that Apple has a lot of programs that help developers in a variety of ways. These programs include marketing assistance, developer-programmer matchmaking, technical support (of course), and more. In addition, Apple distributes lots of different products, like tech-

Reading notes: OCR-style errors such as 'obseive' and 'you favorite' may be damaged or misprinted characters

Printed product names
Inside Macintosh · TMON · Macsbug · Macintosh 68000 Development System · Macintosh Software Supplement · Workshop Pascal
Languages
English