Programming Mac OS X: a GUIDE for UNIX DEVELOPERS
Total Page:16
File Type:pdf, Size:1020Kb
Programming Mac OS X: A GUIDE FOR UNIX DEVELOPERS KEVIN O’MALLEY MANNING Programming Mac OS X Programming Mac OS X A GUIDE FOR UNIX DEVELOPERS KEVIN O’MALLEY MANNING Greenwich (74° w. long.) For electronic information and ordering of this and other Manning books, go to www.manning.com. The publisher offers discounts on this book when ordered in quantity. For more information, please contact: Special Sales Department Manning Publications Co. 209 Bruce Park Avenue Fax: (203) 661-9018 Greenwich, CT 06830 email: [email protected] ©2003 by Manning Publications Co. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission of the publisher. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in the book, and Manning Publications was aware of a trademark claim, the designations have been printed in initial caps or all caps. Recognizing the importance of preserving what has been written, it is Manning’s policy to have the books they publish printed on acid-free paper, and we exert our best efforts to that end. Manning Publications Co. Copyeditor: Tiffany Taylor 209 Bruce Park Avenue Typesetter: Denis Dalinnik Greenwich, CT 06830 Cover designer: Leslie Haimes ISBN 1-930110-85-5 Printed in the United States of America 12345678910–VHG–05 040302 brief contents PART 1OVERVIEW ............................................................................. 1 1 ■ Welcome to Mac OS X 3 2 ■ Navigating and using Mac OS X 27 PART 2TOOLS ................................................................................... 55 3 ■ Project Builder and Interface Builder 57 4 ■ Development tools 109 PART 3PROGRAMMING .................................................................. 169 5 ■ Objective-C and the Cocoa development frameworks 171 6 ■ Cocoa programming 203 7 ■ AppleScript programming 245 8 ■ Mac OS X and beyond 279 v contents foreword xiii preface xv acknowledgments xviii about this book xix about the author xxiii about the cover illustration xxiv PART 1OVERVIEW .................................................................. 1 Welcome to Mac OS X 3 1 1.1 Introduction 4 Origins of Mac OS X 5 1.2 The Macintosh user interface 6 1.3 The Mac OS X user interface 8 The desktop 8 ■ Menus 8 ■ The Dock 10 Window layering 11 ■ Dialog boxes 11 ■ Drawers 12 Keyboard navigation 12 ■ Other interface features 13 1.4 The Mac OS X architecture 13 Architecture layers 15 ■ The kernel environment 16 Core Services layer 20 ■ Application Services layer 21 Application Environment layer 22 ■ Aqua 26 1.5 Summary 26 vii viii CONTENTS Navigating and using Mac OS X 27 2 2.1 Introduction 28 2.2 Shells 29 Terminal features 31 2.3 Help system 32 Help Viewer 33 2.4 User accounts and privileges 33 Creating user accounts 34 2.5 Booting and default services 36 2.6 Programs and Mac OS X bundles 37 2.7 Security issues 39 2.8 File system 39 Finder 41 ■ Case sensitivity and pathname delimiters 43 2.9 Single-user mode 44 2.10 System log files 45 2.11 Processes management 45 2.12 Common commands and tools 46 2.13 Scripting languages 48 AppleScript 48 2.14 Development tools 50 2.15 X Window under Mac OS X 51 Installing the X server 52 2.16 UNIX to Mac OS X software projects 53 2.17 Summary 54 PART 2TOOLS....................................................................... 55 Project Builder and Interface Builder 57 3 3.1 Introduction 58 Macintosh Programmer’s Workbench 59 THINK Pascal and THINK C 59 ■ CodeWarrior 60 Project Builder and Interface Builder 60 3.2 Creating an application with Project Builder 62 CONTENTS ix 3.3 Project Builder in depth 67 Targets and build styles 67 ■ Project Builder’s UNIX tools 68 Project Builder’s interface 69 ■ Project Builder scenarios 78 3.4 Creating an application with Interface Builder 100 Interface Builder scenarios 101 3.5 Summary 108 Development tools 109 4 4.1 Introduction 110 4.2 UNIX development tools under Mac OS X 112 Editors 112 ■ Mac OS X editing tools 113 Version control 117 ■ Static code analysis tools 121 4.3 Compilers and build tools 122 4.4 Mac OS X Aqua-based development tools 122 UNIX-based editors 122 ■ Mac OS X-based editors 127 4.5 Apple’s GUI-based development tools 127 Apple Help Indexing Tool 128 ■ AppleScript Studio 128 FileMerge 129 ■ Icon Composer 132 Interface Builder 132 ■ JavaBrowser 133 MRJAppBuilder 134 ■ MallocDebug 135 ■ ObjectAlloc 143 PEF Viewer 143 ■ PackageMaker 144 ■ Pixie 144 Project Builder 144 ■ PropertyListEditor 144 Quartz Debug 146 ■ Sampler 147 ■ Thread Viewer 150 icns Browser 155 4.6 Apple’s command-line development tools 156 ps (process status) and top (system usage statistics) 156 sc_usage: showing system call usage statistics 158 fs_usage: reporting system calls and page faults related to the filesystem in real-time 160 ■ gprof: displaying execution profile data 161 ■ leaks: searching a process’s memory for unreferenced malloc buffers 163 ■ heap: listing all the malloc-allocated buffers in the process’s heap 165 malloc_history: showing malloc allocations that a process has performed 165 ■ sample: profiling a process during a time interval 166 4.7 Summary 167 x CONTENTS PART 3PROGRAMMING....................................................... 169 Objective-C and the Cocoa development frameworks 171 5 5.1 Introduction 172 5.2 Introduction to Objective-C 173 Object-oriented terminology 174 ■ Classes 175 Messages 177 ■ Categories 178 ■ Protocols 180 Other features 180 ■ Why learn Objective-C? 181 5.3 Cocoa software infrastructure 182 Foundation 182 ■ Application Kit 187 Memory management 188 ■ Design patterns 193 Cocoa event handling 197 5.4 Other Cocoa development languages 200 C++ 201 ■ Perl 201 ■ Ruby 202 5.5 Summary 202 Cocoa programming 203 6 6.1 Introduction 204 6.2 The CocoaWGet example program 205 6.3 Program requirements 207 6.4 Program design 208 6.5 Building the interface 209 Opening the project 209 ■ The interface components 210 Control alignment and spacing 212 ■ Forms 215 Classes and instances 215 6.6 CocoaWGet: implementing code with Project Builder 220 The model 221 ■ The view 224 ■ The controller 224 6.7 Program extensions 233 Letting the user cancel downloads 234 The application icon 239 ■ The help file 241 6.8 Summary 243 CONTENTS xi AppleScript programming 245 7 7.1 Introduction 246 7.2 Scripting languages 247 7.3 AppleScript 248 Creating and running a script 250 ■ Types of AppleScripts 251 AppleScript extensions 252 ■ The AppleScript language 254 Choosing a scripting language 264 7.4 Example applications of AppleScript 264 iTunes and AppleScript 264 ■ AppleScript Studio 269 7.5 Summary 278 Mac OS X and beyond 279 8 8.1 Introduction 280 8.2 Development tools 281 Compilers 281 ■ Project Builder 283 ■ Changing compilers 283 Inline scripting 283 ■ New target editor 286 Searching documentation 287 8.3 Terminal application 289 Setting Terminal preferences 289 Splitting the Terminal window 292 Other Terminal additions 293 8.4 The PerlObjCBridge 293 PerlObjCBridge example 295 8.5 Summary 300 A Getting and installing development tools 301 UNIX and Mac OS X command mappings 303 B B.1 Common Mac OS X operations 304 B.2 UNIX file/directory commands mapped to Mac OS X commands 304 List directory contents: ls 304 Copy/move files or folders: cp, mv 305 Remove files or folders: rm 305 ■ Change directory: cd 305 xii CONTENTS Create a new directory: mkdir 305 Change file permission and group: chmod, chgrp 306 Compare files: diff 306 ■ Get the word, line, or byte count: wc 306 Compress and decompress data: compress, uncompress, tar, gzip, gnuzip, unzip, zcat 306 ■ Edit text files: emacs, vi 306 View files: head, tail 306 ■ Find files: find 307 B.3 UNIX communication commands mapped to Mac OS X commands 307 OpenSSH: ssh, scp 307 ■ Talk to another user: talk, ytalk 307 B.4 UNIX process management commands mapped to Mac OS X commands 307 Show system and process usage statistics: top, ps 307 Terminate a process: kill 307 The precursor of Mac OS X: Mac OS 309 C C.1 A tour of the Mac OS interface 310 C.2 Interacting with the system 312 C.3 Mac OS system components 313 System file and Finder 314 ■ Process scheduling 314 Memory management 315 Extending the system through system extensions 317 Interapplication communication (IAC) 318 File system 319 ■ Macintosh files 319 ■ Graphics 320 Networking 321 A brief history of UNIX 323 D D.1 The origin of UNIX 324 High-level languages and punch cards 324 Batch processing 325 ■ Time-sharing 326 D.2 The birth and development of UNIX 328 D.3 GNU, Free Software Foundation, and open source 333 D.4 UNIX software development philosophy 335 resources 337 index 345 foreword Apple’s release of the Macintosh in 1984 heralded a computer revolution in ease of use for nontechnical people. Over time, computers and computer interfaces split into three main camps: Microsoft Windows, the Macintosh, and the various flavors of UNIX. UNIX has been a traditional favorite of the research and scientific community for a variety of reasons. With the rise of Linux, it has become more popular than ever. Now Apple has brought the worlds of Macintosh user experience and UNIX together to form Mac OS X. With a full-featured UNIX system as the driving engine, the two worlds have merged. All that remains is to create better software for this new blended environment. This has proven to be challenging. Many UNIX developers haven’t written code for graphical user interfaces, while many Macintosh developers haven’t written code based on UNIX environments. Bringing these two diverse types of devel- opers to the same playing field can be difficult because they each need to learn from the other.