for the information dictionary.
ComponentInstance _componentInstance;
NSDictionary *_information;
}
++ (ITOSAComponent *)AppleScriptComponent;
++ (ITOSAComponent *)componentWithCarbonComponent:(Component)component;
+ (NSArray *)availableComponents;
- (id)initWithSubtype:(unsigned long)subtype;
@implementation ITOSAComponent
++ (ITOSAComponent *)AppleScriptComponent
+{
+ return [[[ITOSAComponent alloc] initWithSubtype:kAppleScriptSubtype] autorelease];
+}
+
++ (ITOSAComponent *)componentWithCarbonComponent:(Component)component
+{
+ return [[[ITOSAComponent alloc] initWithComponent:component] autorelease];
+}
+
+ (NSArray *)availableComponents
{
Component currentComponent = 0;
ComponentDescription cd;
+ NSMutableArray *components = [[NSMutableArray alloc] init];
cd.componentType = kOSAComponentType;
cd.componentSubType = 0;
cd.componentManufacturer = 0;
cd.componentFlags = 0;
cd.componentFlagsMask = 0;
-
- while ((currentComponent = FindNextComponent(0, &cd)) != 0) {
+ while ((currentComponent = FindNextComponent(currentComponent, &cd)) != 0) {
+ [components addObject:[ITOSAComponent componentWithCarbonComponent:currentComponent]];
}
- return [NSArray array];
+ return [NSArray arrayWithArray:[components autorelease]];
}
- (id)initWithSubtype:(unsigned long)subtype
}
information = [[NSMutableDictionary alloc] init];
- [information setObject:[[[NSString alloc] initWithBytes:componentName length:GetHandleSize(componentName) encoding:NSASCIIStringEncoding] autorelease] forKey:@"ITComponentName"];
- [information setObject:[[[NSString alloc] initWithBytes:componentInfo length:GetHandleSize(componentInfo) encoding:NSASCIIStringEncoding] autorelease] forKey:@"ITComponentInfo"];
- [information setObject:[NSNumber numberWithUnsignedLong:description.componentSubType] forKey:@"ITComponentSubtype"];
- [information setObject:[NSNumber numberWithUnsignedLong:description.componentManufacturer] forKey:@"ITComponentManufacturer"];
+ AEDesc name;
+ Ptr buffer;
+ Size length;
+ OSAScriptingComponentName(_componentInstance, &name);
+ length = AEGetDescDataSize(&name);
+ buffer = malloc(length);
+
+ AEGetDescData(&name, buffer, length);
+ AEDisposeDesc(&name);
+ [information setObject:[NSString stringWithCString:buffer length:length] forKey:@"ITOSAComponentName"];
+ free(buffer);
+ buffer = NULL;
+
+ //[information setObject:[[[NSString alloc] initWithBytes:componentName length:GetHandleSize(componentName) encoding:NSASCIIStringEncoding] autorelease] forKey:@"ITOSAComponentName"];
+ [information setObject:[[[NSString alloc] initWithBytes:componentInfo length:GetHandleSize(componentInfo) encoding:NSASCIIStringEncoding] autorelease] forKey:@"ITOSAComponentInfo"];
+ [information setObject:[NSNumber numberWithUnsignedLong:description.componentSubType] forKey:@"ITOSAComponentSubtype"];
+ [information setObject:[NSNumber numberWithUnsignedLong:description.componentManufacturer] forKey:@"ITOSAComponentManufacturer"];
+ _information = [information copy];
}
return self;
}
[_information release];
}
+- (NSString *)description
+{
+ return [_information objectForKey:@"ITOSAComponentName"];
+}
+
- (Component)component
{
return _component;
@interface ITOSAScript : NSObject {
NSString *_source;
ITOSAComponent *_component;
+ OSAID _scriptID;
}
- (id)initWithContentsOfFile:(NSString *)path;
- (id)initWithSource:(NSString *)source;
- (NSString *)source;
-- (void)setSource:(NSString *)newSource;
- (ITOSAComponent *)component;
- (void)setComponent:(ITOSAComponent *)newComponent;
-- (BOOL)compile;
+- (BOOL)compileAndReturnError:(NSDictionary **)errorInfo;
- (BOOL)isCompiled;
-
-- (NSString *)execute;
+- (NSString *)executeAndReturnError:(NSDictionary **)errorInfo;
@end
{
if ( (self = [super init]) ) {
_source = [[NSString alloc] initWithContentsOfFile:path];
+ _scriptID = kOSANullScript;
}
return self;
}
- (id)initWithSource:(NSString *)source
{
if ( (self = [super init]) ) {
- [self setSource:source];
+ _source = [source copy];
+ _scriptID = kOSANullScript;
}
return self;
}
- (void)dealloc
{
+ if (_scriptID != kOSANullScript) {
+ OSADispose([_component componentInstance], _scriptID);
+ }
+
[_source release];
[super dealloc];
}
return _source;
}
-- (void)setSource:(NSString *)newSource
-{
- [_source release];
- _source = [newSource copy];
-}
-
- (ITOSAComponent *)component
{
return _component;
_component = newComponent;
}
-- (BOOL)compile
+- (BOOL)compileAndReturnError:(NSDictionary **)errorInfo
{
- return NO;
+ 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 NO;
+ return (_scriptID != kOSANullScript);
}
-- (NSString *)execute
+- (NSString *)executeAndReturnError:(NSDictionary **)errorInfo
{
+ if ([_component componentInstance] == nil) {
+ //Set the error dictionary
+ return nil;
+ }
+
AEDesc scriptDesc, resultDesc;
Size length;
NSString *result;
Ptr buffer;
+ OSAID resultID = kOSANullScript;
- AECreateDesc(typeChar, [_source cString], [_source cStringLength], &scriptDesc);
+ //If not compiled, compile it
+ if (![self isCompiled]) {
+ if (![self compileAndReturnError:nil]) {
+ //Set the error info dictionary
+ return nil;
+ }
+ }
- OSADoScript([_component componentInstance], &scriptDesc, kOSANullScript, typeChar, kOSAModeCanInteract, &resultDesc);
+ OSAExecute([_component componentInstance], _scriptID, kOSANullScript, kOSANullMode, &resultID);
+
+ OSADisplay([_component componentInstance], resultID, typeChar, kOSAModeDisplayForHumans, &resultDesc);
length = AEGetDescDataSize(&resultDesc);
buffer = malloc(length);
}
free(buffer);
buffer = NULL;
+
+ OSADispose([_component componentInstance], resultID);
+
return result;
}