| FFPerson.h |
|
@interface FFPerson : NSObject { NSString* m_first; NSString* m_last; } - (id)initWithFirstName:(NSString*)first andLastName:(NSString*)last; + (FFPerson*)personWithFirstName:(NSString*)first andLastName:(NSString*)last; - (void)setFirstName:(NSString*)first; - (NSString*)firstName; - (void)setLastName:(NSString*)last; - (NSString*)lastName; @end |






| FFController.m |
|
@implementation FFController - (void)awakeFromNib { [m_personsController addObject:[FFPerson personWithFirstName:@"Dieter" andLastName:@"Kuhn"]]; [m_personsController addObject:[FFPerson personWithFirstName:@"Otto" andLastName:@"Dodo"]]; [m_personsController addObject:[FFPerson personWithFirstName:@"Joseph" andLastName:@"Meier"]]; [m_personsController addObject:[FFPerson personWithFirstName:@"John" andLastName:@"Doe"]]; } |
| KValueAScript.sdef |
|
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE dictionary SYSTEM "file://localhost/System/Library/DTDs/sdef.dtd"> <dictionary title="Standard Terminology"> <suite name="KValueAScript" code="????" description="Classes and commands."> <classes> <class name="application" code="capp" inherits="NSApplication" description="Main controller"> <cocoa class="FFApplication"/> <elements> <element type="person" access="r"> <cocoa method="persons"> </element> </elements> </class> <class name="person" code="fper" inherits="NSCoreSuite.AbstractObject" description="A single person"> <cocoa class="FFPerson"/> <properties> <property name="firstName" code="frst" type="string" description="First name"> <cocoa method="firstName"/> <!-- Example. Not required because of KV-coding --> </property> <property name="lastName" code="last" type="string" description="Last name"/> </properties> </class> </classes> <commands> <command name="hello" code="kvashell" description="Says hello"> <cocoa class="FFScript"/> </command> <command name="say" code="kvassayt" description="Make some noise"> <cocoa class="FFScript"/> <direct-parameter type="object" description="The text"/> </command> <command name="wert" code="kvaswert" description="Returns a value"> <cocoa class="FFScript"/> <result type="string" description="value"/> </command> </commands> </suite> </dictionary> |
| FFApplication.m |
|
#import "FFApplication.h" @implementation FFApplication - (id)init { self = [super init]; if (self) m_reggedContainers = [[NSMutableDictionary alloc] init]; return self; } - (void)dealloc { [m_reggedContainers release]; [super dealloc]; } #pragma mark - #pragma mark Container management - (void)registerContainer:(id)container forKey:(id)key { [m_reggedContainers setObject:container forKey:key]; } - (id)valueForKey:(NSString*)key { return [m_reggedContainers objectForKey:key]; } - (NSScriptObjectSpecifier*)specifierForObject:(id)obj fromContainerWithKey:(id)key { // Search container id container = [m_reggedContainers objectForKey:key]; if (container == nil) return nil; // Search object unsigned oidx = [container indexOfObjectIdenticalTo:obj]; if (oidx == NSNotFound) return nil; // Create specifier NSScriptClassDescription* cdesc = (NSScriptClassDescription*) [NSScriptClassDescription classDescriptionForClass:[FFApplication class]]; return [[[NSIndexSpecifier allocWithZone:[self zone]] initWithContainerClassDescription:cdesc containerSpecifier:nil key:key index:oidx] autorelease]; } @end |
| FFPerson.m - new method |
|
- (NSScriptObjectSpecifier*)objectSpecifier { return [NSApp specifierForObject:self fromContainerWithKey:@"persons"]; } |
| FFController.m - added code |
|
- (void)awakeFromNib { : [NSApp registerContainer:[m_personsController content] forKey:@"persons"]; } |
| FFScript.h |
|
@interface FFScript : NSScriptCommand // Nothing to declare here... @end |
| FFScript.m |
|
#import "FFScript.h" @implementation FFScript - (id)performDefaultImplementation { NSString* txt; switch ([[self commandDescription] appleEventCode]) { case FOUR_CHAR_CODE('hell') : txt = @"Hello world"; break; case FOUR_CHAR_CODE('sayt') : txt = [NSString stringWithFormat:@"You said '%@'", [self directParameter]]; break; case FOUR_CHAR_CODE('wert') : return @"I am evil"; default: // Something strange happend NSLog(@"unknown command . commandName=%@, directparm=%@", [[self commandDescription] commandName],[self directParameter]); return nil; } NSLog(@"Script command text: %@", txt); return nil; } |
| all.scpt - modified for the tutorial | |
| tell application "KValueAScript" --* Elements and their properties *-- |
|
| count of persons -- 4 |
count of [NSApp valueForKey:@"persons"] |
| firstName of person 2 -- "Otto" |
1. $tmp = 2nd object from [NSApp valueForKey:@"persons"] 2. [$tmp firstName] |
| set fp to first person lastName of fp -- "Kuhn" |
1. fp = 1st object from [NSApp valueForKey:@"persons"] 2. [fp objectSpecifier] (from FFPerson.m which calls specifierForObject: from FFApplication.m 3. [fp lastName] (actually its a second valueForKey call) |
| repeat with p in persons firstName of p end repeat -- "Dieter", "Otto"... |
1. $tmp = [NSApp valueForKey:@"persons"] 2. p = next object from $tmp ($tmp is cached) 3. [p firstName] 4. Goto step 2. till all objects were processed. |
| --* Commands *-- | |
| hello -- Script command text: "hello world" |
|
| say "howdy" -- Script command text: "howdy" |
|
| set x to wert -- "I am evil" |
|
| end tell | |