Programming
Cocoa
with
Ruby
Create
Compelling
Mac
Apps Using
RubyCocoa
Brian Mariek
The
Pragmatic
Bookshelf1 Introduction 1
1.1 WhatIs Cocoa? 2
1.2 What Is
RubyCocoa?
21.3 What's ItLike toLearn Cocoa
Using
Ruby?
21.4
RubyCocoa?
That's So Last Year! 31.5
Prerequisites
31.6 Versions 5
1.7 Our
Example App
61.8 Centuries of the Bookmaker's Art: Scorned 8
1.9 Some
Terminology
91.10 Service After the Sale 9
1.11
Solving
Problems 91.12
Acknowledgments
102 How Do We Get This
Thing
Started? 132.1 A
Program
That Prints 142.2
Putting
anItem in the Status Bar 172.3 Menus 18
2.4 An
Application
Bundle 222.5 WhatNow? 26
I A First Realistic
App
273
Working
with Interface Builder and Xcode 293.1 The Basics 30
3.2
Creating
andEditing
Classes in Xcode 403.3
Debugging
473.4
Synchronizing
Interface Builder and Xcode 49 3.5 Attributes 50vlii CONTENTS
3.7
Tiy
This Yourself 533.8 What Now? 53
4 One Good
App
Observes Another 554.1 Notifications Within an
App
554.2 Notifications Between
Apps
604.3 The
App
to Fenestrate 63 4.4Putting
NotificationHandling
Behind the GUI 644.5
Reopening Objective-C
Classes 664.6 What Now? 66
II
Reshaping
Fenestra 695 A Better GUI 71
5.1
Toggle
Buttons 725.2 The Default Button 73 5.3 Combo Box Items 74
5.4 The Initial First
Responder
755.5
Try
ThisYourself 755.6 What Now? 76
6
Decoupled
Controllers 776.1
Ignorant Objects
786.2
Extracting
Subclasses 806.3
Reacting
to Button State 856.4
Using
Nibs toAvoidDependencies
856.5
Initializing
Combo Boxes 876.6 What Now? 88
7 Notifications Connect
Decoupled Objects
89 7.1 Controllers 897.2 Translators and the
Rising
Tide ofUgliness
917.3 WhatNow? 94
8 More
Expressive
Code 958.1 A DSL for Notifications 96 8.2
RubyCocoa
Has TwoWays
ofReferring
toSuperclasses
988.3 Shorthand for
Posting
Notifications 988.4
Try
ThisYourself 100III
Project
Mechanics
103
9
Bundling
Gems and Libraries withYourApp
1059.1 Manual Control 106
9.2
Standaloneify
1109.3 What Now? 112
10
Project Organization,
Builds,
and Your Favorite Editor 11310.1
Groups
11410.2
Using
Xcode with HierarchicalProject
Folders 11510.3
Running
in Place 11710.4
Building
Without Xcode 11710.5
Using
Interface Builder with HierarchicalProject
Folders 11910.6
Starting
aNewProject
12010.7 What Now? 121 IV Declarative Data
Handling
12311 PersistentUser Preferences 125 11.1 The User Preferences
System
126 11.2Storing
CustomObjects
as Preferences 12911.3
Using
ArchivedObjects
13711.4 Views Can PullData 141
11.5
Try
This Yourself:ASticky
Window 143 11.6 What Now? 147 12Creating
aPreference Panel in aNew Nib 14912.1
Creating
a Nib 15012.2
Drawing
the Panel 15212.3
Hooking
the Panel to theApp
15412.4 The Nib File's Owner 157
12.5 IB's First
Responder Pseudo-Object
15812.6
Memory
Leaks 15912.7 What Now? 160
13
Implementing
a Preference Panel with CocoaBindings
16113.1
Binding
aSimple
Value 16113.2
Binding
anArray
of Hashes 16513.3 Formatters 171 Transformers
x CONTENTS
13.5
Adding
andDeleting
Table Rows 18113.6 What Now? 183
14
Setting
Up
Bindings
with Code 18514.1 Oh No!
Terminology!
18514.2
Using
RootedKeypaths
inCode 18914.3
Subclassing NSArrayController
18914.4
bind_toObject_withKeyPath_options
192 14.5 What Now? 196V
Fun
with Tables 19715
Prologue:
Folders and Tests 19915.1 Disk
Layout
19916 Selections and
Editing
20316.1 An
Example
ofCreating
Tests: The AddMethod .... 20316.2
Working
with anUncooperative
Control 21316.3
Try
This Yourself 22116.4
Building
Setup
Methods 22816.5 What Now? 230 17 Buttons in Tables 231
17.1 Cells 231
17.2
Making
theChange
23217.3 What Now? 234 18A Formatter with Two Wrinkles 235
18.1 The Formatter Code 236 18.2
Calling
Methods ThatTake ReferenceArguments
... 23818.3
Breaking Encapsulation
in Tests 24118.4 What Now? 243
19
Picking
Files withOpen
Panels 24519.1
NSOpenPanel
24519.2 A
Design
forUsing NSOpenPanel
inFenestra 24819.3
Try
This Yourself: PreferencesControllerTests 250 19.4Try
ThisYourself: TheNSOpenPanel
Controller .... 25820
Drag
andDrop
26320.1 How
Drag
andDrop
Works 26320.2
Designing
the GUI 26520.3 A
Template
for the Solution 26620.4
Utility
Classes and Modules 26720.5
Try
This Yourself:Lively
Dragging
Info 270 20.6Try
ThisYourself:Drag
andDrop
27720.7 Does It Work? 282
20.8 What Now? 283 21
Epilogue:
AWonderful World ofTests 28521.1 Test-Driven
Design
28521.2 To Learn More 288
VI
Wrapping
Up
28922 Fit and Finish 291
22.1
Saving
the Window Position UntiltheNext Launch . . 29122.2 Tab Behavior 292 22.3
Using
NSMatrix toOrganize
Buttons 29322.4
Sizing
29722.5
Cleaning
Up
the Menu Bar 30122.6 The About Window 301
22.7
Changing
theApplication's
Name 30323
Adding Help
30523.1
Help
Book Basics 30523.2
Creating
aHelp
Book 30623.3
Editing
Pages
30723.4
Hooking
aHelp
Book intoanApp
31323.5 AWorkflow for
Creating Help
BookPages
31523.6
Tooltips
31524 Document-Based
Applications
31724.1 The
Major Players
31824.2 The
Responder
Chain 32024.3
Creating
a New Document 32324.4
Opening
andSaving
Documents 33224.5
Editing
334xli CONTENTS 25
MacRuby
339 25.1Getting
MacRuby
342 25.2MacRuby
Basics 342 25.3 AMacRuby
Checklist 344 25.4 What Now? 348 VII Reference 34926 The
Objective-C
Bridge
andBridge
Metadata 35126.1 An
Unexpected
Return Value 35126.2 What Information Can Be Found at Runtime? 352
26.3
Supplementing
Runtime Information 353 26.4 Our Own Private Metadata 35426.5
Finding
Out More 35527 The
Underpinnings
ofCocoaBindings
35727.1
Requirements
35727.2 Our Goal 358
27.3
Declaring
ObservedProperties
35927.4
Observing
Changes
36027.5
Implementing bind_toObject_withKeyPath_options
. . 36227.6
Changing
the Value ofan ObservedKey
36327.7 In
Summary
36427.8
Postscript: Observing Changes
to Collections 366A
Glossary
369B
Bibliography
381