*/
#import "ITOSAScript.h"
+#import "ITOSAComponent.h"
@implementation ITOSAScript
{
if ( (self = [super init]) ) {
_source = nil;
- _scriptSubtype = kAppleScriptSubtype; //Default to AppleScript
}
return self;
}
{
if ( (self = [super init]) ) {
_source = [[NSString alloc] initWithContentsOfFile:path];
- _scriptSubtype = kAppleScriptSubtype; //Default to AppleScript
+ _scriptID = kOSANullScript;
}
return self;
}
- (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];
}
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);
}
free(buffer);
buffer = NULL;
+
+ OSADispose([_component componentInstance], resultID);
+
return result;
}