X-Git-Url: http://git.ithinksw.org/ITFoundation.git/blobdiff_plain/f52146424225e2d13a8fe7a29d703676c91d0d46..a562b4a9e91163116b73f2df7d6589d812d2278e:/ITOSAScript.m diff --git a/ITOSAScript.m b/ITOSAScript.m index 6b079c8..6f1a27f 100755 --- a/ITOSAScript.m +++ b/ITOSAScript.m @@ -19,38 +19,37 @@ Script Subtypes: */ #import "ITOSAScript.h" +#import "ITOSAComponent.h" + +#warning To do - Error Dictionaries @implementation ITOSAScript - (id)init { - if ( (self = [super init]) ) { - _source = nil; - _scriptSubtype = kAppleScriptSubtype; //Default to AppleScript - } - return self; + return nil; // initWithSource: is the designated initializer for this class } - (id)initWithContentsOfFile:(NSString *)path { - if ( (self = [super init]) ) { - _source = [[NSString alloc] initWithContentsOfFile:path]; - _scriptSubtype = kAppleScriptSubtype; //Default to AppleScript - } - return self; + return [self initWithSource:[[[NSString alloc] initWithContentsOfFile:path] autorelease]]; } - (id)initWithSource:(NSString *)source { if ( (self = [super init]) ) { - [self setSource:source]; - _scriptSubtype = kAppleScriptSubtype; //Default to AppleScript + _source = [source copy]; + _scriptID = kOSANullScript; } return self; } - (void)dealloc { + if (_scriptID != kOSANullScript) { + OSADispose([_component componentInstance], _scriptID); + } + [_source release]; [super dealloc]; } @@ -60,49 +59,68 @@ Script Subtypes: return _source; } -- (void)setSource:(NSString *)newSource +- (ITOSAComponent *)component { - [_source release]; - _source = [newSource copy]; + return _component; +} + +- (void)setComponent:(ITOSAComponent *)newComponent +{ + _component = newComponent; } -- (unsigned long)scriptSubtype +- (BOOL)compileAndReturnError:(NSDictionary **)errorInfo { - return _scriptSubtype; + 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; } -- (void)setScriptSubtype:(unsigned long)newSubtype +- (BOOL)isCompiled { - _scriptSubtype = newSubtype; + return (_scriptID != kOSANullScript); } -- (NSString *)execute +- (NSAppleEventDescriptor *)executeAndReturnError:(NSDictionary **)errorInfo { + if ([_component componentInstance] == nil) { + //Set the error dictionary + return nil; + } + + NSAppleEventDescriptor *cocoaDesc; + AEDesc scriptDesc, resultDesc; - Size length; - NSString *result; - Ptr buffer; + OSAID resultID = kOSANullScript; + + //If not compiled, compile it + if (![self isCompiled]) { + if (![self compileAndReturnError:nil]) { + //Set the error info dictionary + return nil; + } + } - AECreateDesc(typeChar, [_source cString], [_source cStringLength], &scriptDesc); + OSAExecute([_component componentInstance], _scriptID, kOSANullScript, kOSANullMode, &resultID); - OSADoScript(OpenDefaultComponent(kOSAComponentType, _scriptSubtype), &scriptDesc, kOSANullScript, typeChar, kOSAModeCanInteract, &resultDesc); + OSACoerceToDesc([_component componentInstance], resultID, typeWildCard, kOSAModeNull, &resultDesc); // Using this instead of OSADisplay, as we don't care about human readability, but rather, the integrity of the data. - length = AEGetDescDataSize(&resultDesc); - buffer = malloc(length); + cocoaDesc = [[NSAppleEventDescriptor alloc] initWithAEDescNoCopy:&resultDesc]; - AEGetDescData(&resultDesc, buffer, length); AEDisposeDesc(&scriptDesc); - AEDisposeDesc(&resultDesc); - result = [NSString stringWithCString:buffer length:length]; - if (![result isEqualToString:@""] && - ([result characterAtIndex:0] == '\"') && - ([result characterAtIndex:[result length] - 1] == '\"')) - { - result = [result substringWithRange:NSMakeRange(1, [result length] - 2)]; - } - free(buffer); - buffer = NULL; - return result; + + OSADispose([_component componentInstance], resultID); + + return [cocoaDesc autorelease]; } @end