3 kAppleScriptSubtype - AppleScript (Default)
4 'Jscr' - JavaScript (if installed)
7 #import "ITOSAScript.h"
8 #import "ITOSAComponent.h"
10 // To do - Error Dictionaries
12 @implementation ITOSAScript
16 return nil; // initWithSource: is the designated initializer for this class
19 - (id)initWithContentsOfFile:(NSString *)path
21 return [self initWithSource:[[[NSString alloc] initWithContentsOfFile:path] autorelease]];
24 - (id)initWithSource:(NSString *)source
26 if ( (self = [super init]) ) {
27 _source = [source copy];
28 _scriptID = kOSANullScript;
35 if (_scriptID != kOSANullScript) {
36 OSADispose([_component componentInstance], _scriptID);
48 - (ITOSAComponent *)component
53 - (void)setComponent:(ITOSAComponent *)newComponent
55 _component = newComponent;
58 - (BOOL)compileAndReturnError:(NSDictionary **)errorInfo
60 if ([_component componentInstance] == nil) {
61 //Set the error dictionary
66 AECreateDesc(typeChar, [_source cString], [_source cStringLength], &moof);
67 if (OSACompile([_component componentInstance], &moof, kOSAModeNull, &_scriptID) != 0) {
68 NSLog(@"Compile error!");
76 return (_scriptID != kOSANullScript);
79 - (NSAppleEventDescriptor *)executeAndReturnError:(NSDictionary **)errorInfo
81 if ([_component componentInstance] == nil) {
82 //Set the error dictionary
86 NSAppleEventDescriptor *cocoaDesc;
88 AEDesc scriptDesc, resultDesc;
89 OSAID resultID = kOSANullScript;
91 //If not compiled, compile it
92 if (![self isCompiled]) {
93 if (![self compileAndReturnError:nil]) {
94 //Set the error info dictionary
99 OSAExecute([_component componentInstance], _scriptID, kOSANullScript, kOSANullMode, &resultID);
101 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.
103 cocoaDesc = [[NSAppleEventDescriptor alloc] initWithAEDescNoCopy:&resultDesc];
105 AEDisposeDesc(&scriptDesc);
107 OSADispose([_component componentInstance], resultID);
109 return [cocoaDesc autorelease];