• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag
 

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Script Objects
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Script Objects


  • Subject: Re: Script Objects
  • From: Chris Page <email@hidden>
  • Date: Mon, 18 Jan 2010 20:50:11 -0800

On Jan 18, 2010, at 2:31 PM, Luther Fuller wrote:

I've never written a script object and I'm not familiar with "data and methods", so I'm not sure what he's getting at, but he is beginning to make some sense.

Every script is a script object. When you write a script, it is as if the entire script is implicitly surrounded with a “script … end script” statement.

So, when you write a script like

property MyName : "Script One"

say "My name is " & MyName

it is almost as if you wrote

script
property MyName : "Script One"

on run
say "My name is " & MyName
end run
end script

I say “almost” because if you run the above it creates a script object and sets that to the result, but it does not run the new script. To run the resulting script, you’d have to add

run the result

which is essentially what happens on your behalf when you run a script with Script Editor or osascript, or by opening a script applet.

Script objects are collections of properties and handlers. (Cocoa calls these “instance variables”—“ivars” for short—and “methods”, respectively.) They can also inherit properties and handlers from other script objects, via the “parent” property, which aids in script organization and enables the sharing of properties and the reuse of handlers.

The reasons for defining script objects are similar to the reasons for defining handlers. They help organize large or complex scripts into smaller pieces that are easier to work with, and they enable you to reuse code/objects.

Simple scripts just use the implicit “run” handler, more complex scripts may define other handlers. Similarly, simple scripts just use the single, “top-level” script object for their properties and handlers, more complex ones may define other script objects.

Here’s an example of using multiple instances (copies) of a script object to represent several different people. When you “copy” a script object, you create a new object whose properties can be changed without altering the original.

script Person
property FullName : "Anonymous"
property Title : "Unknown"

on SayHello()
set message to "Hello, my name is " & FullName & ", " & Title & "."
log message
say message
end SayHello
end script

copy Person to Elmer
set Elmer's FullName to "Elmer J. Fudd"
set Elmer's Title to "Millionaire"

copy Person to Shake
set Shake's FullName to "Shake Zula"
set Shake's Title to "Mike Rula"

tell Shake to SayHello()
tell Person to SayHello()
tell Elmer to SayHello()

-- 
Chris Page

 The other, other AppleScript Chris

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users

This email sent to email@hidden

  • Follow-Ups:
    • Re: Script Objects
      • From: Luther Fuller <email@hidden>
References: 
 >Script Objects (From: Luther Fuller <email@hidden>)

  • Prev by Date: Re: right click menu
  • Next by Date: Re: right click menu
  • Previous by thread: Re: Script Objects
  • Next by thread: Re: Script Objects
  • Index(es):
    • Date
    • Thread