Initial import of ITMac sources.
[ITMac.git] / ITOSAScript.m
1 /*
2 Script Subtypes:
3         kAppleScriptSubtype - AppleScript (Default)
4         'Jscr' - JavaScript (if installed)
5 */
6
7 #import "ITOSAScript.h"
8 #import "ITOSAComponent.h"
9
10 // To do - Error Dictionaries
11
12 @implementation ITOSAScript
13
14 - (id)init
15 {
16         return nil; // initWithSource: is the designated initializer for this class
17 }
18
19 - (id)initWithContentsOfFile:(NSString *)path
20 {
21         return [self initWithSource:[[[NSString alloc] initWithContentsOfFile:path] autorelease]];
22 }
23
24 - (id)initWithSource:(NSString *)source
25 {
26         if ( (self = [super init]) ) {
27                 _source = [source copy];
28                 _scriptID = kOSANullScript;
29         }
30         return self;
31 }
32
33 - (void)dealloc
34 {
35         if (_scriptID != kOSANullScript) {
36                 OSADispose([_component componentInstance], _scriptID);
37         }
38         
39         [_source release];
40         [super dealloc];
41 }
42
43 - (NSString *)source
44 {
45         return _source;
46 }
47
48 - (ITOSAComponent *)component
49 {
50         return _component;
51 }
52
53 - (void)setComponent:(ITOSAComponent *)newComponent
54 {
55         _component = newComponent;
56 }
57
58 - (BOOL)compileAndReturnError:(NSDictionary **)errorInfo
59 {
60         if ([_component componentInstance] == nil) {
61                 //Set the error dictionary
62                 return NO;
63         }
64         
65         AEDesc moof;
66         AECreateDesc(typeChar, [_source cString], [_source cStringLength], &moof);
67         if (OSACompile([_component componentInstance], &moof, kOSAModeNull, &_scriptID) != 0) {
68                 NSLog(@"Compile error!");
69                 return NO;
70         }
71         return YES;
72 }
73
74 - (BOOL)isCompiled
75 {
76         return (_scriptID != kOSANullScript);
77 }
78
79 - (NSAppleEventDescriptor *)executeAndReturnError:(NSDictionary **)errorInfo
80 {
81         if ([_component componentInstance] == nil) {
82                 //Set the error dictionary
83                 return nil;
84         }
85         
86         NSAppleEventDescriptor *cocoaDesc;
87         
88         AEDesc scriptDesc, resultDesc;
89         OSAID resultID = kOSANullScript;
90         
91         //If not compiled, compile it
92         if (![self isCompiled]) {
93                 if (![self compileAndReturnError:nil]) {
94                         //Set the error info dictionary
95                         return nil;
96                 }
97         }
98         
99         OSAExecute([_component componentInstance], _scriptID, kOSANullScript, kOSANullMode, &resultID);
100         
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.
102         
103         cocoaDesc = [[NSAppleEventDescriptor alloc] initWithAEDescNoCopy:&resultDesc];
104         
105         AEDisposeDesc(&scriptDesc);
106         
107         OSADispose([_component componentInstance], resultID);
108         
109         return [cocoaDesc autorelease];
110 }
111
112 @end