833c84f49ba3f46700a027920fb2f4c474c3775b
[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 @implementation ITOSAScript
25
26 - (id)init
27 {
28     if ( (self = [super init]) ) {
29         _source = nil;
30     }
31     return self;
32 }
33
34 - (id)initWithContentsOfFile:(NSString *)path
35 {
36     if ( (self = [super init]) ) {
37         _source = [[NSString alloc] initWithContentsOfFile:path];
38     }
39     return self;
40 }
41
42 - (id)initWithSource:(NSString *)source
43 {
44     if ( (self = [super init]) ) {
45         [self setSource:source];
46     }
47     return self;
48 }
49
50 - (void)dealloc
51 {
52     [_source release];
53     [super dealloc];
54 }
55
56 - (NSString *)source
57 {
58     return _source;
59 }
60
61 - (void)setSource:(NSString *)newSource
62 {
63     [_source release];
64     _source = [newSource copy];
65 }
66
67 - (ITOSAComponent *)component
68 {
69     return _component;
70 }
71
72 - (void)setComponent:(ITOSAComponent *)newComponent
73 {
74     _component = newComponent;
75 }
76
77 - (BOOL)compile
78 {
79     return NO;
80 }
81
82 - (BOOL)isCompiled
83 {
84     return NO;
85 }
86
87 - (NSString *)execute
88 {
89     AEDesc scriptDesc, resultDesc;
90     Size length;
91     NSString *result;
92     Ptr buffer;
93     
94     AECreateDesc(typeChar, [_source cString], [_source cStringLength], &scriptDesc);
95     
96     OSADoScript([_component componentInstance], &scriptDesc, kOSANullScript, typeChar, kOSAModeCanInteract, &resultDesc);
97     
98     length = AEGetDescDataSize(&resultDesc);
99     buffer = malloc(length);
100     
101     AEGetDescData(&resultDesc, buffer, length);
102     AEDisposeDesc(&scriptDesc);
103     AEDisposeDesc(&resultDesc);
104     result = [NSString stringWithCString:buffer length:length];
105     if (![result isEqualToString:@""] &&
106         ([result characterAtIndex:0] == '\"') &&
107         ([result characterAtIndex:[result length] - 1] == '\"'))
108     {
109         result = [result substringWithRange:NSMakeRange(1, [result length] - 2)];
110     }
111     free(buffer);
112     buffer = NULL;
113     return result;
114 }
115
116 @end