Learning OOP and learning how to use script objects to write object-oriented code are really two different things. (BTW, no offense to Adam or Nigel, but while the code they show may use script objects, it isn’t OOP.) OOP is a a general design technique for better organising larger programs by grouping complex data structures and the functions that operate directly on them together as self-contained objects. Script objects just happen to be the AppleScript language feature that allows you to do that sort of grouping, and therefore write object oriented code, although they’re also useful for implementing other things as well, such as callbacks, modules, and performance hacks (as Adam and Nigel’s examples show).
The reason you don’t see much OOP being done in AS is that while learning language features is easy, learning good design skills is somewhat harder, and most ASers don’t have the time/inclination/resources/need to learn more advanced design skills. That in turn means there’s not a lot of existing AppleScript-specific literature, example code and common knowledge on the subject, which makes it very hard to learn OOP unless you’re willing to look outside AppleScript for information, examples and advice. It’s a bit of a Catch-22.
…
In my case, it was a week-long debugging session on a 2000-line ‘Big Ball of Mud’ program I’d written that made me realise that I seriously needed to improve my design skills. Given the lack of useful information in the AppleScript literature, I ended up looking at books, tutorials and examples for Python (useful), Java (not so useful) and Smalltalk (educational), as well as general design books such as Code Complete (a must-have).
Once I’d sort of vaguely grasped the general purpose and motivations for OO design, I was able to go back to the ‘Script Object’ chapter in ASLG and figure out how to translate those principles into AppleScript code. (Which took a little work, as most literature deals with class-based object systems, while AS uses a form of prototype-based OO. But I got there eventually.)
I think the point at which the ‘What is OOP actually useful for?’ question finally clicked was when I wrote my own ‘associative array’ object. As you’ll probably know, AS doesn’t have a built-in hash/dictionary/map/associative array type, something that regularly bugs a lot of users. Storing values under string-based keys was something I was doing quite a bit at the time, creating lists of key-value pairs (e.g. set kvList to {{“some key”, “some value”},…}) to hold the data, and using a bunch of separate to search through those lists to add and retrive values. This all seemed a bit clumsy and disorganised though compared to how the built-in AS types did things, e.g. item 3 of someList is much clearer than getKey(kvList, keyStr). The initial breakthrough was realising that OOP allowed me to make up for AS’s shortage of built-in datatypes by using script objects to define my own ‘datatypes’. This would also give me a much neater syntax closer to that of AS’s own types, e.g. getItem(“foo”) of assocArray.
Once I was able see how OOP could be used to scratch that one particular itch so much better than procedural programming could, the whole concept just started falling into place. Later on, other advantages such as polymorphism also clicked, e.g. for a logging system, I could define interchangeable FileLog, TextEditLog, NullLog objects, each with a different logMessage(str) method. Then, instead of having to put the same big ugly if…elseif…elseif…else… block in my code each time I wanted to log something, I’d just stick one of my logging objects in a global ‘logObj’ variable and have my program say logMessage(…) of logObj. And the ability to group together related code and data into single, self-reliant units made it easier to think about how different parts of my programs could be structured internally, and how they could communicate with one another, resulting in better organised code that was easier to understand and change.
…
So, my advice is: if you want to learn OOP, first identify an itch that you need to scratch and which sounds like something OOP might be good at scratching (such as a need to supplement AS’s own rather basic datatypes with your own, the need for a flexible, extensible logging system, etc). Next, be prepared to read some general programming literature (especially Code Complete!) and study some other object-oriented languages and code a bit. (You don’t need to get fluent in those other languages or anything, just get familiar enough so you can understand what they’re trying to do and how they’re doing it.) Once you’ve got a rough idea of what OOP’s about, you can come back to the AppleScript literature and code, and start fitting the theory and practice parts together in your head.
One place you will find some decent examples of object-oriented AppleScript code to study is AppleMods. Several of the libraries I wrote for that contain object-oriented code - a good, easy one to start on is the Types library, which includes AssociativeList and Dictionary objects similar to what I’ve described.
HTH, and have fun,
has