4 * An extended NSAppleScript that allows any OSA language.
6 * Original Author : Kent Sutherland <ksutherland@ithinksw.com>
7 * Responsibility : Kent Sutherland <ksutherland@ithinksw.com>
8 * Responsibility : Joseph Spiros <joseph.spiros@ithinksw.com>
10 * Copyright (c) 2002 - 2004 iThink Software.
17 kAppleScriptSubtype - AppleScript (Default)
18 'Jscr' - JavaScript (if installed)
21 #import "ITOSAScript.h"
22 #import "ITOSAComponent.h"
24 @implementation ITOSAScript
28 if ( (self = [super init]) ) {
34 - (id)initWithContentsOfFile:(NSString *)path
36 if ( (self = [super init]) ) {
37 _source = [[NSString alloc] initWithContentsOfFile:path];
38 _scriptID = kOSANullScript;
43 - (id)initWithSource:(NSString *)source
45 if ( (self = [super init]) ) {
46 _source = [source copy];
47 _scriptID = kOSANullScript;
54 if (_scriptID != kOSANullScript) {
55 OSADispose([_component componentInstance], _scriptID);
67 - (ITOSAComponent *)component
72 - (void)setComponent:(ITOSAComponent *)newComponent
74 _component = newComponent;
77 - (BOOL)compileAndReturnError:(NSDictionary **)errorInfo
79 if ([_component componentInstance] == nil) {
80 //Set the error dictionary
85 AECreateDesc(typeChar, [_source cString], [_source cStringLength], &moof);
86 if (OSACompile([_component componentInstance], &moof, kOSAModeNull, &_scriptID) != 0) {
87 NSLog(@"Compile error!");
95 return (_scriptID != kOSANullScript);
98 - (NSString *)executeAndReturnError:(NSDictionary **)errorInfo
100 if ([_component componentInstance] == nil) {
101 //Set the error dictionary
105 AEDesc scriptDesc, resultDesc;
109 OSAID resultID = kOSANullScript;
111 //If not compiled, compile it
112 if (![self isCompiled]) {
113 if (![self compileAndReturnError:nil]) {
114 //Set the error info dictionary
119 OSAExecute([_component componentInstance], _scriptID, kOSANullScript, kOSANullMode, &resultID);
121 OSADisplay([_component componentInstance], resultID, typeChar, kOSAModeDisplayForHumans, &resultDesc);
123 length = AEGetDescDataSize(&resultDesc);
124 buffer = malloc(length);
126 AEGetDescData(&resultDesc, buffer, length);
127 AEDisposeDesc(&scriptDesc);
128 AEDisposeDesc(&resultDesc);
129 result = [NSString stringWithCString:buffer length:length];
130 if (![result isEqualToString:@""] &&
131 ([result characterAtIndex:0] == '\"') &&
132 ([result characterAtIndex:[result length] - 1] == '\"'))
134 result = [result substringWithRange:NSMakeRange(1, [result length] - 2)];
139 OSADispose([_component componentInstance], resultID);