Re: Scriptable container objects in cocoa without defined element order
Re: Scriptable container objects in cocoa without defined element order
- Subject: Re: Scriptable container objects in cocoa without defined element order
- From: "Neal A. Crocker" <email@hidden>
- Date: Wed, 16 Jan 2002 22:33:07 -0800
On Wednesday, January 16, 2002, at 09:04 AM, cocoa-dev-
email@hidden wrote:
Can anybody show me how to expose a container object to applescript
in a cocoa applikcation whose elements don't have a defined order?
What i'm
specifically interested in doing is creating a server application
that manages NSDictionary objects. I want to expose each
NSDictionary object to applescript as a container object with
elements called "entries". Each entry has a "key" and a "value".
Ideally, I'd like each "entry" to be specifiable by ID and I don't
want to impose some arbitrary order on the entries in an NSDictionary
so that they can specified by index.
It is possible to access objects by methods other than index, and
there are actually multiple ways of going about it, depending on how
much error checking you want and how much performance matters.
For example, if you have a container that contains a bunch of
objects, you can access them by a "key" by using a "whose" test in
Applescript. For example:
tell application "MyApp"
get window whose name is "Untitled"
--or
get window "Untitled"
end tell
However, I don't believe that this is particularly efficient,
especially if you don't have a defined order for the elements of the
container. I'm pretty sure it just does a linear search, testing
each object in the array to see whether its name property matches
the given string, so it would probably bog down if you have large
numbers of child objects, but for small things it's simple and it
should work.
To implement it, just implement a method that returns an NSArray of
the child objects. For instance:
-(NSArray*)values
{
return [myDictionary allValues];
}
Their order won't matter when a script evaluates a whose test, but
this will still allow scripts to access objects by index, and the
index will probably be nonsensical.
This method occured to me, but it raises some problems, if I
understand cocoa script support correctly. As far as I understand
it, if I do this, then cocoa will automatically allow script to make
new "entries" at any desired postition within the array. Perhaps I'm
missing something. Perhaps cocoa wouldn't allow this if I specify
that the "entries" property of the dictionary class is readonly in
the scriptSuite file. Alternatively, may cocoa wouldn't allow it if
I failed to implement a "setValues" method. My grasp of how
applescript interacts with cocoa objects is still somewhat hazy.
Alternatively, I think you should also be able to handle get/set
commands manually by adding an entry under the SupportedCommands key
and specifying a method in your app to handle the commands. I'm not
entirely certain what object you'll have to add the method to in
order to have it called. It's probably the container object that
has the key/value pairs, but I'm not 100% certain.
Anyway, once your command handler method is called, you can retrieve
the command arguments from the NSScriptCommand object that gets
passed to the command handler method. From there, you can look at
the NSScriptObjectSpecifiers yourself and return the appropriate
value. Also, if a script tries to access something by index, you
can use setScriptErrorNumber: and setScriptErrorString: to return an
error message.
That has distinct possibilities. I hadn't realized that get and set
were treated as ordinary events by cocoa.
Brian Webster
email@hidden
http://homepage.mac.com/bwebster
Thanks,
Neal.