Online Book Reader

Home Category

AppleScript_ The Definitive Guide - Matt Neuburg [52]

By Root 1480 0
to write AppleScript code that bears an extraordinary resemblance to Scheme code. To give an example, here's a little Scheme program that defines a routine for removing the nth element from a list, and then tests the routine:

(define remvix

(lambda (ix ls)

(cond

((null? ls)

'( ))

((= ix 1)

(cdr ls))

(else

(cons (car ls) (remvix (- ix 1) (cdr ls)))))))

(remvix 2 '(mannie moe jack))

And here's the same thing done in just the same style in AppleScript:

on remvix(ix, ls)

if ls is {} then

return {}

else if ix is 1 then

return rest of ls

else

return {item 1 of ls} & remvix(ix - 1, rest of ls)

end if

end remvix

remvix(2, {"Mannie", "Moe", "Jack"})

Even if you don't know any Scheme or any AppleScript, the structural and stylistic similarity of these approaches is unmistakeable; they are in fact move-for-move identical, the only differences between them being matters of syntactic detail. To be sure, I've stacked the deck by deliberately writing the AppleScript routine in a Scheme-like style; but the point is that AppleScript is capable of that style, and invites it.

AppleScript also can generate closures (subroutines that remember their global environment). And there is a sense in which all the components of a script—variables, handlers, and script objects—possess the same first-class status; for example, any of them can be passed as a parameter to, or returned as a result from, a subroutine. All of this has a markedly LISP -like flavor.

The Learning Curve


In this chapter I've argued that AppleScript isn't easy just because it's small or just because it's English-like, and I've mentioned that AppleScript borrows some features from object-oriented languages and LISP that you may never have heard of and whose mere mention may have made your eyes glaze over. So perhaps at this point you're starting to worry that AppleScript is difficult to learn. But that isn't what I mean either! What I'm telling you is that AppleScript is a computer language, like any other computer language. You weren't born knowing it, and you aren't going to be able to use it without learning it. And, thanks to this book, you will learn it. AppleScript is a straightforward computer language, and can be taught and learned in a straightforward manner; if I didn't believe that, this book wouldn't exist.

AppleScript is extensible, so this book will tell you how to read a scriptable application's dictionary, warn of possible pitfalls, and give plenty of examples. AppleScript is English-like, so the book will teach a clean, concise style, and will wave a red flag when an analogy with natural language threatens to mislead. AppleScript values are object-like; this book tells you how to talk to them. AppleScript has some LISP-like features; this book elicits these features where they are relevant, but where they seem too advanced, you can always skip a section and return to it later on. If this book occasionally comments on the odd way AppleScript does certain things, it is not to frighten or frustrate the reader, but rather to gain the reader's trust. It's just my way of saying, "Don't worry if this seems weird; it is weird."

So approach AppleScript without fear. It deserves respect, appreciation, and perhaps a little wonder. After all, it's amazingly old. Thanks to the Mac OS X revolution, Apple has thoroughly modernized a system that was breaking under its own accumulated weight of years; yet AppleScript remains, to all intents and purposes, its same old self. The fact that AppleScript works at all in this brave new world of Unicode text and POSIX paths is simply amazing. But it does, and until a new broom comes along to sweep it clean, having to negotiate some accumulated quirks and cobwebs dating from the creation seems a small price to pay.

Chapter 5. Syntactic Ground of Being


This chapter is about the basic facts of AppleScript language syntax. These are the facts you must be aware of before you can read or write any AppleScript code at all.

Lines


AppleScript is a line-based language. There is no visible command terminator,

Return Main Page Previous Page Next Page

®Online Book Reader