Learn AppleScript - Hamish Sanderson, Hanaan Rosenthal, Ian Piper, Barry Wainwright, Emmanuel Levy, Harald Monihart, Craig Williams, Shane Stanley

Learn AppleScript (eBook)

The Comprehensive Guide to Scripting and Automation on Mac OS X
eBook Download: PDF
2010 | 3rd ed.
1104 Seiten
Apress (Verlag)
978-1-4302-2362-7 (ISBN)
Systemvoraussetzungen
99,99 inkl. MwSt
  • Download sofort lieferbar
  • Zahlungsarten anzeigen

AppleScript is an English-like, easy-to-understand scripting language built into every Mac. AppleScript can automate hundreds of AppleScript-able applications, performing tasks both large and small, complex and simple.

Learn AppleScript: The Comprehensive Guide to Scripting and Automation on Mac OS X, Third Edition has been completely updated for Mac OS X Snow Leopard. It's all here, with an emphasis on practical information that will help you solve any automation problem-from the most mundane repetitive tasks to highly integrated workflows of complex systems.

  • Friendly enough for beginners, detailed enough for advanced AppleScripters
  • Includes major contributions from expert AppleScripters: Emmanuel Levy, Harald Monihart, Ian Piper, Shane Stanley, Barry Wainwright, Craig Williams, and foreword by AppleScript inventor, William Cook


Hamish Sanderson is a ten-year veteran of AppleScript and professional automation developer living and working in the U.K. Over the years, he has contributed a range of useful libraries and utilities to the AppleScript community and pops up with some regularity on the various AppleScript mailing lists. He is also the author of the highly regarded Appscript library, which brings AppleScript-style application scripting support to the Python, Ruby, and Objective-C languages on Mac OS X.
AppleScript is an English-like, easy-to-understand scripting language built into every Mac. AppleScript can automate hundreds of AppleScript-able applications, performing tasks both large and small, complex and simple.Learn AppleScript: The Comprehensive Guide to Scripting and Automation on Mac OS X, Third Edition has been completely updated for Mac OS X Snow Leopard. It's all here, with an emphasis on practical information that will help you solve any automation problem from the most mundane repetitive tasks to highly integrated workflows of complex systems. Friendly enough for beginners, detailed enough for advanced AppleScripters Includes major contributions from expert AppleScripters: Emmanuel Levy, Harald Monihart, Ian Piper, Shane Stanley, Barry Wainwright, Craig Williams, and foreword by AppleScript inventor, William Cook

Hamish Sanderson is a ten-year veteran of AppleScript and professional automation developer living and working in the U.K. Over the years, he has contributed a range of useful libraries and utilities to the AppleScript community and pops up with some regularity on the various AppleScript mailing lists. He is also the author of the highly regarded Appscript library, which brings AppleScript-style application scripting support to the Python, Ruby, and Objective-C languages on Mac OS X.

Title Page 1
Copyright Page 2
Contents at a Glance 3
Table of Contents 5
Foreword 19
About the Authors 21
About the Technical Reviewer 22
Acknowledgments 23
Introduction 24
If You Have Never Scripted Before… 24
If You Are Already an AppleScript User… 25
If You Are an Experienced Programmer… 26
Resources 27
Formatting Conventions Used in This Book 27
Errata 28
Part I Welcome to AppleScript 29
Chapter 1 Introducing AppleScript 30
Automating with AppleScript: From Small Tasks to Large 30
Automating Small Tasks 30
Automating Large Tasks 31
When to Automate (or Not)? 32
The Golden Triangle: Scriptable Applications, the AppleScript Language, and the Scripts You Write 33
What Is a Scriptable Application? 33
Introducing Objects 34
Introducing Commands 34
What Is the AppleScript Language? 35
What Is a Script? 36
Your First AppleScript: Hello World! 37
Summary 40
Chapter 2 AppleScript in Principle 41
The Four Key Concepts of Application Scripting 42
How Applications Represent Information As Objects 42
How Applications Manipulate Objects Using Commands 44
How Applications Organize Their Objects into Object Models 45
How Applications Describe Their Objects and Commands in Dictionaries 47
The Four Key Concepts of the AppleScript Language 48
How AppleScript Represents Information As Objects 49
How AppleScript Manipulates Objects Using Commands, Operators, and Variables 50
Using Commands 50
Using Operators 50
Using Variables 51
How AppleScript Makes Decisions on What to Do and When to Do It 51
How AppleScript Organizes Code Using Handlers and Script Objects 52
Tying It All Together: What Makes AppleScript Special 54
The English-like Syntax 54
Built-in Application Scripting Support 55
Attaching Scripts to Applications 55
The AppleScript Community 56
Summary 57
Chapter 3 AppleScript in Practice 58
The Project: Tidy Desktop 58
Planning the Script 59
Writing the Script 60
Creating the Folder Name 60
Getting Today’s Date As Text 60
Putting the Name Together 63
Interacting with the File System 64
Addressing the Finder 65
Creating the Archive Folder 67
Moving the Files 70
Final Thoughts 71
Improving Reliability 71
Adding Features 72
Documenting Your Code 73
Summary 74
Part II Understanding AppleScript 75
Chapter 4 Writing Scripts in AppleScript Editor 76
Working with Script Windows 76
Compiling a Script 77
Running and Stopping a Script 78
Recording a Script 78
Viewing the Result of Running a Script 79
Viewing the Events Sent by a Running Script 79
Adding a Description to a Script 80
Viewing Application Dictionaries 80
Other Useful AppleScript Editor Features 81
Using the Library Window 82
Navigating to a Handler in a Script 83
Viewing the Event Log History 84
Scripting the AppleScript Editor 84
Using Context Menu Scripts 85
Viewing the Bundle Contents Drawer 85
Setting the Script Formatting Preferences 86
Saving Scripts 88
Text-Based Scripts 89
Compiled Scripts 89
Saving Scripts As Run Only 90
Saving Scripts As Bundles 91
Script-Based Applications (Applets) 92
Droplets 93
Stay-Open Applets 94
Saving Applets As Run Only 94
Customizing the Startup Screen 94
Other Tools for Writing and Running AppleScripts 95
The System-wide Script Menu 95
Where Are the Menu Scripts Stored? 96
Adding Your Own Scripts 97
Third-Party AppleScript Editors 97
Smile 97
Script Debugger 98
Developing GUI Applications 99
Command-Line Tools 99
Summary 99
Chapter 5 Understanding How Application Scripting Works 100
A Quick Application Scripting Example 101
Understanding Objects 102
How Objects Are Categorized by Class 102
Introducing Object Properties 104
Introducing Object Elements 107
Understanding Commands 109
Introducing Command Names 110
Introducing Command Parameters 110
Commands Must Be Targeted at Objects 112
Commands Can Return Results or Report Errors 113
Understanding the Application Object Model 114
How Objects Can Contain Other Objects 115
How AppleScript Refers to Application Objects 116
Exploring a Typical Object Model 121
Understanding Application Dictionaries 126
Introducing AppleScript Editor’s Dictionary Viewer 127
How Classes Are Documented 130
Viewing the Properties and Elements Defined by a Class 131
How Inheritance Describes Features Shared Between Classes 132
Class Definitions Can Be Spread Across More than One Suite! 137
Browsing the Containment Hierarchy 138
How Commands Are Documented 141
Application Dictionaries Don’t Tell You Everything! 143
More on application Objects 146
Creating application Objects 146
Identifying Applications by Name 147
Identifying Applications by Path 147
Identifying Applications by Bundle ID 147
Identifying Applications by Remote URL 148
The Standard Properties of application Objects 149
How AppleScript Compiles tell application ... Blocks 150
More on Constructing References 151
Referring to Properties 152
Referring to Elements 152
Identifying All Elements 153
Identifying an Element by Its Index, or Position 153
Identifying an Element by Its Name 155
Identifying an Element by Its Unique ID 156
Identifying an Element Before or After Another Element 157
Identifying a Range of Elements 158
Identifying Elements Using the whose Clause 159
Referring to Insertion Locations 161
Summary 162
Chapter 6 Learning to Work with AppleScript Objects 165
How AppleScript Represents Information As Objects 166
What Kinds of Objects Does AppleScript Provide? 166
Creating New Objects 167
Getting Information from Objects 167
How AppleScript Works with Objects 168
Manipulating Objects with Commands 168
Introducing the Five Built-in Commands 168
Introducing Scripting Addition Commands 169
Introducing User-Defined Commands 170
Manipulating Objects with Operators 170
What Makes Operators Special? 171
What Kinds of Operators Does AppleScript Provide? 172
The Concatenation Operator 172
Math Operators 172
Comparison Operators 173
Containment Operators 173
Boolean Operators 174
The Coercion Operator 174
The a reference to Operator 175
Coercing Objects 175
Storing Objects in Variables 176
How Are Identifiers Written? 177
Where Can Variables Be Used? 178
Working with Boolean Objects 179
Boolean Operators 180
The not Operator 180
The and Operator 181
The or Operator 181
How Boolean Operators Are Evaluated 182
Boolean-Related Coercions 182
Summary 183
Chapter 7 Working with Text 184
Introducing Text 184
A Brief History of Text 185
When Is Text Not Text? 185
Understanding Character Sets 186
The Problem with Character Sets 187
Unicode to the Rescue! 190
Introducing Text in AppleScript 2.0 190
How to Write Literal Strings in AppleScript 191
The Properties of Text 195
The class Property 195
The length Property 195
The quoted form Property 195
The id Property 195
The Elements of Text 196
Characters 196
Words 199
Paragraphs 200
Text Ranges 200
Operators and Commands 202
Joining Strings 202
Comparing Strings 203
Checking for Strings Within Strings 205
Considering and Ignoring Attributes 206
The count Command 210
The offset Command 210
Coercing to and from Text 211
Working with Text Item Delimiters 213
Splitting Strings with Text Item Delimiters 213
Combining List Items into a Single String 215
Finding and Replacing Text 217
How AppleScript Text Works in Tiger 218
The Many Different Classes of Text 219
Working with the Various Text Classes 219
The ASCII number and ASCII character Commands 221
Example Projects 222
Defining a Find-and-Replace Command 223
Transferring Meeting Arrangements from Mail to iCal 225
Getting the Message from Mail 226
Parsing the Message Text 229
Creating the Event in iCal 232
Summary 234
Chapter 8 Working with Numbers 236
Introducing Integers and Reals 236
Operators and Commands 237
Math Operations 238
Adding, Subtracting, Multiplying, and Dividing Numbers 238
Negating Numbers 240
Calculating the Integral and Remainder 240
Using the Exponent Operator 241
Comparison Operations 242
Comparing Reals Can Be Trickier Than You Think 243
Performing Multiple Comparisons 244
Introducing Operator Precedence 245
The round Command 246
Rounding Up 247
Rounding Down 247
Rounding Toward Zero 247
Rounding to Nearest 248
Rounding As Taught in School 248
The random number Command 248
Random Number Result 249
Parameters 249
Coercing to and from Numbers 250
Example Projects 252
Defining a Round-to-Decimal-Places Command 252
Blackjack! 254
Kitchen Timer 258
Summary 261
Chapter 9 Working with Dates 262
Introducing Dates 262
Understanding Date and Time Formats 263
Forming Dates On-the-Fly 265
Specifying a Time Relative to a Date 267
The Properties of Dates 268
The class Property 268
The year Property 268
The month Property 268
The day Property 269
The weekday Property 269
The hours Property 270
The minutes Property 270
The seconds Property 270
The time Property 270
The date string Property 271
The short date string Property 271
The time string Property 271
Setting Date Properties 271
Operators and Commands 273
Comparison Operators 273
Math Operators 275
Some Useful Constants 275
Calculating Time Differences 276
The current date Command 278
The time to GMT Command 279
Example Projects 279
Deleting Old Files 280
Creating the Script 280
Adding the User Interaction Portion 282
Alarm Clock 284
Summary 287
Chapter 10 Working with Lists and Records 288
Introducing Lists and Records 288
What Is a List? 288
What Is a Record? 289
How Are Lists and Records Used in Scripts? 290
Working with Lists 290
The Properties of a List 291
The class Property 291
The length Property 291
The rest Property 291
The reverse Property 292
The Elements of a List 292
Getting Items 292
Setting Items 294
Adding Items 294
“Removing” Items 294
Operators and Commands 295
Concatenating Lists 295
Comparing Lists 296
Checking for Sublists 296
The starts with Operator 296
The ends with Operator 297
The contains Operator 297
The is in Operator 298
More Uses for Containment Operators 298
Counting Lists 299
Coercing to and from Lists 299
Processing List Items with a Repeat Loop 300
Sorting Items in a List 302
Working with Records 303
Getting and Setting the Properties of a Record 305
Operators and Commands 307
Concatenating Records 307
Comparing Records 307
Checking for Subrecords 307
Coercing from Records 307
Counting Records 308
Example Project: Measuring Word Frequency in TextEdit 308
Planning the Script 308
Developing the Script 309
Conclusion 312
Summary 313
Chapter 11 Storing Objects in Variables 314
Creating Variables 314
Declaring Your Variables 315
Choosing Your Variable Names 316
When Keywords and Identifiers Conflict 316
Tips for Avoiding Problems with Variable Names 317
Follow Good Naming Practices 318
Break All the Rules! 320
Working with Local Variables 320
Understanding Local Variable Scope 321
Passing Values to and from User-Defined Handlers 324
Working with Properties and Global Variables 326
Understanding Property and Global Variable Scope 326
How Properties (and Global Variables) Can Retain Values Between Runs 327
When to Use Properties and Global Variables 328
Managing User Preferences 328
Enabling Debugging Code 330
Clarifying the Meaning of Important Hard-Coded Values 330
Importing Script Libraries 331
Using AppleScript’s Built-in Variables 331
Variables Containing Predefined Values 332
The return, linefeed, space, tab, and quote Variables 332
The weeks, days, hours, and minutes Variables 333
The pi Variable 334
Variables Containing Objects of Special Interest 334
The it Variable 334
The me Variable 335
The result Variable 338
Summary 339
Chapter 12 More on Commands 340
Understanding How and Where Commands Are Defined 340
Application-Defined Commands 340
Scripting Addition Commands 343
AppleScript Commands 344
User-Defined Commands 345
Picking the Right Target for Your Commands 346
The Five Standard AppleScript Commands 348
The get Command 348
The set Command 348
The copy Command 349
The count Command 351
The run Command 351
Commands That All Applications Understand 352
The run Command 352
The launch Command 353
The open Command 353
The activate Command 354
The quit Command 354
The Standard Suite of Application Commands 355
The get Command 355
The set Command 356
The make Command 357
The exists Command 359
The count Command 359
The move Command 360
The duplicate Command 361
The delete Command 362
The print Command 363
The save Command 364
The close Command 365
More on Working with get, set, and copy Commands 366
When Do Implicit get Commands Occur? 366
When Are Explicit get Commands Necessary? 368
Telling the Difference Between AppleScript and Application set Commands 371
Getting and Setting Multiple Values in a Single Statement 372
Using copy As an Application Command 373
Changing How Commands Are Handled 375
Considering and Ignoring Application Responses 375
Considering and Ignoring Other Attributes 378
Controlling the Timeout Delay for Long-Running Commands 378
Sending Commands to Remote Applications 380
Enabling Remote Apple Events 380
Talking to Remote Machines 382
Authenticating Access to Remote Machines 383
Compiling Scripts Offline 384
Using the choose remote application Command 385
Targeting Remote Applications by User and Process IDs 385
Launching Remote Applications 387
Summary 387
Chapter 13 More on Operators and Coercions 389
Understanding the a reference to Operator 389
Introducing Unit Type Conversions 393
A Summary of AppleScript Coercions 394
Coercing Class and Constant Names 395
Coercing Numbers and Text 396
Coercing File Objects 397
Coercing Lists 398
A Summary of AppleScript Operators 399
Boolean Logic Operators 399
The Concatenation Operator 399
Math Operators 400
Comparison Operators 401
Containment Operators 402
The Coercion Operator 403
The a reference to Operator 403
Operator Precedence in Detail 404
Understanding Precedence 405
Understanding Forms of Association 406
Understanding when You Should Use Parentheses 407
Summary 408
Chapter 14 Making Decisions Using Conditionals and Loops 409
Choosing Your Course with Conditional Blocks 409
Understanding the Basic Conditional Statement 410
The Different Flavors of Conditional Statements 411
Offering an Alternative Ending with the else Clause 411
Offering Multiple Choices with else if Clauses 412
Tips for Writing Conditional Statements 413
Using a Condition in a Single Line 413
Avoiding Unneeded Conditional Statements 413
Is It True? 413
Testing a Value Against Multiple Options 414
Running an Assembly Line with Repeat Loops 414
The Different Flavors of Repeat Loops 415
Repeating Forever 416
Repeating n Times 417
Repeating with Numbers in a Range 417
Repeating with Items in a List 420
Repeating While/Until a Condition Is Met 424
Tips for Writing Repeat Statements 426
Using Loops to Avoid Repetition 426
Naming Loop Variables 428
Choosing the Right Loop for the Job 428
Avoid Looping when Possible 429
Summary 430
Chapter 15 Making Decisions When Dealing with Errors 432
Understanding Compilation Errors 433
Simple Typos 433
Unbalanced Statements 433
Unbalanced Parentheses and Unescaped Quotes 433
Unrecognized Keywords 434
Reserved Word Mishaps 434
Invalid Date Literals 435
Understanding Runtime Errors 435
How Runtime Errors Work in AppleScript 435
Understanding the Psychology of an Error 438
Understanding the Anatomy of an Error 438
Trapping Runtime Errors 439
Using the try Block 439
Using the Full try Statement 440
Knowing What to Do in Case of an Error 440
Putting Error Numbers to Use 443
Testing For Multiple Error Numbers 443
Nesting try Handlers 444
Generating Runtime Errors 444
Tips for Handling Runtime Errors 447
Being Careful Not to Trap Too Much 447
Providing More Detailed Error Messages 447
Using a Scriptwide try Statement 448
Logging Errors to a File 449
Understanding Common Error Numbers 450
Identifying Operating System Errors 450
Identifying Apple Event Errors 451
Identifying Application Scripting Errors 452
Identifying AppleScript Language Errors 453
Summary 454
Chapter 16 Interacting with the User 455
Creating Basic Dialog Boxes and Gathering Text Input 455
Introducing the display dialog Command 455
From the Dictionary: display dialog 455
From the Dictionary: dialog reply 456
Using the Basic Form of the Command 456
Dealing with the Erroneous Cancel Button 457
Creating Custom Buttons 458
Specifying a Default Button 458
Specifying a Cancel Button 459
Adding a Title 460
Showing Icons in Dialog Boxes 460
Using an Icon File in a Dialog Box 461
Getting Text Input from the User 462
Creating Password Dialog Boxes 463
Dismissing Dialogs Automatically 463
Validating User-Entered Text 464
Introducing the display alert Command 465
From the Dictionary: display alert 465
Using the Basic Form of the Command 466
Using the message Parameter 466
Using the as Parameter 467
Setting the Button Behavior 467
Introducing the choose from list Command 467
From the Dictionary: choose from list 468
Getting the Results of the Command 468
Using the Basic Command 468
Creating a Custom Prompt 469
Adding a Title 470
Setting the Default Selection 470
Restricting the Selection 471
Customizing Buttons 472
Choosing Files, Folders, and Disks 472
Introducing the choose file Command 472
From the Dictionary: choose file 473
Getting the Results of the Command 473
Figuring Out When to Use the Command 473
Using the Basic Command 474
Creating a Custom Prompt 474
Restricting to Specific File Types 475
Setting the Default Location 476
Picking Invisibles 476
Allowing Multiple Selections 477
Showing Package Contents 477
Introducing the choose file name Command 478
From the Dictionary: choose file name 478
choose file name vs. choose file 478
Figuring Out When to Use the Command 478
Getting the Results of the Command 478
Using the Basic Command 479
Creating a Custom Prompt 479
Setting a Default Name and Location 479
Replacing an Existing File 480
Seeing an Example in Action 480
Introducing the choose folder Command 481
From the Dictionary: choose folder 481
Using the Command 481
Figuring Out When to Use the Command 482
Setting the Parameters 482
Choosing a Folder Once 482
Choosing Other Types of Items 483
Introducing the choose application Command 483
From the Dictionary: choose application 483
Using the Command 484
Introducing the choose remote application Command 484
From the Dictionary: choose remote application 485
Using the Command 485
Introducing the choose URL Command 485
From the Dictionary: choose URL 485
Using the Command 486
Introducing the choose color Command 486
From the Dictionary: choose color 487
Getting the Results of the Command 487
Figuring Out When to Use the Command 487
Using the Basic Command and Using It with Parameters 487
Summary 488
Chapter 17 Working with Files 489
Identifying Files, Folders, and Disks 489
Using Path Strings 490
Let’s start with an example of an HFS path: Understanding HFS Paths 490
Understanding POSIX Paths 491
Using File System Objects 492
Understanding Alias Objects 492
How Aliases Work 493
Using Aliases to Keep Track of File System Items 494
Converting to and from Alias Objects 495
Understanding File “Objects” 496
How Files (Really) Work 496
Converting to and from File References 498
Understanding POSIX File Objects 498
How POSIX Files Work 498
Converting to and from POSIX File Objects 499
File URL Strings 500
Reading and Writing Files 501
Commands for Reading and Writing Files 501
Reading Files 501
From the Dictionary 501
Using the read Command 502
Opening and Closing Access to Files 502
From the Dictionary 502
Opening a File for Reading or Writing 502
The open for access Result 503
Read Command Parameters 503
Reading Different Kinds of Data 503
Using a Delimiter to Read Text into a List 504
Reading a Specific Number of Bytes 505
The from and to Parameters 505
The before and until Parameters 506
Using the open for access and read Commands Together 506
Working with the End-of-File (EOF) Commands 507
From the Dictionary 508
Getting a File’s EOF 508
Setting a File’s EOF 508
Writing Files 509
From the Dictionary 509
Using the write Command 509
How Much to Write and Where to Start 510
Useful File Writing Handlers 511
Using the write Command to Create a Script Log 511
Saving and Loading AppleScript Lists and Records 513
Dealing with Text File Encodings 513
About ASCII, MacRoman, and Unicode Character Sets 513
Reading and Writing Text Files in Different Encodings 514
Reading and Writing UTF-8 Encoded Files 515
Reading and Writing UTF-16 Encoded Files 515
Reading and Writing Files in Other Encodings 517
What Happens if the Wrong Encoding is Used? 518
A Summary of Common Text File Encodings 520
Further Reading on Text Encodings 520
Summary 520
Chapter 18 Organizing Your Code with Handlers 522
What Are User-Defined Handlers? 522
Creating a Simple Handler 524
Using Parameters 525
What’s the Result? 526
Using the return Statement 526
Working with Return Values 527
Can a Handler Return More Than One Result? 528
Specifying Handler Parameters 529
Using Positional Parameters 530
Definition for Positional Parameter Handlers 530
Defining and Calling Positional Parameter Handlers 530
Adding Parameters 531
Using Labeled Parameters 532
Definition for Positional Parameter Handlers 532
Using Predefined Parameter Labels 532
Making Up Your Own Labels 534
Calling Handlers with Boolean Parameters 534
Introducing Recursion 536
Using the run Handler in Scripts 537
I’ve Never Seen No run Handler! 537
When Should You Make the run Handler Explicit? 538
Working with Handlers in Applets 539
Using the Standard Event Handlers 539
The run Event Handler 540
The open Event Handler 541
The reopen Event Handler 541
The quit Event Handler 541
The idle Event Handler 542
Using User-Defined Handlers from Other Scripts 543
Tips for Designing and Using Handlers 543
Organizing Code 543
Reusing Code 544
Thinking Ahead 544
Thinking Small 544
Reorganizing Existing Code 545
Example Project: A Reusable Associative List 546
Designing the Associative List 547
Writing the Essential Handlers 548
Writing the Extra Handlers 551
Revising the Design 552
Conclusion 554
Summary 555
Chapter 19 Organizing Your Code with Script Objects 557
Introducing Script Objects 557
What Is a Script Library? 558
What Is Object-Oriented Programming? 559
Understanding Script Objects 559
Defining a Script Object in a Script 560
Loading and Storing Script Objects from Disk 561
How Variable Scope Works in Script Objects 562
Working with Script Libraries 564
What Advantages Do Script Libraries Provide? 565
Creating Your First Script Library 566
Where Should You Save Your Library? 566
What Are the Options for Loading Script Libraries? 568
Using the load script Command 568
Using AppleMods Loader 569
Beginning Object-Oriented Programming 572
Your First Object: A Better Associative List 573
Planning the Conversion 574
Converting the make_associative_list Handler 575
Repackaging the Remaining Handlers 576
Using the Object-Oriented Associative List 578
Extending Objects Through Inheritance 579
A Quick Reminder of General Inheritance Concepts 579
How Inheritance Works with Script Objects 581
Implementing Case-Insensitive and Case-Sensitive Associative Lists 585
Testing the New Associative List Objects 587
Creating Plug-and-Play Objects 589
Planning the Logging System 591
Designing the LogBase Object 591
Defining the Concrete Log Objects 594
Testing the Finished Logging System 597
Conclusion 599
Summary 599
Part III Putting AppleScript to Work 601
Chapter 20 Scripting the File System 602
How the Finder Represents the Mac OS X File System 602
Understanding Containers 604
Understanding Files 607
Understanding Packages 609
Understanding the computer-object Class 609
How System Events Represents the Mac OS X File System 610
Understanding Files 612
Understanding Folders 613
Understanding Disks 614
Understanding System Events’ alias Class 614
Other Classes of Interest 615
The user Class 615
The domain Class 615
The login item Class 617
Working with Finder Items 617
Opening and Closing Items 617
Duplicating Files and Folders 618
Deleting Files and Folders 619
Moving Files and Folders 620
Checking if Items Exist 621
Making New Items 621
Sorting Lists of Finder References 622
Ejecting Disks 623
Selecting Items in a Finder Window 623
Converting Finder References to AppleScript Aliases 623
More on Working with Folders 625
Filtering the Content of Folders 625
Getting the Entire Contents of a Folder 626
Locating Important Files and Folders with Standard Additions 627
Avoiding Hardcoded Paths to the Startup Disk and Home Folder 628
Getting the Path to the Currently Running Script 629
Getting Paths to Resources in an Application Bundle 631
Mounting Volumes 633
Summary 636
Chapter 21 Scripting Apple Applications 637
Scripting iTunes 637
Understanding the iTunes Object Model 638
Working with Sources 640
Working with Tracks and Playlists 641
Example Project: My Themed Playlist 645
Gathering the User Input 645
Preparing the Playlist 647
Adding the Tracks to the Playlist 648
Scripting Mail 649
Working with Mailboxes 650
Working with Messages 650
Creating Outgoing Messages 655
Working with Mail Rules 656
Example Project: Building a Monthly Message Archive 660
Preparing to Write the Script 660
Creating the Sub-Mailboxes 662
Putting It All Together 663
Scripting iCal 664
Working with Calendars 666
Working with Events 667
Scripting Address Book 669
Working with People 670
Working with Groups 673
Working with Rollovers 675
Example Project: Looking Up Contact Information by Name 677
Scripting Automator 680
Using the Run AppleScript Action 680
Understanding the Action’s Input and Output 681
Understanding the Action’s Parameters 683
Filtering Files with Run AppleScript 684
Scripting the Automator Application 685
Summary 687
Chapter 22 Extending AppleScript with Scripting Additions 689
Introducing Scripting Additions and Scriptable Faceless Background Applications 689
Scripting Additions in Detail 691
Scripting Additions and the AppleScript Language 691
Installing Scripting Additions 692
Missing Additions and Garbled Scripts 693
Distributing Scripting Additions with Your Scripts 694
Embedding Scripting Additions Within a Script Application 695
Using the Mac OS X Installer to Install Your Script and Scripting Additions 697
Understanding Scripting Addition Compatibility Issues 697
Caution: Mac OS 9 Scripting Additions Don’t Work on Mac OS X 698
Using PowerPC-Only Scripting Additions on Intel-Based Macs 698
Using 32-Bit-Only Scripting Additions on 64-Bit Macs 700
Working Around Scripting Addition Limitations 700
Understanding Scriptable Faceless Background Applications 702
Installing Scriptable FBAs 702
Distributing Scriptable FBAs with Your Scripts 702
Examples of AppleScript Extensions 703
Built-in Extensions 703
Third-Party Extensions 703
Using the Standard Additions Scripting Addition 704
Providing Audio Feedback 705
The beep Command 705
The say Command 705
Pausing Your Scripts 706
The delay Command 706
Getting and Setting the System Volume 706
The get volume settings Command 706
The set volume Command 706
Getting System Information 707
The system attribute Command 707
The system info Command 708
Working with URLs 708
The open location Command 709
Coercing URLs 709
Using the Image Events Faceless Background Application 709
Processing Text with the Satimage ScriptingAddition 711
Downloading and Installing the Satimage Scripting Addition 712
Performing Simple Find and Replace Tasks 712
Counting Occurrences of a Substring in a String 713
Finding and Replacing Substrings in a File 714
Searching and Replacing Across Many Files 715
Finding and Replacing Text with Regular Expressions 715
A Brief Background on Regular Expressions 715
Using Satimage Regular Expression Commands 719
Batch-Changing File Names 721
Summary 724
Chapter 23 AppleScript Amenities 725
Using the Services Menu 725
AppleScript Editor Services 726
Creating Your Own Services 726
Scheduling Scripts with iCal 729
Triggering Scripts with Folder Actions 730
Defining Event Handlers for Folder Actions 731
Where Are Folder Action Scripts Stored? 731
Managing Your Folder Actions 731
Using the Folder Actions Setup Utility 732
Using Scripts 732
Creating Your First Folder Action 733
Create Your Folder Action Script 733
Save Your Script 734
Create Your Hot Folder 734
Activate the Folder Actions Setup Utility 734
Attach the Folder Action Script to the Folder 734
Test Your Folder Action 736
Controlling Applications with GUI Scripting 736
Enabling GUI Scripting 737
The GUI Scripting Dictionary 737
GUI Scripting Object Structure 737
Basic GUI Scripting Examples 739
Using GUI Scripting Commands 740
The click Command 740
The keystroke Command 740
The key code Command 741
Example Project: Searching in Preview 742
Introducing PreFab UI Browser 742
Beginning the Script 743
Performing the Search 744
Getting Data from the Search Table 747
Understanding the Limitations of GUI Scripting 749
Working with the Clipboard 750
Getting Clipboard Data into AppleScript 750
Understanding How the Mac Clipboard Works 750
Finding Out What’s on the Clipboard 751
Getting Specific Types of Data from the Clipboard 751
Setting the Clipboard Data 751
Using the cut, copy, and paste Commands in Applications 752
Using the set the clipboard to Command 753
Using GUI Scripting 753
Example Project: Creating PDF Cuttings from Selections in Preview 753
Summary 756
Chapter 24 Scripting iWork and Office 757
Scripting iWork 757
Scripting Pages 758
Creating a New Document 758
Working with a Document’s Paragraphs 759
Adding Headers and Footers to a Document 761
Drawing Shapes in Pages 762
Adding Tables and Charts 763
Tips for Scripting Pages 764
Scripting Keynote 765
Creating a Slideshow 765
Choosing a Theme 766
Filling Slides with Content 767
Working with Transitions 769
Scripting an Advanced Screen Reader 771
Scripting Numbers 772
Creating Sheets and Tables 773
Working with Cells 773
Scripting Microsoft Office 774
Working with Microsoft Entourage 775
Disable the Security Warning 776
Creating Messages 777
Some Shortcuts 779
Working with Contacts 780
Creating Contacts 781
Selecting and Changing Multiple Contacts 782
Setting Categories 783
Creating Calendar Events 784
Extracting Information from a Whole Lot of Messages in a Folder 787
Working with Excel 791
Creating Workbooks and Worksheets 792
Working with Ranges 793
Creating Charts 800
Working with Word 802
Creating a Word Document 802
Working with Selected Text 803
Where Next? 805
Summary 806
Chapter 25 Scripting Data and Databases 807
Automating FileMaker Pro with AppleScript 808
The FileMaker Object Model and the Commands You Can Use 809
The database Class 810
The window Class 811
The table Class 811
The layout Class 812
The record, field, and cell Classes 813
Finding Data Quickly with the whose Clause 816
Using the whose Clause to Retrieve Relational Data 817
Using the find Command 818
Running AppleScripts from Inside FileMaker Pro 820
Scripting Runtime Labs’ MacSQL 821
Getting Connected 822
Using a Simple select Command 823
More on Result Sets 823
Some Useful Handlers 824
Database Events 826
Database Events Classes and Commands 826
Database Events Examples 827
Summary 828
Chapter 26 Scripting Adobe InDesign 830
Learning by Example 831
Understanding the InDesign Object Model 833
How InDesign Organizes Its Many Properties 833
How InDesign Manages Units of Measurement 836
How to Set Properties Safely when Making New Objects 837
Working with Documents 838
Working with Document Layers 842
Understanding InDesign References 844
Working with Text 844
Working with Text Stories 845
Working with References to Text 846
Working with Graphics 848
Drawing Lines 849
Example Project: An InDesign Scripts Launcher 850
Working with Tables 853
Importing Table Text from a Spreadsheet 854
Copying Formatting Between Cells 856
Working with Selections 858
Example Project: Captioning a Selected Graphic 859
Example Project: Cleaning Up Selected Text 861
Labeling Objects for Use in Workflow Scripts 863
Taking Advantage of AppleScript-Specific Preferences 865
Example Project: Replacing Tags with Images 867
Preparing InDesign 868
Processing the Tags 869
Preparing the Image Files 870
Placing the Graphics 871
Testing the Script 873
Using InDesign on a Server 873
Summary 874
Chapter 27 Interacting with the Unix Command Line 875
Why and When Are Shell Scripts Used in AppleScript? 875
Understanding Unix Scripting Concepts 876
The Unix Command Line and Mac Desktop Compared 876
How the Unix Command Line Works 877
The Shell 878
Commands 879
Pipes 880
Other Redirections 880
Getting Help 881
Built-in Help 881
Other Sources of Help 882
Things to Watch Out For 882
Data in Unix Is “Dumb” 882
Unix Doesn’t Know About Different Character Sets 883
Unix Understands POSIX File Paths Only 883
Watch Where You Use Spaces and Other Special Characters! 883
Unix Assumes You Know What You’re Doing 884
Some Useful Unix Commands 884
Running Shell Scripts from AppleScript 885
Scripting the Terminal Application 885
Understanding Terminal’s Object Model 885
Using the do script Command 886
A Simple do script Example 886
Limitations of the do script Command 887
Using the do shell script Command 887
From the Dictionary 887
Understanding the do shell script Command’s Parameters 888
The Direct Parameter 888
The as Parameter 888
The administrator privileges Parameter 888
The user name and password Parameters 888
The altering line endings Parameter 888
How do shell script Deals with Unicode 889
A Simple do shell script Example 890
Limitations of the do shell script Command 890
No User Interaction 890
Performance Overheads 891
Can’t Directly Supply Data to Standard Input 891
Assembling Shell Script Strings 891
Unix File Paths 891
Quoting Arguments 892
Absolute Paths, Relative Paths, and the Working Directory 893
Passing Data to Standard Input 895
Using echo 895
Using Temporary Files 896
Creating the Temporary File 896
Writing the Temporary File 897
Redirecting the Shell Script 897
Running AppleScript from Shell Scripts 898
Using the osascript Command 898
Passing Parameters to the Script 899
Other Related Commands 900
Example Projects 900
Simple Text Processing Handlers 900
Encoding URLs 901
Changing Case 901
Changing File System Permissions 902
Analyzing PDF Files 905
Summary 908
Chapter 28 Using Smile: The AppleScript Integrated Production Environment 909
Introducing Smile 909
Introducing Smile’s Integrated Engine Architecture 909
About Smile’s Terminal 910
Why Should You Use Smile? 910
Downloading Smile 911
Using Smile 911
Using the AppleScript Command-Line Environment 912
Using Smile’s Custom Dialog Boxes 913
Creating the Interface 913
Programming the Functionality 914
Using Regular Expressions 915
Using the Graphic Engine 916
Using SmileLab 917
Example Project: Adding Text to a PDF Document 920
Preparing the PDF 920
Rolling Up the Scripts into a Graphical Interface 927
Building the Interface 928
Programming the Dialog Box 931
Handling User Clicks 934
Further Exercises 937
Summary 938
Chapter 29 Tips and Techniques for Improving Your Scripts 939
Design, in a Nutshell 940
Taking Your Scripting Skills to the Next Level 940
How Much Design Do Your Scripts Need? 941
Starting Out Is Hard (But Gets Easier with Practice) 943
How the Design-Driven Approach Changes the Way You Think About Code 945
Testing and Debugging Your Code 946
General Testing Tips and Techniques 947
Design Your Scripts for Easy Testing 947
Test Your Code with Bad Data As Well As Good 947
Test Your Code As You Go 948
Why Testing Matters 949
Debugging in AppleScript Editor 950
Viewing Intermediate Results with return and error Statements 950
Monitoring a Script’s Progress Using Dialog Boxes 951
Using the Event Log 951
Debugging in Script Debugger 953
Using the start log and stop log Commands 953
Using the Script Window 954
Using the Debugging Mode 954
Stepping Through Scripts 955
Keeping Track of Changes 956
Comparing Scripts with FileMerge 956
Managing Your Code with Source Control 959
Improving the Performance of Your Code 960
The Dos and Don’ts of Optimization 960
When Should You Optimize? 961
Where Should You Optimize? 961
Profiling Your Code with Timing Commands 962
Common AppleScript Optimizations 963
Building Up Large Lists 963
Building Up Large Strings 964
Manipulating Multiple Application Objects with a Single Command 966
Filtering Multiple Application Objects with whose Clause References 967
Advanced Optimization Techniques 968
Assessing Your Code’s Efficiency with Big O Notation 969
Improving the Efficiency of AppleScript Lists 971
Linear Search vs. Binary Search 974
Bubble Sort vs. Quicksort 976
Summary 978
Chapter 30 Creating Cocoa Applications with AppleScriptObjC 980
Example Project: HelloAppleScriptObjC 981
Creating the Project in Xcode 982
Adding the AppleScript Code in Xcode 984
Constructing the Interface in Interface Builder 984
Understanding Interface Builder’s Interface 984
The MainMenu Window 985
The Application Window 985
The MainMenu.xib – English Window 986
The Library Palette 986
Adding the GUI Objects 987
Making Connections 989
Building and Testing the Application 990
Understanding How Cocoa Works 991
Understanding Outlets 991
Understanding Targets and Actions 991
How AppleScript Interacts with Cocoa Objects 993
Modifying HelloAppleScriptObjC to Set the Text Field 994
Updating the Code and Testing the Changes 994
What Is All That Code in the Default AppleScript File? 994
Example Project: PlaySound 996
Creating the Project and Adding the Code 996
Constructing the Interface 997
Building and Testing the Application 998
Understanding How It Works 999
Viewing the Cocoa Documentation in Xcode 1001
How Cocoa Classes Are Documented 1002
How Cocoa Methods Are Documented 1004
The Method Name 1005
Parameters 1006
Return Value 1006
Discussion, Availability, See Also, Related Sample Code, and Declared In 1006
Example Project: SnapShot 1007
Creating the Project and Starting the Code 1007
Constructing the Interface 1007
Completing the Code 1008
Understanding How It Works 1010
Preparing the Text Fields Using an awakeFromNib Handler 1011
Building and Testing the Application 1012
Modifying SnapShot to Use Cocoa Bindings 1012
Adjusting the Code for Bindings 1012
Setting Up Bindings in Interface Builder 1013
Building and Testing the Application 1017
Modifying PlaySound to Use Bundled Sound Files 1017
Adding the Sound File Resources to the Project 1017
Adding the Code 1018
Adding the Pop-Up Button 1018
Building and Testing the Application 1018
Understanding How It Works 1019
Example Project: KitchenTimer 1020
Creating the Project and Starting the Code 1020
Constructing the Interface 1021
Completing the Code 1024
Extra Exercises 1026
Where Next? 1027
Summary 1028
Conclusion 1029
Index 1030

Erscheint lt. Verlag 10.7.2010
Zusatzinfo 1104 p.
Verlagsort Berkeley
Sprache englisch
Themenwelt Informatik Betriebssysteme / Server Macintosh / Mac OS X
Informatik Programmiersprachen / -werkzeuge Mac / Cocoa Programmierung
Mathematik / Informatik Informatik Theorie / Studium
Schlagworte AppleScript • CoCoA • Code • Control • Controlling • Database • Debugging • Information • Mac OS X
ISBN-10 1-4302-2362-6 / 1430223626
ISBN-13 978-1-4302-2362-7 / 9781430223627
Haben Sie eine Frage zum Produkt?
PDFPDF (Wasserzeichen)
Größe: 35,3 MB

DRM: Digitales Wasserzeichen
Dieses eBook enthält ein digitales Wasser­zeichen und ist damit für Sie persona­lisiert. Bei einer missbräuch­lichen Weiter­gabe des eBooks an Dritte ist eine Rück­ver­folgung an die Quelle möglich.

Dateiformat: PDF (Portable Document Format)
Mit einem festen Seiten­layout eignet sich die PDF besonders für Fach­bücher mit Spalten, Tabellen und Abbild­ungen. Eine PDF kann auf fast allen Geräten ange­zeigt werden, ist aber für kleine Displays (Smart­phone, eReader) nur einge­schränkt geeignet.

Systemvoraussetzungen:
PC/Mac: Mit einem PC oder Mac können Sie dieses eBook lesen. Sie benötigen dafür einen PDF-Viewer - z.B. den Adobe Reader oder Adobe Digital Editions.
eReader: Dieses eBook kann mit (fast) allen eBook-Readern gelesen werden. Mit dem amazon-Kindle ist es aber nicht kompatibel.
Smartphone/Tablet: Egal ob Apple oder Android, dieses eBook können Sie lesen. Sie benötigen dafür einen PDF-Viewer - z.B. die kostenlose Adobe Digital Editions-App.

Zusätzliches Feature: Online Lesen
Dieses eBook können Sie zusätzlich zum Download auch online im Webbrowser lesen.

Buying eBooks from abroad
For tax law reasons we can sell eBooks just within Germany and Switzerland. Regrettably we cannot fulfill eBook-orders from other countries.

Mehr entdecken
aus dem Bereich