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