X-Git-Url: http://git.ithinksw.org/ITFoundation.git/blobdiff_plain/f52146424225e2d13a8fe7a29d703676c91d0d46..3275385775db14f654afff24919314941b8a1b7b:/ITOSAScript.m diff --git a/ITOSAScript.m b/ITOSAScript.m index 6b079c8..5a20ef3 100755 --- a/ITOSAScript.m +++ b/ITOSAScript.m @@ -19,6 +19,7 @@ Script Subtypes: */ #import "ITOSAScript.h" +#import "ITOSAComponent.h" @implementation ITOSAScript @@ -26,7 +27,6 @@ Script Subtypes: { if ( (self = [super init]) ) { _source = nil; - _scriptSubtype = kAppleScriptSubtype; //Default to AppleScript } return self; } @@ -35,7 +35,7 @@ Script Subtypes: { if ( (self = [super init]) ) { _source = [[NSString alloc] initWithContentsOfFile:path]; - _scriptSubtype = kAppleScriptSubtype; //Default to AppleScript + _scriptID = kOSANullScript; } return self; } @@ -43,14 +43,18 @@ Script Subtypes: - (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,32 +64,61 @@ Script Subtypes: return _source; } -- (void)setSource:(NSString *)newSource +- (ITOSAComponent *)component { - [_source release]; - _source = [newSource copy]; + return _component; } -- (unsigned long)scriptSubtype +- (void)setComponent:(ITOSAComponent *)newComponent { - return _scriptSubtype; + _component = newComponent; } -- (void)setScriptSubtype:(unsigned long)newSubtype +- (BOOL)compileAndReturnError:(NSDictionary **)errorInfo { - _scriptSubtype = newSubtype; + 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; } -- (NSString *)execute +- (BOOL)isCompiled { + return (_scriptID != kOSANullScript); +} + +- (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(OpenDefaultComponent(kOSAComponentType, _scriptSubtype), &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); @@ -102,6 +135,9 @@ Script Subtypes: } free(buffer); buffer = NULL; + + OSADispose([_component componentInstance], resultID); + return result; }