Cocoa AppleScript question
Cocoa AppleScript question
- Subject: Cocoa AppleScript question
- From: "Brant Sears" <email@hidden>
- Date: Tue, 17 Jan 2006 17:33:01 -0500
- Thread-topic: Cocoa AppleScript question
I have a cocoa application and I am trying to enable the following AppleScript functionality:
1. The application has a concept of a set of "friends". To make things simple, let's say each friend only has a first name. Therefore, I don't really care whether the friend is a single string or is a class that has a property that is a string called "name". However, because I really don't understand the format of the .sdef file and am copying based on examples from the documentation, I have implemented a simple class called "friend" that responds to the "name" selector.
2. The application has a concept of a "selected friend". One friend at a time can be the "selected" friend.
What I want to enable via AppleScript is:
The ability to retrieve the name of the selected friend as a string. (I have this working.)
The ability to set the selected friend by supplying a string that matches the name of a friend. (I have this working.)
The ability to retrieve a list of all of the friends that the app has available. (This part I need help with.)
So, in my "controller" class - which is my app delegate, I added the following methods:
-(BOOL)application:(NSApplication *)sender delegateHandlesKey:(NSString*)key
{
printf("%s\n", [key cString]);
if ([key isEqualToString:@"selectedFriend"]) {
return YES;
} else if ([key isEqualToString:@"friends"]) {
return YES;
}
return NO;
}
- (NSString*)selectedFriend
{
return mSelectedFriend;
}
- (void)setSelectedFriend:(NSString*)friend
{
if (mSelectedFriend) {
[mSelectedFriend release];
}
mSelectedFriend = [friend copy];
}
- (NSArray*)friends
{
NSArray * temp = [NSArray arrayWithObjects: @"Rachael", @"Monica", @"Phoebe", @"Ross", @"Chandler", @"Joey", nil];
NSMutableArray * temp2 = [NSMutableArray arrayWithCapacity: [temp count]];
NSEnumerator * myEnumerator = [temp objectEnumerator];
NSString * aName;
while (aName = [myEnumerator nextObject]) {
[temp2 addObject: [Friend friendWithName: aName];
}
return [NSArray arrayWithArray: temp2]; // return a static array of Friend objects.
}
Then there is a "Friend" class that contains a single NSString * and an accessor function "name" to retrieve it. AutoReleased Friend objects can be created using "friendWithName:" class method.
Then I have a "friends.sdef" that looks like this:
<class name="application" code="capp"
description="The application's top-level scripting object.">
<cocoa class="NSApplication"/>
<property name="name" code="pnam" type="text" access="r"
description="The name of the application."/>
<property name="frontmost" code="pisf" type="boolean" access="r"
description="Is this the frontmost (active) application?">
<cocoa key="isActive"/>
</property>
<property name="CurrentFriend" code="ccnl" type="text" access="rw" description="The name of the current friend.">
<cocoa key="selectedFriend"/>
</property>
<element type="window" access="r">
<cocoa key="orderedWindows"/>
</element>
<element type="friends" access="r">
<cocoa key="friends"/>
</element>
<responds-to name="quit">
<cocoa method="handleQuitScriptCommand:"/>
</responds-to>
</class>
<class name="friend" plural="friends" code="frnd" description="A friend.">
<cocoa class="Friend"/>
<property name="name" code="name" type="text" access="r" description="The friend's name" >
<cocoa key="name"/>
</property>
</class>
</suite>
</dictionary>
The result is that the CurrentFriend property works OK, but trying to retrieve the list results in an error inside the application that apparently has to do with NSScriptObjectSpecifier. I'm looking at that class and I'm a bit baffled at the way it is being used in the "Sketch" example.
How close am I to havin this do what I want? What kinds of changes do I need to make to get this to do what I want?
Thanks.
Brant Sears
<<winmail.dat>>
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden