ITOSA looks like NSAppleScript, almost. It works quite well now, except
[ITFoundation.git] / ITOSAComponent.m
1 /*
2  *      ITFoundation
3  *      ITOSAComponent
4  *          A Cocoa wrapper for scripting components.
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 #import "ITOSAComponent.h"
16
17 @implementation ITOSAComponent
18
19 + (ITOSAComponent *)AppleScriptComponent
20 {
21     return [[[ITOSAComponent alloc] initWithSubtype:kAppleScriptSubtype] autorelease];
22 }
23
24 + (ITOSAComponent *)componentWithCarbonComponent:(Component)component
25 {
26     return [[[ITOSAComponent alloc] initWithComponent:component] autorelease];
27 }
28
29 + (NSArray *)availableComponents
30 {
31     Component currentComponent = 0;
32     ComponentDescription cd;
33     NSMutableArray *components = [[NSMutableArray alloc] init];
34     
35     cd.componentType = kOSAComponentType;
36     cd.componentSubType = 0;
37     cd.componentManufacturer = 0;
38     cd.componentFlags = 0;
39     cd.componentFlagsMask = 0;
40     while ((currentComponent = FindNextComponent(currentComponent, &cd)) != 0) {
41         [components addObject:[ITOSAComponent componentWithCarbonComponent:currentComponent]];
42     }
43     return [NSArray arrayWithArray:[components autorelease]];
44 }
45
46 - (id)initWithSubtype:(unsigned long)subtype
47 {
48     ComponentDescription cd;
49     cd.componentType = kOSAComponentType;
50     cd.componentSubType = subtype;
51     cd.componentManufacturer = 0;
52     cd.componentFlags = 0;
53     cd.componentFlagsMask = 0;
54     Component temp = FindNextComponent(0, &cd);
55     if ( (self = [self initWithComponent:temp]) ) {
56     }
57     return self;
58 }
59
60 - (id)initWithComponent:(Component)component;
61 {
62     if ( (self = [super init]) ) {
63         Handle componentName = NewHandle(0);
64         Handle componentInfo = NewHandle(0);
65         ComponentDescription description;
66         NSMutableDictionary *information;
67         
68         _component = component;
69         _componentInstance = OpenComponent(component);
70         
71         if (GetComponentInfo(component, &description, componentName, componentInfo, nil) != 0) {
72             NSLog(@"FATAL ERROR!");
73             return nil;
74         }
75         
76         information = [[NSMutableDictionary alloc] init];
77         
78         AEDesc name;
79         Ptr buffer;
80         Size length;
81         OSAScriptingComponentName(_componentInstance, &name);
82         length = AEGetDescDataSize(&name);
83         buffer = malloc(length);
84
85         AEGetDescData(&name, buffer, length);
86         AEDisposeDesc(&name);
87         [information setObject:[NSString stringWithCString:buffer length:length] forKey:@"ITOSAComponentName"];
88         free(buffer);
89         buffer = NULL;
90         
91         //[information setObject:[[[NSString alloc] initWithBytes:componentName length:GetHandleSize(componentName) encoding:NSASCIIStringEncoding] autorelease] forKey:@"ITOSAComponentName"];
92         [information setObject:[[[NSString alloc] initWithBytes:componentInfo length:GetHandleSize(componentInfo) encoding:NSASCIIStringEncoding] autorelease] forKey:@"ITOSAComponentInfo"];
93         [information setObject:[NSNumber numberWithUnsignedLong:description.componentSubType] forKey:@"ITOSAComponentSubtype"];
94         [information setObject:[NSNumber numberWithUnsignedLong:description.componentManufacturer] forKey:@"ITOSAComponentManufacturer"];
95         _information = [information copy];
96     }
97     return self;
98 }
99
100 - (void)dealloc
101 {
102     [_information release];
103 }
104
105 - (NSString *)description
106 {
107     return [_information objectForKey:@"ITOSAComponentName"];
108 }
109
110 - (Component)component
111 {
112     return _component;
113 }
114
115 - (ComponentInstance)componentInstance
116 {
117     return _componentInstance;
118 }
119
120 - (NSDictionary *)information
121 {
122     return _information;
123 }
124
125 @end