Organization of This BookAppleScript in a Nutshell is structured in six parts. Part IThis section provides an overview of AppleScript and Script Editor, the free AppleScript development tool that installs with the Macintosh. Quick studies and experienced programmers will probably be able to develop their first AppleScripts (if you have never used AppleScript before) based on a reading of this introductory section alone. Chapter 1 describes how AppleScript is primarily used and also describes the relevance to AppleScript of Apple events, an internal messaging system that the Macintosh operating system uses for interapplication communication. The end of Chapter 1 summarizes AppleScript's core language features (Part II provides a more comprehensive language reference). You can use Chapter 2 as a helpful reference to Script Editor as you use this Apple Computer tool to develop your scripts. Chapter 1This AppleScript overview includes a description of how AppleScript is primarily used, an Apple-event tutorial, and a compressed language reference for those who want to dive right into scripting. Novice users should start here with the book, while very experienced AppleScripters may use this section as a review or skip over it. Chapter 2This chapter describes all of Script Editor's primary menu commands and controls. It also explains the various options for saving AppleScript files. Part IIIf scripters need more information on specific language features, this is the place to look. The core-language information is presented with syntax examples, code examples, and text descriptions. Everything is arranged in alphabetical order to make things easy to locate. This includes the various data types (i.e., how AppleScript stores data in memory), operators (such as the common Math operators and the string-concatenation operator "&"), and how to set AppleScript variables and create user-defined functions, as well as advanced features, such as creating object-oriented script objects (Chapter 9). Chapter 3This chapter describes the built-in AppleScript data types, including string, integer, real, list, and record. Comparisons with programming languages are made where it is appropriate (e.g., a list is like an array, and a record is an associative array). Chapter 4Use this chapter as a reference to the built-in symbols (e.g., &, +, *, - ) that you can use in AppleScript expressions. Chapter 5AppleScript provides several English-language terms to use when the script refers to objects on your computer system, such as files, folders, disks, and applications. This chapter is an alphabetical reference to these terms (e.g., first, every, id, where). Chapter 6AppleScript, like other languages, uses variables as placeholders that represent data (e.g., strings or numbers). This chapter describes the rules for naming and creating your own variables; it also provides a reference to AppleScript's constants and predefined variables (like pi). Chapter 7This chapter is an alphabetical reference to AppleScript's flow-control statements, such as if, repeat, try, exit, and continue. Chapter 8This chapter is a tutorial on creating user-defined subroutines, which are also called handlers, functions, or methods (in object-oriented parlance). The second part of this chapter describes five special handlers in AppleScript: idle, open, quit, reopen, and run. Chapter 9AppleScripters can create script objects, which are user-defined types that can have their own attributes and methods. This chapter also describes function libraries, which are script objects that give other external scripts the ability to load and/or call the object's own functions. Part IIIThis section is devoted to the scripting of system-level Mac OS 9 programs, such as Apple System Profiler, Keychain Scripting, the Finder, Network Setup Scripting, and Sherlock 2. The scriptable control panels and extensions are covered in the next section, Part IV. The programs that are covered in this section for the most part have comprehensive AppleScript dictionaries and can be used to extend your computer's capabilities (particularly with AppleScript!); however; they are not control panels or extensions. The exception to this scheme is Apple Guide, which is an extension but was included in this section so that the reader has access in a single chapter to a description of AppleScript and the help-related programs. Each chapter describes the purpose of the application, then describes each dictionary command and class in a reference-style form. Chapter 10This chapter describes the dictionaries and includes scripting tips for Apple Guide, the traditional automated Apple-help program, and the newer browser-based Help Viewer tool. Chapter 11Accessible from the Apple menu, Apple System Profiler displays a wealth of information about the hardware and software on your system. This chapter describes its commands and classes and includes numerous code examples. Chapter 12These are two Apple-security tools. Keychain Scripting is used to encrypt files and passwords, and Apple Verifier can verify digitally-signed files. This chapter tells where to find these applications and describes their commands and classes in reference form. Chapter 13Scripters can use Desktop Printer Manager, a program introduced with Mac OS 8.5, to create and manage desktop icons that can be used for printing or otherwise processing documents and files. This chapter describes the proper syntax for controlling this application with AppleScript and also includes a reference to its dictionary commands and classes. Chapter 14The Finder is the Mac OS 9 application that controls the user's visual interface to the computer: its desktop controls as well as hard disks, network volumes, printers, and other devices. A lot of fun and useful AppleScripts deal with automating Finder activities, such as reading from and writing to files. This chapter covers the Finder commands, like restart, shutdown, sleep, and make, with detailed references to each command and any of their parameters. Chapter 15This chapter covers the Finder classes, which are all the objects or things you are likely to control when scripting the Finder (e.g., files, folders, disks, and running applications). Finder Classes provides a detailed reference to each object's elements (if any) and properties. Chapter 16As the Macintosh becomes a sophisticated client and server on TCP/IP networks, Network Setup Scripting shows how you can use the commands and classes of this program with Open Transport to script a machine's various network configurations. Chapter 17You can automate sophisticated searches of local networks and the Web with AppleScript and Sherlock 2. Scripting Sherlock 2 provides a description of this program and a reference, with code examples, to its commands (e.g., index containers, search) and classes. Chapter 18URL Access Scripting describes the download and upload commands of this program, which can be used with the FTP and HTTP protocols to grab and save files off the Web. Part IVThis section is dedicated to the scripting of the Mac's control panels and extensions, which are located in the Control Panels and Extensions folders of the System Folder. Each chapter describes the purpose of this system software, then includes a reference to their dictionary commands and classes. Some of the more exciting new scriptable technologies are included in this section, including Apple Data Detectors, Folder Actions, and the Speech-related extensions in Chapter 30. Chapter 19This scriptable control panel lets you use AppleScript to set and change the visual and audible aspects of your computer, such as its background color, the font for desktop text, and how window title bars and scroll bars work. We show you how to do this and include a detailed reference to this software's commands and classes. Chapter 20This chapter describes a powerful scripting technology by which you can assign an AppleScript to be triggered based on certain information that a user selects inside of a contextual menu, such as an email or web address. Apple Data Detectors Extension describes the Apple Data Detectors scripting-addition class and commands in reference form. Chapter 21This chapter describes how to use AppleScript to automate various menu items (e.g., Recent applications, documents, and servers) in the Apple menu (the drop-down menu in the upper-left part of the computer screen). Chapter 22The Application Switcher is the floating palette that the user can "tear" off of the Application menu (on the upper-right part of the computer screen). This chapter describes how to set various Switcher elements (e.g., its size, position, button order) with AppleScript and includes a reference to its extensive application class. Chapter 23ColorSync Extension describes the AppleScript commands and classes for this built-in Macintosh software, which helps synchronize color-matching between the devices that create an image (e.g., scanners) and printers. Chapter 24This chapter describes the File Exchange commands that you can use to create new extension mappings (i.e., a way to tell the Macintosh how to handle files with certain extensions like .html ), for instance, or view the existing file-type mappings on a machine. Chapter 25This chapter first summarizes file sharing on the Macintosh, which establishes the level of access network users have to a machine's disks and folders. Then it shows how to create new users or groups (or delete miscreants) with code examples and a reference section on File Sharing's dictionary commands and classes. Chapter 26Folder actions are AppleScripts that are triggered when items are added to or removed from a folder. Folder action commands constitute the Folder Actions suite of the Standard Additions osax and the dictionary commands that derive from the Folder Actions extension. This chapter describes both sets of commands. Chapter 27This chapter describes the dictionaries for the FontSync control panel and extension. They are used to synchronize the fonts between devices during image production and printing. Chapter 28This chapter shows how you can use AppleScript to switch between the various computer and networking configurations that are displayed by the Location Manager control panel. Chapter 29This chapter describes the dictionary commands and classes for both the Memory and Mouse control panels. For example, the chapter shows how you can use an applet to find out about the computer's virtual-memory settings or disk-cache size. Chapter 30This chapter describes the different ways that you can integrate speech into your scripts, such as the listen for and say AppleScript commands. Speech listener is actually an application that is located in the Scripting Additions folder of the System Folder, but it will not work unless the Speech Recognition extension is installed and enabled. Chapter 31This chapter describes the functionality of the Web Sharing control panel and also gives an example of how to use AppleScript with a Common Gateway Interface (CGI) script. CGI scripts execute in response to web page requests, in order to process the incoming data from a form a web user has filled out, for instance. The Web Sharing control panel can be used to allow a computer to perform as a light-weight web server. Part VAppleScript is in a state of flux and evolution on the new Mac OS X system. AppleScript also faces tremendous competition from the programming tools that come with (and can be installed on) Mac OS X, such as shell scripting tools, Perl, and Java. Nevertheless, this section will describe what you can do with AppleScript and three Mac OS X programs that can be used with AppleScript: Mail, Terminal application (a command-line tool), and TextEdit. Part V begins with a discussion of AppleScript and scripting the new Mac OS X Finder, which is the OS 9 Finder after a major facelift. Chapter 32This chapter explains some of the familiar Finder-like scripting that you can accomplish on Mac OS X, such as getting information about desktop items (e.g., files, folders, and disks) and making new files. This chapter compares the Mac OS X Finder dictionary to the Mac OS 9 Finder dictionary (and finds few differences, but that is likely to change with new OS X versions). Chapter 33This chapter describes the use of AppleScript with Apple Computer's new email application, aptly called "Mail." This chapter provides descriptions and code examples on setting up a new mail message and getting information about an email account. Chapter 34Terminal application is the command-line tool or interface (a window or shell that you type script commands into) that comes with Mac OS X. This chapter shows how you can create, compile, and execute AppleScripts from the Terminal program. Chapter 35It is likely that the TextEdit's available AppleScript commands will change with new Mac OS X releases, so this chapter focuses on TextEdit's major commands (e.g., count, open, save) and text-related classes, such as character, document, paragraph, and text. Part VIOur AppleScript book would not be complete without a description and reference information on the many scripting additions or "osaxen" that veteran scripters use in almost every script (remember display dialog or current date ?). Appendix A covers the Standard Additions (a group of scripting additions that Apple Computer bundles with the OS installation) that are installed with both Mac OS 9 and Mac OS X. This section describes each of the Standard Additions (e.g., ASCII number, beep, choose application) and any parameters that these osax commands use. Appendix B, is a list of URLs that are relevant to AppleScript users. Appendix AThis appendix focuses on the several dozen Standard Addition scripting additions, which are installed along with Mac OS 9 and Mac OS X. These are extensions to the built-in AppleScript commands that you can use virtually anywhere in your script (Chapter 1 also discusses scripting additions). The Standard Additions are located in the startup disk:System Folder:Scripting Additions folder in OS 9 and, with Mac OS X, /System/Library/ScriptingAdditions/ (the primary location on OS X). Appendix BThis is an extensive list of web pages relating to Macintosh scripting and AppleScript. |