Appendix A. The AppleScript Experience

This appendix illustrates informally the process of developing AppleScript code. The idea is to convey to the beginner some sense of what it's like to work with AppleScript, and to present some typical stages in the development of an AppleScript-based solution. My approach is to demonstrate by example, letting you look over my shoulder as I tackle a genuine problem in my real life. The procedures and thought processes exemplified here are quite typical of my own approach to writing AppleScript code, and probably that of many other experienced users as well; as such, the neophyte may benefit by witnessing them. Besides, if you've never programmed with AppleScript before, you're probably curious about what you're getting yourself into.

Think of this appendix, then, as a nonprogrammer's introduction to the art of AppleScript development. It's the art that's important here. The particular problem I'll solve in this chapter will probably have no relevance whatsoever to your own life. But the way I approach the problem, the things I do and experience as I work on it, contain useful lessons. At the end we'll extract some general principles on how to approach a task with AppleScript.

I have just completed, working in Adobe FrameMaker , the manuscript for a book about AppleScript. This manuscript is now to be submitted to my publisher. My publisher can take submissions in FrameMaker, which is what the production department uses in-house, and there is a checklist enumerating certain details of the form the manuscript should take. Looking over this checklist, I find an entry from the illustration department informing me that I'm supposed to follow certain rules about the naming of the files that contain the illustrations, and that I'm to submit a list of illustrations providing the number, name, and caption of each figure. Table A-1 presents the example the illustration department provides.

As the example shows, the illustration department would like each illustration file named according to its place in the book. Each name starts with some letters identifying the book, followed by an underscore. Then there are two digits signifying the number of the chapter in which the figure appears. Then there are two more digits signifying the relative position of the figure within the chapter. Finally, there's the suffix indicating what kind of file it is. The illustration department would also like me to attach a list that looks like the table, associating figure numbers, filenames, and captions.

Naturally, as I've been writing the book, I haven't done any of that. It wouldn't have made sense, because I didn't know, as I wrote the book, exactly how many chapters there would be and what order they would be in, and exactly what illustrations there would be and what order they would be in. I've been cutting and pasting and rearranging right up until the last moment. My illustration files simply have whatever names I gave them at the time of creation; these names are generally pretty meaningless, and in the Finder they appear in alphabetical order, which is not at all the order in which they appear in the book. For example, here are the names of the illustration files for Chapter 2, as they appear in the Finder:

automator.eps
automator2.eps
cocoa.eps
fileMaker1.eps
fileMaker2.eps
radio.eps
scriptDebugger.eps
scriptDebuggerDict.eps
scriptEditor.eps
scriptEditorDict.eps

Now, however, the proverbial last moment has arrived. So it's time for me to grapple with the illustration department's requirements. The problem, therefore, is to rename these files in accordance with the chapter in which they appear and their order of appearance within it. Clearly I'm going to have to work in two places at once. In FrameMaker, I need to look at each illustration in order, and see what file on disk it corresponds to. In the Finder, I need to change the name of that file. Then, back in FrameMaker, I need to change the reference for each illustration, so that it points to the correct file under its new name.

This promises to be a massively painful, tedious, and error-prone task—not something I'm looking forward to. Then I get an idea. Adobe FrameMaker is scriptable; in fact, it's extraordinarily scriptable. And so is the Finder. Perhaps this task can be automated using AppleScript.