Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.


  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • DownloadDownload
  • PrintPrint
Share this Page URL
Help

Chapter 12. Script Objects > Script Objects and Object-Oriented Programming - Pg. 401

Script Objects store script testScript in file scriptFileName replacing yes -- run the script stored in the file run script file scriptFileName run script file scriptFileName -- load the script into memory and run it set loadedScript to load script file scriptFileName run loadedScript run loadedScript 2. Run the program. You should see four successive dialogs containing the numbers 100 , 100 , 100 , and 101 , respectively. How It Works Each time the run script command is executed, the script contained in the file is defined. So the prop- erty X is initialized to 100 each time run script is executed on the file test.scpt . That explains the first two values of 100 that are shown in the dialog. If you load a script using the load script command, it reads the script into memory but doesn't exe- cute it. You need to run it. The program loads the script and stores it in the variable loadedScript . Loading a script file does have the effect of reinitializing the script file's properties. The second time the script is run, the display dialog command shows you that the property X has the value 101 , verifying that its value does persist and is not reinitialized each time the run command is executed. Script Objects and Object-Oriented Programming I mentioned at the start of this chapter that AppleScript supports many of the notions of object-oriented programming. You already know that it has the concepts of classes and objects. But it's not as obvious that AppleScript also supports some fundamental OOP features such as inheritance, instantiation, data encapsulation, and polymorphism. There's not enough space in this book to teach OOP concepts. As a result, this section is geared for pro- grammers with some OOP background. If you don't have such a background, you should read through this section anyway. Perhaps you'll learn something new! Script objects provide the foundation for OOP programming in AppleScript because they provide the means for you to create an object that contains its own hidden variables (in OOP parlance instance vari- ables), and to associate a set of handlers (methods) with that object. The script object also encapsulates those handlers, meaning that you can have handlers with the same name inside different script objects. This provides the mechanism for supporting OOP's concept of polymorphism, which I discuss in more detail in the next section. 401