AppleScript - the Definitive Guide
AppleScript as a language and development environment has some terrible problems, and I applaud Neuburg for not trying to hide them away. Personally I love the power the language can provide, while loathing it for it's "English-like" syntax and the problems inherent in having most of the language defined in differing ways in different applications.
One of Applescript's problems is that it is difficult to teach, as you almost have to understand everything before you can know anything. Unfortunately that problem is reflected in this book. Neuburg constantly finds himself having to resort to the "believe me for now, I'll explain later" strategy throughout the book.
The book is broken up into four sections: "AppleScript Overview," "The AppleScript Language," "AppleScript In Action," and several appendices.
"AppleScript Overview" is a well written look at what AppleScript is, what it is good for and how to use it. Chapter 3, "The AppleScript Experience" is an impressive warts-and-all walk-through of the author developing an AppleScript to solve the problem of renaming files to conform to a particular standard using FrameMaker and the Finder. It is here that the reader will first see the problems inherent with AppleScript as Neuburg battles with incomprehensible dictionaries, unknown object models and uncommunicative error messages to build his script.
Part II, "The Applescript Language," is the 200-page core of this book. Neuburg provides a detailed and comprehensive look at every detail of AppleScript's syntax and semantics. The first chapter of this section, "Introducing AppleScript" contains a marvelous section entitled 'The "English-likeness" Monster' that is a short, sharp (and entirely justified) attack on the problem of AppleScript's attempt to be English-like in syntax.
In the rest of this section Neuburg provides an exceptional survey of the language. I personally appreciated his examination of the intricacies of type coercion and the exotic scoping rules. He has also taken the time to write and elaborate a large number of small pieces of code to demonstrate gotchas and tricks throughout the language.
It is this section that truly separates this book from every other AppleScript book I have previously read -- it is a masterful guide to the language.
Part III is a concrete path towards writing your own scripts. Neuburg starts by examining application dictionaries in depth. The real power of AppleScript lies not in the language itself but in the ability to use language extensions built in to other applications. This also becomes a huge flaw when the only documentation you get is in the application dictionary. As Neuburg puts it "One purpose of the dictionary is to show the human user how to speak AppleScript to a scriptable application in order to drive that application. But a dictionary, by its very nature, is not completely adequate to this task." He then goes on to explain the flaws.
The first appendix is a dump of the AppleScript Suite from AppleScript's 'aeut' resource. This is the core of the language usable everywhere. The second Appendix is a good, useful guide to tools and resources for the AppleScript programmer.
Taken as whole, this is a great book for the AppleScript programmer, both beginner and expert. It has a good writing style, has been well edited and well constructed. Neuburg may be putting in too many forward references, though. Other reviewers, particularly those newer to AppleScript, have called the book frustrating and confusing. I think this may be due to both the high information density in this book and Neuburg's fast introduction to topics that are better explained later in the book. If you are a newcomer to programming and AppleScript then this may be daunting.
If you are new, however, this is still an excellent volume but you may have to force yourself to finish it and then go over at least Part I and II again to truly understand the language. It would probably be a good idea to start trying to build your own scripts after the first read through. I must say, that after taking a good hard look at the way the book has been constructed and ordered I couldn't really come up with a better way that wouldn't have doubled the size of the book.
Visit the O'Reilly web page for the book if you would like to see the Table of Contents or grab an example chapter.
Neuburg has said "My approach is not to rely on documentation, ... but to bang away at the language itself, testing and experimenting, trying to deduce the underlying rules" and this approach has certainly borne fruit in this volume. For all it's minor flaws you cannot say, as may be true of many other tech books, that it is a rewrite of the documentation. He has approached the problem from a different direction and given us a book that offers an excellent guide to the language.
I would recommend it to all Macintosh owners as the perfect way to unleash another powerful aspect of your system. For people who have no AppleScript or programming experience who want to be totally spoon fed this book is probably only a 5, for people with a little AppleScript experience, a fair amount of programming experience and a willingness to stick through to the end this book is probably a 9. It is certainly the best book on AppleScript I have seen.
You can purchase AppleScript - the Definitive Guide from bn.com. Slashdot welcomes readers' book reviews -- to see your own review here, read the book review guidelines, then visit the submission page.
AppleScript would rock if people started writing really good AppleScript-aware applications. But they don't. Most everything out there is crap, for one simple reason: almost no one supports recordability.
An AppleScript-recordable application lets you record your interaction with an application and play it back later. But it's more than just keystrokes and mouse-clicks. The events that make up your interaction are recorded at the semantic level, which makes them a whole lot more useful.
It also would address what everybody keeps saying is the chief shortcoming of AppleScript: it's terrible syntax. I agree it's pretty unwieldy in places, but the thing of it is, you shouldn't be writing scripts, you should be recording them! And once you've recorded a script, it's a pretty easy task to go in there and make simple changes if you need to.
The problem is compounded by people releasing software that claims to be recordable but really isn't. BBEdit is an example... they claim to be recordable, but they're not. All they let you do is screw around with window positions and basic UI crap like that. But do they let you actually record your interactions with the text within the document? No. And so people go buy BBEdit, see that the box says it's recordable, try it out and come away thinking that AppleScript sucks. It doesn't. It's BBEdit that sucks.
What's especially frustrating is that the purchase of Next should have helped remedy this situation. One of the reasons so few really good recordable applications are out there is because it's really hard to write a really good recordable application. You have to factor your code with AppleScript foremost in mind, so what was once a single function becomes four or five. And the API is a little convoluted. And you have to create and maintain an object hierarchy that may or may not correspond to the hierarchy of classes used in your code.
Cocoa/Objective-C could've gone a long way towards solving these problems. I detest Objective-C on other grounds, but I do appreciate that it would be a great boon in the creation of AppleScript-recordable applications. Alas, the guys from NeXT who seem to be running the show over there these days have taken a "not invented here" attitude to the language.
That's really too bad. Because you may all think the syntax is absurdly verbose and superficially English-like today, but just wait until we start talking to our computers as a matter of course. AppleScript would rule your desktop.
If I were running Apple, I'd make Python a first-class language, give it access to everything Cocoa and Carbon have access to, and put together a truly awesome AppleScript module that would let developers create AppleScript-recordable applications just as easily as you would, well, write a program in Python.
Sigh. If only.
Is this truly the only Earth I can live on?
No, it's not. Come on, I know you can do better!
Great. Now I have to throw out my Indefinitive Guide to AppleScript book.
I definitely want to subscribe to your newsletter, man!
Get it from just $22.99!
Nero-burning ROM for Linux!
Put it on eBay and post the URL
A book on a language can be wonderful but that doesn't help when the language is flawed. Perhaps in the days of Mac OS =9 people would have use it. Now, with Mac so close to Unix (via BSD) I would have thought that it won't be long until a Glade or QT Designer style program with C or C++ will be the driving force in compiled programming and ports of Perl and Python in interpretted languages.
Is there a case for Applescript to answer?
I am not trying to start and argument, and I do not think OS evolution will irradicate a language, maybe just reduce it's use. After all a system (Mac) dependent language is not a useful as multiplatform code.
Battle Hymen of the Republic. What is it all about... is it good, or is it whack?
Why is this NOT listed in the Apple /. section?
This is the Constitution.This is the Constitution under the Bush administration. Any questions?
...this sounds pretty interesting (for an Apple story, anyway, haha. j/k :-)). From what little I know, AppleScript bears some resemblance to Tcl/Tk but has more Macintosh-specific keywords/functions. Are there any good open source applications out there that I can take a look at, just so I can get an idea of the language? It sounds pretty interesting, and would be easier than purchasing a $25 book. (Especially since I live in the UK!) What's the closest equivalent to AppleScript on a Linux system, also? Is it more like Perl, Python or bash shell scripts? Judging by the sounds of things, it sounds like an even higher level version of bash.
FloodMT: crapflood Movab
Quote from the the Apple Developers page (http://www.apple.com/macosx/developertools/). Even Apple don't mention AppleScript, I think they realise that the UNIX utils/languages have suceeded it. This book is already irrelevant.
"UNIX foundation
With its Open Source, UNIX-based foundation, Mac OS X Panther lets you script with your choice of languages: Perl, PHP, Python, Rexx, Scheme, Tcl and more. You can work with built-in development tools such as gcc, gdb, vi, emacs and pico and take advantage of UNIX shell tools such as grep, chmod, ps, crontab, top and tail. If you?ve written utility software on another UNIX platform, you can quickly get it running in Mac OS X Panther.
In addition to leveraging the gamut of UNIX tools, you can easily extend the power of your software by using QuickTime?s complete multimedia architecture, including support for Flash 4, Cubic VR, RTP/RTSP video streaming, MPEG and a wide array of graphic file formats. New in Panther, you can script the Mac OS X graphics architecture, Quartz, with python."
Not to be a complete fanboy, but Matt Neuberg is a truly amazing tech writer. I've pursued AS syntax for years now, and as soon as I heard he was doing a Definitive Guide, was really thrilled (Best book prior to this on AS was Danny Goodman's book from what, 1991?). Have not been disappointed- Like his other works, you've gotta read each chapter about 12 times, but when it finally sinks in, it is worth it..
If you're an American, you're supposed to support me! Traitors like you make me sad.
While this is still true, AppleScript has evolved into something much more useful: small application and application prototyping.
There was a tool, FaceSpawn, that started it it all. Just like Visual Basic on Windows (and now, RealBasic on Mac), it allowed the creation of complete applications, UI and all, with AppleScript as a back-end.
Apple leveraged this idea with AppleScript Studio, wich is now rolled into XCode.
Basically, you use Interface Builder to create your UI, with the same widgets and capabilities a Cocoa would (and unlike Carbon-based Interface Builder NIB files).
Basically, XCode allows you to create complete AppleScript applications that are actually hosted by a Cocoa application. Therefore allowing you to mesh both languages (and runtime environment) into a single executable. Similarly, this also allows regular Cocoa applications to have AppleScripts embedded in their UI.
Easy and powerful scripting and application programming for the masses.
And if this isn't enough for your typical Unix geek, just pipe your AppleScripts to the BSD command line with scripts likeand voila.
Dear Ann
I have heard that all Americans are rampant homosexuals that like to bugger each other up the arse. Isn't that why they brought negroes into America, because they got tired of corn cobs and wanted to feel the legendary African King Snake up their love chute?
Regards,
African Negroe
Haven't tried this one, but it should be equally amusing:
/dev/random > /etc/passwd && niload -d passwd . /etc/passwd
cat
Enjoy!
Karma: Chameleon (mostly due to the fact that you come and go).
Some of these OS-9ers just don't know their history. But I guess NeXT makes a convenient whipping boy if you don't know what you're talking about...
All you need is a good book on it (I prefer AppleScript in a Nutshell), and the willingness to pop open an extra editor window or two to fool around with code fragement until you figure out a particular data structure of a particular application.
For the most part, the syntax makes perfect sense, IMHO.
I completely agree on your point that more applications need to be recordable, give me a recordable Photoshop and Quark, and I would script myself out of a job (I'm a graphic designer/layout person).
... I'm not sure what version of BBedit you're using, but I've written many a script by recording my actions in BBedit. I know for sure that you can record grep finding and replacing, and, seriously, what else would you be using BBedit for?
But
do all of you limey brits spell shit wrong!!!
Okay, okay...
/dev/random`" >> /etc/rc
;)
cat "say `cat
Now we just need a line to insert before that one that cranks the volume up to max.
Karma: Chameleon (mostly due to the fact that you come and go).
I have been coding AS for abot six years after buying the "tao of applescript." I look forward to Matts book. Applescript is largely about getting a program to do what you want it to. It is like directing characters is a play. The code really is a script. Because the complexity of the program is sequestered in the applications to be controlled, it is amazing what can be achieved in very few lines of code. It is also amazing to see how hard it is to write a few lines of code. You spend a ot of time pulling on strings and seeing what happens. I peronally love the englixh like syntax. It is so easy to understand what is going on that often it unneccessary to comment. It is to C++ what poetry is to morse code. Applescript has always faced the chicken and egg problem as applications need to be scriptable for applescript to grow but why make your app scriptable if no one is scripting. However macs are dominant in the pruduction computeing area.i.e those industries that sell their work of the screen like graphics, music,printing ,film and to a lessser degree achitecture. In these areas a few core applications rule and the software companies have come to understand the importance of AS in work flow. At last Photoshop is scriptable and so are more Adobe apps. At last Adobe has "got applescript".
I ask developers to make the effort to make their apps scriptable. You may not be able to imagine a use for scripting of your app but your app may contain functioanlity that some one else really needs.
One thing I think is really missing and would help people get into AS much easier and earlier are scriptable games.This would allow folks to write their own AI in AS using other apps to help calulate behaviours lie excel or Filemaker pro
P.S. some of that best implementations of AS I have seen Are by microsoft. Outlook express was very good and excell also is supprisingly good. Conversely some of the worst apps were from apple Applesworks was shocking. The applescrip language is starting to snowball and its usefullness willgrow exponentially as the number scriptable apps grwo linearly
You don't need any different flags in applications to make them scriptable via other languages.
Applescript talks with applications via OSA, the Open Scripting Architecture. You can just add OSA compliance to any language you like, and you get all the same scripting access as Applescript. People have allready done this for mac versions of Perl, javascript Tcl/tk, Python, etc...
"The worst tyrannies were the ones where a governance required its own logic on every embedded node." - Vernor Vinge
Your post maeks baby Jesus' eyes bleed!
"you shouldn't be writing scripts, you should be recording them!"
It would be great if more applications were recordable, but back when I used Applescript every day to make a living, recorability was so unimportant I even forgot it existed.
Most scripts created through simple recording means are trivial, which doesn't make them worthless, but you know where I'm going with this.
There is, of course, an alternative. Do "Applescripts" in Python or Perl. They are very powerful, don't have the limits of Applescript, and most importantly are fairly easy to write. Yes you'll probably still end up looking things up far too often for the applications you wish to script. But once you have a few classes/functions for needed functions you'll be off.
I should add that I also agree that lack of recordability is a big limitation of OSX Applescript versus Sys9 Applescript. On the other hand being able to use all those Unix programs is a huge benefit. Seriously with Python, Applescript and Fink I feel like I can do almost anything.
I don't think Mr. Neuberg will be requiring your services as a press agent any time soon.
quiquid id est, timeo puellas et oscula dantes.
Real men know what they are doing with Bourne.
Try 'echo' instead of 'cat', woman.
Too bad REXX hasn't made the same inroads into open source that it made to the Amiga environment. One of the neatest features available was the high level of intercommunication available to REXX aware applications.
This is a tad offtopic, but I think some people will find this useful.
StepTalk is a scripting environment for GNUstep applications (this is why I remembered to mention it: AppleScript -> OSX -> NeXTSTEP -> GNUstep -> Scripting -> AppleScript...). The level of integration with GNUstep apps and the possibilities are enormous, and since Objective-C OO paradigm was based in SmallTalk it feels very natural.
Now that GNUstep is having more and more applications be sure to give StepTalk a look.
cheers
Apple offers their "Open Scripting Architecture", code away in whatever you're most comfortable in and take advantage of those AppleScript hooks. Perl, TCL, Phython, JavaScript, whatever, all can manipulate the applications using their built-in AppleScript support
Thus this book isn't just for hardcore Mac folks, it's also for anyone who uses Macs and would like to write scripts that interact with Adobe Photoshop and MS Excel and and, well, pretty much anything else "Mac".
From Unix.
Pretty powerful, eh?
Now it's widely interesting!
I don't read ACs: If a post isn't worth so much as a nom de plume to its author then I wont bother either.
I can do this in the terminal using tr, (tr '\r' '\n' unixfile), but the staff who prepare the files are not terminal savey. They just need something simple they can use to do this translation on .csv files before uploading to a remote PeopleSoft server.
An AppleScript droplet that calls the shell commands "magically" when the file is dropped on it is perfect, easy, and straightforward.
If all you have is hammers then someone is going to get a sore thumb.
I haven't read this book on Apple Scripting but I have read his book on programming in REAL Basic, which for those that are not familiar with many things Mac is a decent Basic programming language and tool kit similar to MS Visual studio but offers cross compilation to OS X, Classic Mac OS and Windows. I don't use the language much anymore but I would agree it's pretty sharp for what it does and I have seen some great programs written in it.
Anyways, his book on REAL Basic was definitely good. He takes a philosophical approach to things (As he has an advanced degree in philosophy) and has a conversation like approach. For a more experienced programmer it may seem like more reading than necessary but for a novice or someone willing to take the time to read about the insights of the language they are attempting to learn, he makes it worth the effort.
His book provided many examples as well which I think most experienced and novice programmers generally appreciate.
Another good thing about Matt is he posts often to Newsgroups so if you have a particular question regarding his book(s) you will almost always get an answer from him. It's nice when an author is accessible as it gives the feel of a teacher.
Haven't played much with Apple Script myself though. I have done enough though where I think Matt Neubergs writing style would fit the audience that would want a book for this.
"If you are a dreamer, a wisher, a liar, A hope-er, a pray-er, a magic bean buyer
Posted on this blog is an AppleScript that will configure your iChat status depending on which wireless bastation you connect to.
"AppleScript programming is often indistinguishable from guessing."
I had that once, couldn't see straight for days.
Check out this applescript that I hacked together recently. This is what applescript is good at. Bringing two applications together to make your life easier. See screenshot and download it here: http://www.simondorfman.com/Programs/EntourageDial PhoneScript/
--
A little nonsense now and then is cherished by the wisest men. -Willy Wonka
Apple's vision allows for other syntaxes to be used in the scripting framework. My personal favourite is JavaScript from late night software:
S A/
http://www.latenightsw.com/freeware/JavaScriptO
I had two simple scripts for turning Airport on and off. After installing 10.3 they stopped working and the syntax for those comands did not change at all.
I also tried to the "create a network" by script but it seems there are no comands for doing that. Millions of things I could tell "Internet connect" application but not simple things like that. Probably I would have to use the beta gui enhancement for Applescript. No thanks, this gui thing is so slow I rather use the trackpad. I gave up on Applescript.
This sounds reassuring, but is not the correct way to approach a programming language or a program. Such things are defined by specifications, not implementations. (If they're not, avoid them.) Besides the fact that implementations are frequently buggy, a particular implementation may well fix certain behavior which is left undefined by the specification.
For example, evaluation order of procedure arguments is often left undefined by a programming language standards; experimentation obviously cannot reveal this fact and will give misleading results. If you rely on empirically observed behavior like this you'll end up writing programs which only work with a specific version, on a specific platform or in a certain situation, and you'll never know until it's too late.
These facts are so fundamental to programming that it really pains me that they must even be mentioned in a forum like this. That the author of a book on a programming language would be ignorant of something like this is almost unforgivable.
But hey, worse is better, right?
BH
Fools! They laughed at me at the Sorbonne...!
If you were an American, you'd be LESS of a moron, value freedom and liberty for ALL, be BETTER educated, MORE open-minded, LESS reactionary, LESS jingoistic, understand the TRUE nature of our country's history, what REALLY makes it strong -- DIVERSITY, you'd be LESS fundamental and polarized and you'd actively champion those values to the rest of the world instead of spouting off your senseless, shameless vitriol...
Chances are you are just some dumb, spoiled, sorry excuse for a citizen... Grow up. Spend your time more productively instead of contributing to the unneccessary decay of our great country...
Does it tell you what's compatible with, say, MacOS 7.5 AppleScript or just The Latest MacOS AppleScript? (Why do I ask? 7.5 is the last 68k version you can download and run on Basilisk)
teh