Apple Pro Training Series: AppleScript 1-2-3
Sal Soghoian and Bill Cheeseman
Apple Training Series: AppleScript 1-2-3
Sal Soghoian and Bill Cheeseman
Published by Peachpit Press. For information on Peachpit Press, contact:
Peachpit Press
1249 Eighth Street
Berkeley, CA 94710
510/524-2178
510/524-2221 (fax)
Find us on the Web at: www.peachpit.com
To report errors, please send a note to
Peachpit Press is a division of Pearson Education
Copyright 2009 by Apple Inc. and Bill Cheeseman
Project editor: Clifford Colby
Production coordinator: Lisa Brazieal
Copyeditor: Kim Wimpsett
Compositor: WolfsonDesign
Indexer: Rebecca Plunkett
Cover design: Mimi Heft
Cover illustration: Mimi Heft; source photo by Derrick Story
Cover photo: Derrick Story
Notice of Rights
All rights reserved. No part of this book may be reproduced or transmitted in any form by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior written permission of the publisher. For information on getting permission for reprints and excerpts, contact .
Notice of Liability
The information in this book is distributed on an As Is basis, without warranty. While every precaution has been taken in the preparation of the book, neither the authors nor Peachpit Press shall have any liability to any person or entity with respect to any loss or damage caused or alleged to be caused directly or indirectly by the instructions contained in this book or by the computer software and hardware products described in it.
Trademarks
Apple and AppleScript are trademarks of Apple Inc. Many of the designations used by manufacturers and sellers to distinguish their products are claimed as trademarks. Where those designations appear in this book, and Peachpit was aware of a trademark claim, the designations appear as requested by the owner of the trademark. All other product names and services identified throughout this book are used in editorial fashion only and for the benefit of such companies with no intention of infringement of the trademark. No such use, or the use of any trade name, is intended to convey endorsement or other affiliation with this book.
ISBN 13: 978-0-321-14931-2
ISBN 10: 0-321-14931-9
9 8 7 6 5 4 3 2 1
Printed and bound in the United States of America
I dedicate this book to my wife, Naomi, and Mosby, without whose support and understanding it would not have been written.
And to my family, Avedis, Laura, Stephen, and Christopher, and the tapestry of relatives who add color to my life.
And finally to the Apple engineers and staff whose vision and dedication to AppleScript have been such a benefit to me and thousands of others. I thank them for their work and their unwavering belief that the power of the computer should rest in the hands of those that use it.
Sal Soghoian
For my wife, Sharon, with love.
Thanks to AppleScripters everywhere for the extraordinary good will and high spirits of our community all these many years.
Bill Cheeseman
Contents at a Glance
Foreword
I think we owe my Aunt Sarah a big thank you. After all, shes the one who inspired the book thats about to change your computer life forever.
It was at one of those big get-togethers that occur frequently in ethnic families like mine. Hundreds of relatives gather, taking over the neighborhood and local markets to play, feast, and talk for days. Kids with parents and elders clustered around an ongoing buffet of Armenian goodness (my stomach still is full to this day), making the joyous racket that is life in motion.
Anyway, I wander into the kitchen, and there is Sarah, standing with her belly pressing against the edge of the sink, her dark hair pulled back into a bun of sorts, sleeves rolled up, elbows deep in a tub of dishes, with pile upon pile of plates on the counter next to her. Moved by her toil, I approach and offer to show her an easier way to clean the days plates, pans, and cups, to which she immediately quips I dont have time to learn how to use the dishwasher, Im busy washing dishes.
At some point in our lives, weve all felt like that. Theres no time to try something new, even if it can help us. We know what we know, learned on the job, and it works for us. It might not be the best or fastest way, but its what we know how to do. And so we labor harder and longer than we need to, missing any opportunity to make things easier for ourselves.
Until one day, something happens. Maybe you pick up a book. This book. And you decide youre finally going to take a chance and spend a little timejust half an hour or soto follow the first chapter, to see whats possible and if its right for you. Thats when magic happens. The magic of AppleScript and its power to make the computer do what you want and need it to do for you.
It Was a Dark and Stormy Night ...
My name is Sal Soghoian, and since January 13, 1997, Ive had the honor to be the product manager for automation technologies at Apple. To paraphrase a famous lawyer, for me, AppleScript is a living breathing reality. AppleScript been a big part of my life since a moment of inspiration in 1992: While working at a service bureau in Charlottesville, Virginia, I wrote my first script and changed the course of my life. It was this simple script for QuarkXPress:
set the color of the current box to "Blue"
And when the color of that selected text box changed from white to blue, I knew nothing would be the same. In an instant, the power to control the computer was no longer just in the hands of companies whose software I purchased: It was in mine as well.
By writing English-like statements, I could automate the many redundant tasks I performed over and over, all day long, at the service bureau. It was liberating! I quit my job, and for the next three months spent every waking hour teaching myself to master this wondrous technology. It took that long because there were no books, no videos, no podcasts, no Web sites to turn to for assistancejust a dedicated group of individuals in online communities who shared a passion to automate and accomplish the impossible.
How some things have changed for the better.
As time went on, my automation skills matured and grew (like most scripters) and people noticed, asking (and paying) me to write scripts for them. And for the next years, I wrote magazine articles, presented at trade shows, and taught individuals and classes how to take advantage of AppleScript and make it an essential part of their computer lives.
About This Book
This book is based on years of hands-on classes and contains many of the scripts developed for those sessions.
But the task of documenting and teaching a technology such as AppleScript is daunting (especially while youre employed at a busy company like Apple), and it could not have been accomplished without the assistance and contribution of an exceptional individual with laser-like focus and deep knowledge of AppleScript: Bill Cheeseman. A lawyer of considerable renown, Bill leads a double-life as a talented programmer who has written extensively on the subject. He also is a master scripter and someone who Ive learned from time and again. It was very fortunate that he made time in his schedule to contribute to this book.