From: Kent Sutherland Date: Sat, 24 Jan 2004 15:41:53 +0000 (+0000) Subject: ITOSA looks like NSAppleScript, almost. It works quite well now, except X-Git-Tag: v0.2~22 X-Git-Url: http://git.ithinksw.org/ITFoundation.git/commitdiff_plain/3275385775db14f654afff24919314941b8a1b7b ITOSA looks like NSAppleScript, almost. It works quite well now, except for the information dictionary. --- diff --git a/ITOSAComponent.h b/ITOSAComponent.h index 54b3b04..a36fab6 100755 --- a/ITOSAComponent.h +++ b/ITOSAComponent.h @@ -20,6 +20,8 @@ ComponentInstance _componentInstance; NSDictionary *_information; } ++ (ITOSAComponent *)AppleScriptComponent; ++ (ITOSAComponent *)componentWithCarbonComponent:(Component)component; + (NSArray *)availableComponents; - (id)initWithSubtype:(unsigned long)subtype; diff --git a/ITOSAComponent.m b/ITOSAComponent.m index 9ab1a7f..0205e7e 100755 --- a/ITOSAComponent.m +++ b/ITOSAComponent.m @@ -16,20 +16,31 @@ @implementation ITOSAComponent ++ (ITOSAComponent *)AppleScriptComponent +{ + return [[[ITOSAComponent alloc] initWithSubtype:kAppleScriptSubtype] autorelease]; +} + ++ (ITOSAComponent *)componentWithCarbonComponent:(Component)component +{ + return [[[ITOSAComponent alloc] initWithComponent:component] autorelease]; +} + + (NSArray *)availableComponents { Component currentComponent = 0; ComponentDescription cd; + NSMutableArray *components = [[NSMutableArray alloc] init]; cd.componentType = kOSAComponentType; cd.componentSubType = 0; cd.componentManufacturer = 0; cd.componentFlags = 0; cd.componentFlagsMask = 0; - - while ((currentComponent = FindNextComponent(0, &cd)) != 0) { + while ((currentComponent = FindNextComponent(currentComponent, &cd)) != 0) { + [components addObject:[ITOSAComponent componentWithCarbonComponent:currentComponent]]; } - return [NSArray array]; + return [NSArray arrayWithArray:[components autorelease]]; } - (id)initWithSubtype:(unsigned long)subtype @@ -63,11 +74,25 @@ } information = [[NSMutableDictionary alloc] init]; - [information setObject:[[[NSString alloc] initWithBytes:componentName length:GetHandleSize(componentName) encoding:NSASCIIStringEncoding] autorelease] forKey:@"ITComponentName"]; - [information setObject:[[[NSString alloc] initWithBytes:componentInfo length:GetHandleSize(componentInfo) encoding:NSASCIIStringEncoding] autorelease] forKey:@"ITComponentInfo"]; - [information setObject:[NSNumber numberWithUnsignedLong:description.componentSubType] forKey:@"ITComponentSubtype"]; - [information setObject:[NSNumber numberWithUnsignedLong:description.componentManufacturer] forKey:@"ITComponentManufacturer"]; + AEDesc name; + Ptr buffer; + Size length; + OSAScriptingComponentName(_componentInstance, &name); + length = AEGetDescDataSize(&name); + buffer = malloc(length); + + AEGetDescData(&name, buffer, length); + AEDisposeDesc(&name); + [information setObject:[NSString stringWithCString:buffer length:length] forKey:@"ITOSAComponentName"]; + free(buffer); + buffer = NULL; + + //[information setObject:[[[NSString alloc] initWithBytes:componentName length:GetHandleSize(componentName) encoding:NSASCIIStringEncoding] autorelease] forKey:@"ITOSAComponentName"]; + [information setObject:[[[NSString alloc] initWithBytes:componentInfo length:GetHandleSize(componentInfo) encoding:NSASCIIStringEncoding] autorelease] forKey:@"ITOSAComponentInfo"]; + [information setObject:[NSNumber numberWithUnsignedLong:description.componentSubType] forKey:@"ITOSAComponentSubtype"]; + [information setObject:[NSNumber numberWithUnsignedLong:description.componentManufacturer] forKey:@"ITOSAComponentManufacturer"]; + _information = [information copy]; } return self; } @@ -77,6 +102,11 @@ [_information release]; } +- (NSString *)description +{ + return [_information objectForKey:@"ITOSAComponentName"]; +} + - (Component)component { return _component; diff --git a/ITOSAScript.h b/ITOSAScript.h index 3d29aa9..7c8d337 100755 --- a/ITOSAScript.h +++ b/ITOSAScript.h @@ -20,19 +20,18 @@ @interface ITOSAScript : NSObject { NSString *_source; ITOSAComponent *_component; + OSAID _scriptID; } - (id)initWithContentsOfFile:(NSString *)path; - (id)initWithSource:(NSString *)source; - (NSString *)source; -- (void)setSource:(NSString *)newSource; - (ITOSAComponent *)component; - (void)setComponent:(ITOSAComponent *)newComponent; -- (BOOL)compile; +- (BOOL)compileAndReturnError:(NSDictionary **)errorInfo; - (BOOL)isCompiled; - -- (NSString *)execute; +- (NSString *)executeAndReturnError:(NSDictionary **)errorInfo; @end diff --git a/ITOSAScript.m b/ITOSAScript.m index 833c84f..5a20ef3 100755 --- a/ITOSAScript.m +++ b/ITOSAScript.m @@ -35,6 +35,7 @@ Script Subtypes: { if ( (self = [super init]) ) { _source = [[NSString alloc] initWithContentsOfFile:path]; + _scriptID = kOSANullScript; } return self; } @@ -42,13 +43,18 @@ Script Subtypes: - (id)initWithSource:(NSString *)source { if ( (self = [super init]) ) { - [self setSource:source]; + _source = [source copy]; + _scriptID = kOSANullScript; } return self; } - (void)dealloc { + if (_scriptID != kOSANullScript) { + OSADispose([_component componentInstance], _scriptID); + } + [_source release]; [super dealloc]; } @@ -58,12 +64,6 @@ Script Subtypes: return _source; } -- (void)setSource:(NSString *)newSource -{ - [_source release]; - _source = [newSource copy]; -} - - (ITOSAComponent *)component { return _component; @@ -74,26 +74,51 @@ Script Subtypes: _component = newComponent; } -- (BOOL)compile +- (BOOL)compileAndReturnError:(NSDictionary **)errorInfo { - return NO; + if ([_component componentInstance] == nil) { + //Set the error dictionary + return NO; + } + + AEDesc moof; + AECreateDesc(typeChar, [_source cString], [_source cStringLength], &moof); + if (OSACompile([_component componentInstance], &moof, kOSAModeNull, &_scriptID) != 0) { + NSLog(@"Compile error!"); + return NO; + } + return YES; } - (BOOL)isCompiled { - return NO; + return (_scriptID != kOSANullScript); } -- (NSString *)execute +- (NSString *)executeAndReturnError:(NSDictionary **)errorInfo { + if ([_component componentInstance] == nil) { + //Set the error dictionary + return nil; + } + AEDesc scriptDesc, resultDesc; Size length; NSString *result; Ptr buffer; + OSAID resultID = kOSANullScript; - AECreateDesc(typeChar, [_source cString], [_source cStringLength], &scriptDesc); + //If not compiled, compile it + if (![self isCompiled]) { + if (![self compileAndReturnError:nil]) { + //Set the error info dictionary + return nil; + } + } - OSADoScript([_component componentInstance], &scriptDesc, kOSANullScript, typeChar, kOSAModeCanInteract, &resultDesc); + OSAExecute([_component componentInstance], _scriptID, kOSANullScript, kOSANullMode, &resultID); + + OSADisplay([_component componentInstance], resultID, typeChar, kOSAModeDisplayForHumans, &resultDesc); length = AEGetDescDataSize(&resultDesc); buffer = malloc(length); @@ -110,6 +135,9 @@ Script Subtypes: } free(buffer); buffer = NULL; + + OSADispose([_component componentInstance], resultID); + return result; }