Today is/was my first day at a job for which I am required to learn applescript. So far I have been looking at the tutorials on apple’s website, and this forum, which I found on google. Can anyone with experience recommend any other specific tutorials or books that they found particularly helpful? Thanks!
edit: I have a pretty good deal of programming experience in C++. I have little familiarity with Mac OS.
Lack of Mac OS experience shouldn’t present a problem; AppleScript is a bit of a unique enclave there anyway. Given you’re coming to AS from a’real programming’ background, here’s some random thoughts (from someone who’s going in the opposite direction;):
Learn to distinguish between the AppleScript language itself (which is very small and simple) and its inter-application communication facilities (akin to RPC). While AS tries to mask the differences between language objects and application objects, it’s a somewhat leaky abstraction. The quicker you learn to see the differences, the less frustrating you’ll find the whole experience (e.g. the language’s ‘string’ and ‘Unicode text’ objects do not behave in quite the same ways as a typical application’s ‘text’ objects; a common source of confusion for users when they’re not clear which they’re manipulating).
Similarly, learn to identify underlying similarites even when surface syntax differs (e.g. AS has six or seven different syntaxes for sending a message to an object, but apart from the one used for XML-RPC the difference is purely cosmetic and doesn’t distinguish between local and remote messaging.)
Everything in AS is an object; no C++/Java-style mix of primitives and objects. The AppleScript language has few built-in commands. All native objects have at least one property, ‘class’; more complex types often have additional properties and elements that can often be manipulated. You won’t find a substr() function, for example; instead use the ‘text’ element of a string to get a sub-string.
Swot up briefly on the Smalltalk ‘message passing’ model. I think this’ll help in understanding how AS operates; i.e. by sending synchronous messages* to local [language] objects or remote [application] objects, where they’re either handled by [event] handlers, delegated to another object or (as a last resort) cause a ‘object Foo doesn’t recognise message Bar’ exception to be raised. By and large, just think in terms of virtual method calls to local/remote objects and you shouldn’t go far wrong. While Apple events do enable both synchronous and asynchronous communication, AppleScript and application scripting only bother with the former.
AS also borrows some of Smalltalk’s philosophy on object persistency. Compiled scripts are themselves objects, and data can persist even after they’re stored to disk. A lovely little feature, and I’m still waiting to see the more mainstream scripting languages catch up with it on this.
Amongst the list of nice things about Mac IAC that’s often overlooked is that application object models are accessed via a rather nice smart query-based mechanism, rather than the traditional ‘dumb’ hierarchical object model you may be more used to using. e.g. A single message can often be sent to more than one object at a time (depending on how good the application object model is); e.g. ‘set minimized of windows 2 thru -1 to true’; ‘delete every paragraph whose text is “n” of text of document 1’
AppleScript loves adding application and scripting addition keywords to the language. This is a real barrel of laughs, oh yes. Be on the guard for conflicts between language/application keywords and your own identifiers; AS’s syntax highlighting will help you distinguish between the two. (This is why most AS scripters use abbreviations or compound words like ‘str’ or ‘theString’ as identifiers: there’s a bit less chance of these conflicting with keywords than plain words.)
Working with references (both application and language-based) takes some getting used to. (Though anyone who can learn pointer arithmetic and live to tell the tale should find it easy by comparison.) As usual, the problem is finding a sufficiently good explanation of how they work in the first place; once you grok 'em though they’re fine.
While application objects are fairly standard in having properties, elements and methods (typically referred to as “commands”), commands are documented separately from the classes upon which they operate. This is a great idea in theory (it makes for very neat documentation and fits the non-programmer’s “do something to something” mindset quite well), but due to some terminally braindead oversight, traditional application terminology resources neglect to document which commands act upon which classes. As a result, application scripters have to rely on secondary documentation (where available), bundled and/or third-party examples, educated guesses and plain old trial and error. This’ll be fixed eventually (tho’ don’t hold your breath); in the meantime, buy yourself a swear-jar.
Aside from some static variable scoping stuff that’s done for performance reasons, the language is pretty dynamic.
Variables are untyped. Many built-in types support various casts and coercions which is cool, except when it’s not; so make sure you know what’s going on type-wise as there’s various well-known gotchas with stuff like concatenation and ‘repeat with itemRef in iterableObj’ looks.
While AppleScript has a variety of built-in types (it refers to them all as “classes”), and applications similarly use a fairly conventional class-object approach, it doesn’t have user-defined class type. Coming from C++, if you want to do OOP in AppleScript you’ll need to swot up on prototype-based OOP, which AS has adequate, if relatively basic, support for via its ‘script’ type. I’d recommend reading up on Sun’s Self language for pointers.
The AppleScript language lacks a native modules/language extension mechanism. This is a bit of a pain for doing modular/reusable construction. There are two ways you can ‘add’ commands and/or objects to your scripts: 1. via C-based scripting additions, aka ‘osax[en]’ (these add commands that AS presents globally); 2. via an ad-hoc AppleScript library-loading mechanism. Simple static bindings are easily doing just using script properties and the ‘load script’ command from the Standard Additions osax. Best third-party, static/dynamic, portable AS library binding system you’ll find is Loader, which I wrote a couple years back; see AppleMods (note: I think it’s a bit short on developer docs at the moment; if so, I’d suggest bugging under the AM maintainer so he can then bug me;).
Look for examples of application scripting code to see how other folks have done it; ScriptBuilders is probably a good place to start. Note that a lot of third-party AS code is somewhat variable in quality (most ASers are not professional programmers); the examples on Apple’s own side also tend to be a bit mediocre. So you’ll need to hunt around a bit to find good stuff. For some nice examples of vanilla AS code, feel free to check out some of my other libraries at AM.
Finally, while AppleScript is the most common scripting language used for Mac IAC, it’s by no means the only one. There are several others, of which Perl and Python are currently the closest and are slowly but surely catching up with AS. See Mac::Glue and appscript [mine] respectively. You might want to keep an eye on these as well. In particular, you may find comparing appscript helps you understand how AS’s own IAC support ties in with the rest of the language (as the way appscript works is modelled after it).