1 #import "ITAppleEventCenter.h"
3 Boolean MyAEIdleCallback (
4 EventRecord * theEvent,
9 Boolean MyAEIdleCallback (
10 EventRecord * theEvent,
18 @implementation ITAppleEventCenter
20 static ITAppleEventCenter *_sharedAECenter = nil;
24 if( _sharedAECenter ) {
25 return _sharedAECenter;
27 _sharedAECenter = [[ITAppleEventCenter alloc] init];
28 return _sharedAECenter;
32 - (NSString*)sendAEWithRequestedKey:(NSString*)key eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
34 //Add error checking...
35 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
36 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
38 const char *sendString = [[NSString stringWithFormat:@"'----':obj { form:'prop', want:type('prop'), seld:type('%s'), from:'null'() }", [key UTF8String]] UTF8String];
39 NSString *_finalString;
41 AppleEvent sendEvent, replyEvent;
42 AEIdleUPP upp = NewAEIdleUPP(&MyAEIdleCallback);
45 Size resultSize, charResultSize;
48 AEBuildError buildError;
52 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
53 NSLog(@"Error getting PID of application! Exiting.");
57 NSLog(@"_sendString: %s", sendString);
59 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
61 [self printCarbonDesc:&sendEvent];
64 NSLog(@"%d:%d at \"%@\"",buildError.fError,buildError.fErrorPos,[sendString substringToIndex:buildError.fErrorPos]);
67 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, upp, NULL);
69 [self printCarbonDesc:&replyEvent];
72 NSLog(@"Send Error: %i",err);
76 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
77 result = malloc(resultSize);
80 NSLog(@"Error After AESizeOfParam: %i", err2);
82 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, result, resultSize, &charResultSize);
85 NSLog(@"Error After AEGetParamPtr: %i", err3);
87 _finalString = [[NSString stringWithCharacters:result length:charResultSize/sizeof(unichar)] copy];
93 AEDisposeDesc(&sendEvent);
94 AEDisposeDesc(&replyEvent);
99 - (long)sendAEWithRequestedKeyForNumber:(NSString*)key eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
101 //Add error checking...
102 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
103 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
105 const char *sendString = [[NSString stringWithFormat:@"'----':obj { form:'prop', want:type('prop'), seld:type('%s'), from:'null'() }", [key UTF8String]] UTF8String];
108 AppleEvent sendEvent, replyEvent;
109 AEIdleUPP upp = NewAEIdleUPP(&MyAEIdleCallback);
112 Size resultSize, charResultSize;
115 AEBuildError buildError;
119 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
120 NSLog(@"Error getting PID of application! Exiting.");
124 NSLog(@"_sendString: %s", sendString);
126 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
128 [self printCarbonDesc:&sendEvent];
131 NSLog(@"%d:%d at \"%@\"",buildError.fError,buildError.fErrorPos,[sendString substringToIndex:buildError.fErrorPos]);
134 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, upp, NULL);
136 [self printCarbonDesc:&replyEvent];
139 NSLog(@"Send Error: %i",err);
141 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
144 NSLog(@"Error After AESizeOfParam: %i", err2);
146 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, &result, resultSize, &charResultSize);
149 NSLog(@"Error After AEGetParamPtr: %i", err3);
151 NSLog(@"%i", result);
156 AEDisposeDesc(&sendEvent);
157 AEDisposeDesc(&replyEvent);
162 - (NSString*)sendTwoTierAEWithRequestedKey:(NSString*)key fromObjectByKey:(NSString*)object eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
164 //Add error checking...
165 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
166 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
168 const char *sendString = [[NSString stringWithFormat:@"'----':obj { form:'prop', want:type('prop'), seld:type('%s'), from:obj { form:'prop', want:type('prop'), seld:type('%s'), from:'null'() } }", [key UTF8String], [object UTF8String]] UTF8String];
169 NSString *_finalString;
171 AppleEvent sendEvent, replyEvent;
172 AEIdleUPP upp = NewAEIdleUPP(&MyAEIdleCallback);
175 Size resultSize, charResultSize;
178 AEBuildError buildError;
182 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
183 NSLog(@"Error getting PID of application! Exiting.");
187 NSLog(@"_sendString: %s", sendString);
189 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
191 [self printCarbonDesc:&sendEvent];
194 NSLog(@"%d:%d at \"%@\"",buildError.fError,buildError.fErrorPos,[sendString substringToIndex:buildError.fErrorPos]);
197 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, upp, NULL);
199 [self printCarbonDesc:&replyEvent];
202 NSLog(@"Send Error: %i",err);
206 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
207 result = malloc(resultSize);
210 NSLog(@"Error After AESizeOfParam: %i", err2);
212 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, result, resultSize, &charResultSize);
215 NSLog(@"Error After AEGetParamPtr: %i", err3);
217 _finalString = [[NSString stringWithCharacters:result length:charResultSize/sizeof(unichar)] copy];
223 AEDisposeDesc(&sendEvent);
224 AEDisposeDesc(&replyEvent);
229 - (long)sendTwoTierAEWithRequestedKeyForNumber:(NSString*)key fromObjectByKey:(NSString*)object eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
231 //Add error checking...
232 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
233 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
235 const char *sendString = [[NSString stringWithFormat:@"'----':obj { form:'prop', want:type('prop'), seld:type('%s'), from:obj { form:'prop', want:type('prop'), seld:type('%s'), from:'null'() } }", [key UTF8String], [object UTF8String]] UTF8String];
238 AppleEvent sendEvent, replyEvent;
239 AEIdleUPP upp = NewAEIdleUPP(&MyAEIdleCallback);
242 Size resultSize, charResultSize;
245 AEBuildError buildError;
249 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
250 NSLog(@"Error getting PID of application! Exiting.");
254 NSLog(@"_sendString: %s", sendString);
256 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
258 [self printCarbonDesc:&sendEvent];
261 NSLog(@"%d:%d at \"%@\"",buildError.fError,buildError.fErrorPos,[sendString substringToIndex:buildError.fErrorPos]);
264 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, upp, NULL);
266 [self printCarbonDesc:&replyEvent];
269 NSLog(@"Send Error: %i",err);
271 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
274 NSLog(@"Error After AESizeOfParam: %i", err2);
276 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, &result, resultSize, &charResultSize);
279 NSLog(@"Error After AEGetParamPtr: %i", err3);
281 NSLog(@"%i", result);
286 AEDisposeDesc(&sendEvent);
287 AEDisposeDesc(&replyEvent);
292 - (NSString *)sendAEWithRequestedArray:(NSArray *)array eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
294 NSString *buildString = [NSString stringWithFormat:@"{ form:'prop', want:type('prop'), seld:type('%s'), from:'null'() }", [[array objectAtIndex:0] UTF8String]];
295 const char *sendString;
297 NSString *_finalString;
299 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
300 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
302 AppleEvent sendEvent, replyEvent;
303 AEIdleUPP upp = NewAEIdleUPP(&MyAEIdleCallback);
306 Size resultSize, charResultSize;
309 AEBuildError buildError;
313 for (i = 1; i < [array count]; i++) {
314 NSString *from = [NSString stringWithFormat:@"{ form:'prop', want:type('prop'), seld:type('%s'), from:obj %@ }",
315 [[array objectAtIndex:i] UTF8String], buildString];
318 buildString = [@"'----':obj " stringByAppendingString:buildString];
319 sendString = [buildString UTF8String];
321 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
322 NSLog(@"Error getting PID of application! Exiting.");
326 NSLog(@"_sendString: %s", sendString);
328 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
330 [self printCarbonDesc:&sendEvent];
333 NSLog(@"%d:%d at \"%@\"",buildError.fError,buildError.fErrorPos,[sendString substringToIndex:buildError.fErrorPos]);
336 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, upp, NULL);
338 [self printCarbonDesc:&replyEvent];
341 NSLog(@"Send Error: %i",err);
345 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
346 result=malloc(resultSize);
349 NSLog(@"Error After AESizeOfParam: %i", err2);
351 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, result, resultSize, &charResultSize);
353 NSLog(@"Error After AEGetParamPtr: %i", err3);
355 _finalString = [[NSString stringWithCharacters:result length:charResultSize/sizeof(unichar)] copy];
363 - (void)sendAEWithEventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
365 //Add error checking...
366 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
367 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
368 AppleEvent event, reply;
370 AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, kAnyTransactionID, &event, nil, "");
372 AESend(&event, &reply, kAENoReply, kAENormalPriority, kAEDefaultTimeout, nil, nil);
374 AEDisposeDesc(&event);
375 AEDisposeDesc(&reply);
378 - (void)printCarbonDesc:(AEDesc*)desc {
380 AEPrintDescToHandle(desc,&xx);
381 NSLog(@"Handle: %s", *xx);