ITOSA looks like NSAppleScript, almost. It works quite well now, except
authorKent Sutherland <ksuther@ithinksw.com>
Sat, 24 Jan 2004 15:41:53 +0000 (15:41 +0000)
committerKent Sutherland <ksuther@ithinksw.com>
Sat, 24 Jan 2004 15:41:53 +0000 (15:41 +0000)
for the information dictionary.

ITOSAComponent.h
ITOSAComponent.m
ITOSAScript.h
ITOSAScript.m

index 54b3b04..a36fab6 100755 (executable)
@@ -20,6 +20,8 @@
     ComponentInstance _componentInstance;
     NSDictionary *_information;
 }
     ComponentInstance _componentInstance;
     NSDictionary *_information;
 }
++ (ITOSAComponent *)AppleScriptComponent;
++ (ITOSAComponent *)componentWithCarbonComponent:(Component)component;
 + (NSArray *)availableComponents;
 
 - (id)initWithSubtype:(unsigned long)subtype;
 + (NSArray *)availableComponents;
 
 - (id)initWithSubtype:(unsigned long)subtype;
index 9ab1a7f..0205e7e 100755 (executable)
 
 @implementation ITOSAComponent
 
 
 @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;
 + (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;
     
     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
 }
 
 - (id)initWithSubtype:(unsigned long)subtype
         }
         
         information = [[NSMutableDictionary alloc] init];
         }
         
         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;
 }
     }
     return self;
 }
     [_information release];
 }
 
     [_information release];
 }
 
+- (NSString *)description
+{
+    return [_information objectForKey:@"ITOSAComponentName"];
+}
+
 - (Component)component
 {
     return _component;
 - (Component)component
 {
     return _component;
index 3d29aa9..7c8d337 100755 (executable)
 @interface ITOSAScript : NSObject {
     NSString *_source;
     ITOSAComponent *_component;
 @interface ITOSAScript : NSObject {
     NSString *_source;
     ITOSAComponent *_component;
+    OSAID _scriptID;
 }
 
 - (id)initWithContentsOfFile:(NSString *)path;
 - (id)initWithSource:(NSString *)source;
 
 - (NSString *)source;
 }
 
 - (id)initWithContentsOfFile:(NSString *)path;
 - (id)initWithSource:(NSString *)source;
 
 - (NSString *)source;
-- (void)setSource:(NSString *)newSource;
 - (ITOSAComponent *)component;
 - (void)setComponent:(ITOSAComponent *)newComponent;
 
 - (ITOSAComponent *)component;
 - (void)setComponent:(ITOSAComponent *)newComponent;
 
-- (BOOL)compile;
+- (BOOL)compileAndReturnError:(NSDictionary **)errorInfo;
 - (BOOL)isCompiled;
 - (BOOL)isCompiled;
-
-- (NSString *)execute;
+- (NSString *)executeAndReturnError:(NSDictionary **)errorInfo;
 
 @end
 
 @end
index 833c84f..5a20ef3 100755 (executable)
@@ -35,6 +35,7 @@ Script Subtypes:
 {
     if ( (self = [super init]) ) {
         _source = [[NSString alloc] initWithContentsOfFile:path];
 {
     if ( (self = [super init]) ) {
         _source = [[NSString alloc] initWithContentsOfFile:path];
+        _scriptID = kOSANullScript;
     }
     return self;
 }
     }
     return self;
 }
@@ -42,13 +43,18 @@ Script Subtypes:
 - (id)initWithSource:(NSString *)source
 {
     if ( (self = [super init]) ) {
 - (id)initWithSource:(NSString *)source
 {
     if ( (self = [super init]) ) {
-        [self setSource:source];
+        _source = [source copy];
+        _scriptID = kOSANullScript;
     }
     return self;
 }
 
 - (void)dealloc
 {
     }
     return self;
 }
 
 - (void)dealloc
 {
+    if (_scriptID != kOSANullScript) {
+        OSADispose([_component componentInstance], _scriptID);
+    }
+    
     [_source release];
     [super dealloc];
 }
     [_source release];
     [super dealloc];
 }
@@ -58,12 +64,6 @@ Script Subtypes:
     return _source;
 }
 
     return _source;
 }
 
-- (void)setSource:(NSString *)newSource
-{
-    [_source release];
-    _source = [newSource copy];
-}
-
 - (ITOSAComponent *)component
 {
     return _component;
 - (ITOSAComponent *)component
 {
     return _component;
@@ -74,26 +74,51 @@ Script Subtypes:
     _component = newComponent;
 }
 
     _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
 {
 }
 
 - (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;
     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);
     
     length = AEGetDescDataSize(&resultDesc);
     buffer = malloc(length);
@@ -110,6 +135,9 @@ Script Subtypes:
     }
     free(buffer);
     buffer = NULL;
     }
     free(buffer);
     buffer = NULL;
+    
+    OSADispose([_component componentInstance], resultID);
+    
     return result;
 }
 
     return result;
 }