I'd like to introduce you to ITAppleEventCenter, our new facility for sending and...
[ITFoundation.git] / ITAppleEventCenter.m
1 #import "ITAppleEventCenter.h"
2
3 Boolean MyAEIdleCallback (
4                                          EventRecord * theEvent,
5                                          SInt32 * sleepTime,
6                                          RgnHandle * mouseRgn
7                                          );
8
9 Boolean MyAEIdleCallback (
10                                          EventRecord * theEvent,
11                                          SInt32 * sleepTime,
12                                          RgnHandle * mouseRgn
13                                          )
14 {
15     return FALSE;
16 }
17
18 @implementation ITAppleEventCenter
19
20 static ITAppleEventCenter *_sharedAECenter = nil;
21
22 + (id)sharedCenter
23 {
24     if( _sharedAECenter != nil ) {
25         return _sharedAECenter;
26     } else {    
27         _sharedAECenter = [[ITAppleEventCenter alloc] init];
28         return _sharedAECenter;
29     }
30 }
31
32 - (NSString*)runAEWithRequestedKey:(NSString*)key eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
33 {
34     AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
35     AEEventID    eID    = *((unsigned long*)[eventID UTF8String]);
36     
37     const char *sendString = [[NSString stringWithFormat:@"'----':obj { form:'prop', want:type('prop'), seld:type('%s'), from:'null'() }", [key UTF8String]] UTF8String];
38     NSString  *_finalString;
39     
40     // Variables for building and sending the event    
41     AppleEvent sendEvent, replyEvent;
42     //AEDesc replyDesc;
43     AEIdleUPP upp = NewAEIdleUPP(&MyAEIdleCallback);
44     
45     // Variables for getting the result
46     char *charResult;
47     DescType resultType;
48     Size resultSize, charResultSize;
49     
50     // Variables for error checking
51     AEBuildError buildError;
52     OSStatus err;
53     OSErr err2, err3;
54     //Ptr buffer;
55     //Size length;
56     
57     // Start Code
58     // ^ Most pointless comment EVAR!
59     // ^^ Nope, that one is
60
61     NSLog(@"_sendString: %s", sendString);
62     
63     // The problem is, if I use eClass and eID in place of 'core' and 'getd' respectively, It won't build a valid AppleEvent :'(
64     err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
65     
66     [self printCarbonDesc:&sendEvent];
67     
68     if (err) {
69         NSLog(@"%d:%d at \"%@\"",buildError.fError,buildError.fErrorPos,[sendString substringToIndex:buildError.fErrorPos]);
70     }
71     
72     err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, upp, NULL);
73     
74     [self printCarbonDesc:&replyEvent];
75     
76     if (err) {
77         NSLog(@"Send Error: %i",err);
78     } else {
79
80         unichar* result=0;
81         
82         err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
83         result=malloc(resultSize);
84         //err2 = AEGetParamDesc(&replyEvent, keyDirectObject, typeWildCard, &replyDesc);
85         
86         if (err2) { NSLog(@"Error After AESizeOfParam: %i", err2); } else {
87         //if (err2) { NSLog(@"Error After AEGetParamDesc: %i", err2); } else {
88         
89         err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, result, resultSize, &charResultSize);
90         //length = AEGetDescDataSize(&replyDesc);
91         //buffer = malloc(length);
92     
93         //err3 = AEGetDescData(&replyDesc, buffer, length);
94         
95         if (err3) { NSLog(@"Error After AEGetParamPtr: %i", err3); } else {
96         
97         // if (err3) { NSLog(@"Error After AEGetDescData: %i", err3); } else {
98         
99         _finalString = [[NSString stringWithCharacters:result length:charResultSize/sizeof(unichar)] copy];
100         
101         /* _finalString = [NSString stringWithCString:buffer length:length];
102         
103         if ( (! [_finalString isEqualToString:@""])      &&
104             ([_finalString characterAtIndex:0] == '\"') &&
105             ([_finalString characterAtIndex:[_finalString length] - 1] == '\"') ) {
106             _finalString = [_finalString substringWithRange:NSMakeRange(1, [_finalString length] - 2)];
107         }
108         free(buffer);
109         buffer = nil; */
110
111         }
112         }
113     }
114     
115     //NSLog(@"Result Size: %i", charResultSize);
116     //NSLog(@"Result: %c", charResult);
117     
118     AEDisposeDesc(&sendEvent);
119     AEDisposeDesc(&replyEvent);
120     
121     return _finalString;
122 }
123
124 - (void)printCarbonDesc:(AEDesc*)desc {
125     Handle xx;
126     AEPrintDescToHandle(desc,&xx);
127     NSLog(@"Handle: %s", *xx);
128     DisposeHandle(xx);
129 }
130
131 @end