On 13 Jul 2007, at 10:12 PM, Scott Thompson wrote:
I'm working on an AppleScript class for my application and I'd like
to create some properties that can be defined when an instance of
the class is created, but which are read-only afterward.
For example, the instances of the class include the "name" and
"category" properties. These properties are defined when the
instance is created, and can be read afterward, but there should
not be any way to change those properties.
I'm curious about the right way to express that in the sdef and in
the running application. I envision two scenarios:
1. I mark the properties as "read-only" in the sdef and require the
user to specify the value of those properties in the "with
properties" parameter of the "make" command. Can a read-only
property still be initialized with the "with properties" parameter?
Yes: you can get the properties from [NSScriptCommand currentCommand]
in -init.
2. I leave the properties read/write but just return an error if
you try to set the value after the instance is created.
If they are normally read-only, it is better to flag them as such in
the sdef. Note that this flag is only used for clarity to the user,
and doers not have any real purpose.
Also you have to implement the setters, but you best leave them
empty. Don't return an error, because they will be called right after
init (in the implementation of NSCreateCommand).
Is either of these scenarios the preferred one? (or is there some
other mechanism that would work better?)
Scott
So I would say the first one is the preferred one, and the second one
would not even work.
An alternative is to use the "with data" argument, but that only
works when your object can be created by coercing some standard data
object.