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