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 return _sharedAECenter = [[ITAppleEventCenter alloc] init];
33 if (self = [super init])
35 idleUPP = NewAEIdleUPP(MyAEIdleCallback);
42 DisposeAEIdleUPP(idleUPP);
45 - (NSString*)sendAEWithRequestedKey:(NSString*)key eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
47 //Add error checking...
48 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
49 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
51 NSString *nssendString = [NSString stringWithFormat:@"'----':obj { form:'prop', want:type('prop'), seld:type('%s'), from:'null'() }", [key UTF8String]];
52 const char *sendString = [nssendString UTF8String];
53 NSString *_finalString = nil;
55 AppleEvent sendEvent, replyEvent;
58 Size resultSize, charResultSize;
60 AEBuildError buildError;
64 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
65 NSLog(@"Error getting PID of application! Exiting.");
69 //NSLog(@"_sendString: %s", sendString);
71 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
73 //[self printCarbonDesc:&sendEvent];
76 NSLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[nssendString substringToIndex:buildError.fErrorPos]);
79 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
81 //[self printCarbonDesc:&replyEvent];
84 NSLog(@"Send Error: %i",err);
88 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
89 result = malloc(resultSize);
92 NSLog(@"Error After AESizeOfParam: %i", err2);
94 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, result, resultSize, &charResultSize);
97 NSLog(@"Error After AEGetParamPtr: %i", err3);
99 _finalString = [NSString stringWithCharacters:result length:charResultSize/sizeof(unichar)];
105 AEDisposeDesc(&sendEvent);
106 AEDisposeDesc(&replyEvent);
111 - (NSString*)sendAEWithSendString:(NSString*)nssendString eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
113 //Add error checking...
114 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
115 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
117 const char *sendString = [nssendString UTF8String];
118 NSString *_finalString = nil;
120 AppleEvent sendEvent, replyEvent;
123 Size resultSize, charResultSize;
125 AEBuildError buildError;
129 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
130 NSLog(@"Error getting PID of application! Exiting.");
134 NSLog(@"_sendString: %s", sendString);
136 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
138 [self printCarbonDesc:&sendEvent];
141 NSLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[nssendString substringToIndex:buildError.fErrorPos]);
144 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
146 [self printCarbonDesc:&replyEvent];
149 NSLog(@"Send Error: %i",err);
153 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
154 result = malloc(resultSize);
157 NSLog(@"Error After AESizeOfParam: %i", err2);
159 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, result, resultSize, &charResultSize);
162 NSLog(@"Error After AEGetParamPtr: %i", err3);
164 _finalString = [NSString stringWithCharacters:result length:charResultSize/sizeof(unichar)];
170 AEDisposeDesc(&sendEvent);
171 AEDisposeDesc(&replyEvent);
176 - (long)sendAEWithRequestedKeyForNumber:(NSString*)key eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
178 //Add error checking...
179 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
180 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
182 NSString *nssendString = [NSString stringWithFormat:@"'----':obj { form:'prop', want:type('prop'), seld:type('%s'), from:'null'() }", [key UTF8String]];
183 const char *sendString = [nssendString UTF8String];
186 AppleEvent sendEvent, replyEvent;
189 Size resultSize, charResultSize;
191 AEBuildError buildError;
195 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
196 NSLog(@"Error getting PID of application! Exiting.");
200 //NSLog(@"_sendString: %s", sendString);
202 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
204 //[self printCarbonDesc:&sendEvent];
207 NSLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[nssendString substringToIndex:buildError.fErrorPos]);
210 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
212 [self printCarbonDesc:&replyEvent];
215 NSLog(@"Send Error: %i",err);
217 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
220 NSLog(@"Error After AESizeOfParam: %i", err2);
222 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, &result, resultSize, &charResultSize);
225 NSLog(@"Error After AEGetParamPtr: %i", err3);
230 AEDisposeDesc(&sendEvent);
231 AEDisposeDesc(&replyEvent);
236 - (NSString*)sendTwoTierAEWithRequestedKey:(NSString*)key fromObjectByKey:(NSString*)object eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
238 //Add error checking...
239 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
240 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
242 NSString *nssendString = [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]];
243 const char *sendString = [nssendString UTF8String];
244 NSString *_finalString = nil;
246 AppleEvent sendEvent, replyEvent;
249 Size resultSize, charResultSize;
251 AEBuildError buildError;
255 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
256 NSLog(@"Error getting PID of application! Exiting.");
260 //NSLog(@"_sendString: %s", sendString);
262 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
264 //[self printCarbonDesc:&sendEvent];
267 NSLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[nssendString substringToIndex:buildError.fErrorPos]);
270 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
272 //[self printCarbonDesc:&replyEvent];
275 NSLog(@"Send Error: %i",err);
279 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
280 result = malloc(resultSize);
283 NSLog(@"Error After AESizeOfParam: %i", err2);
285 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, result, resultSize, &charResultSize);
288 NSLog(@"Error After AEGetParamPtr: %i", err3);
290 _finalString = [NSString stringWithCharacters:result length:charResultSize/sizeof(unichar)];
296 AEDisposeDesc(&sendEvent);
297 AEDisposeDesc(&replyEvent);
302 - (long)sendTwoTierAEWithRequestedKeyForNumber:(NSString*)key fromObjectByKey:(NSString*)object eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
304 //Add error checking...
305 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
306 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
308 NSString *nssendString = [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]];
309 const char *sendString = [nssendString UTF8String];
312 AppleEvent sendEvent, replyEvent;
315 Size resultSize, charResultSize;
317 AEBuildError buildError;
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 \"%@\"",(int)buildError.fError,buildError.fErrorPos,[nssendString substringToIndex:buildError.fErrorPos]);
336 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
338 [self printCarbonDesc:&replyEvent];
341 NSLog(@"Send Error: %i",err);
343 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
346 NSLog(@"Error After AESizeOfParam: %i", err2);
348 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, &result, resultSize, &charResultSize);
351 NSLog(@"Error After AEGetParamPtr: %i", err3);
356 AEDisposeDesc(&sendEvent);
357 AEDisposeDesc(&replyEvent);
362 - (NSString *)sendAEWithRequestedArray:(NSArray *)array eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
364 NSString *buildString = [NSString stringWithFormat:@"{ form:'prop', want:type('prop'), seld:type('%s'), from:'null'() }", [[array objectAtIndex:0] UTF8String]];
365 const char *sendString;
367 NSString *_finalString = nil;
369 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
370 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
372 AppleEvent sendEvent, replyEvent;
375 Size resultSize, charResultSize;
377 AEBuildError buildError;
381 for (i = 1; i < [array count]; i++) {
382 NSString *from = [NSString stringWithFormat:@"{ form:'prop', want:type('prop'), seld:type('%s'), from:obj %@ }",
383 [[array objectAtIndex:i] UTF8String], buildString];
386 buildString = [@"'----':obj " stringByAppendingString:buildString];
387 sendString = [buildString UTF8String];
389 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
390 NSLog(@"Error getting PID of application! Exiting.");
394 //NSLog(@"_sendString: %s", sendString);
396 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
398 //[self printCarbonDesc:&sendEvent];
401 NSLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[buildString substringToIndex:buildError.fErrorPos]);
404 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
406 //[self printCarbonDesc:&replyEvent];
409 NSLog(@"Send Error: %i",err);
413 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
414 result=malloc(resultSize);
417 NSLog(@"Error After AESizeOfParam: %i", err2);
419 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, result, resultSize, &charResultSize);
421 NSLog(@"Error After AEGetParamPtr: %i", err3);
423 _finalString = [NSString stringWithCharacters:result length:charResultSize/sizeof(unichar)];
431 - (void)sendAEWithEventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
433 //Add error checking...
434 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
435 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
437 AppleEvent event, reply;
439 //AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, kAnyTransactionID, &event, nil, "");
440 AECreateDesc(typeProcessSerialNumber,(ProcessSerialNumber*)&psn,sizeof(ProcessSerialNumber),&dest);
441 AECreateAppleEvent(eClass,eID,&dest,kAutoGenerateReturnID,kAnyTransactionID,&event);
442 AESend(&event, &reply, kAENoReply, kAENormalPriority, kAEDefaultTimeout, idleUPP, nil);
444 AEDisposeDesc(&dest);
445 AEDisposeDesc(&event);
446 AEDisposeDesc(&reply);
449 - (void)printCarbonDesc:(AEDesc*)desc {
451 AEPrintDescToHandle(desc,&xx);
452 NSLog(@"Handle: %s", *xx);
457 - (AEArrayDataPointer)sendAEWithRequestedKeyForArray:(NSString*)key eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
459 //Add error checking...
460 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
461 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
463 NSString *nssendString = [NSString stringWithFormat:@"'----':obj { form:'indx', want:'%s', seld:abso($616C6C20$), from:'null'() }", [key UTF8String]];
464 const char *sendString = [nssendString UTF8String];
465 AEArrayDataPointer result = nil;
467 AppleEvent sendEvent, replyEvent;
470 AEBuildError buildError;
473 //NSLog(@"_sendString: %s", sendString);
475 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
477 //[self printCarbonDesc:&sendEvent];
480 NSLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[nssendString substringToIndex:buildError.fErrorPos]);
483 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
485 //[self printCarbonDesc:&replyEvent];
488 NSLog(@"Send Error: %i",err);
490 SInt32 count, resultCount;
492 AECountItems(&replyEvent,&count);
493 result=malloc(sizeof(AEDesc)*count);
494 AEGetArray(&replyEvent, kAEDescArray, result, sizeof(AEDesc)*count, NULL, NULL, &resultCount);
499 AEDisposeDesc(&sendEvent);
500 AEDisposeDesc(&replyEvent);
505 - (long)sendAEWithSendStringForNumber:(NSString*)string eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
507 //Add error checking...
508 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
509 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
511 const char *sendString = [string UTF8String];
514 AppleEvent sendEvent, replyEvent;
517 Size resultSize, charResultSize;
519 AEBuildError buildError;
523 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
524 NSLog(@"Error getting PID of application! Exiting.");
528 //NSLog(@"_sendString: %s", sendString);
530 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
532 [self printCarbonDesc:&sendEvent];
535 NSLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[string substringToIndex:buildError.fErrorPos]);
538 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
540 [self printCarbonDesc:&replyEvent];
543 NSLog(@"Send Error: %i",err);
545 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
548 NSLog(@"Error After AESizeOfParam: %i", err2);
550 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, &result, resultSize, &charResultSize);
553 NSLog(@"Error After AEGetParamPtr: %i", err3);
558 AEDisposeDesc(&sendEvent);
559 AEDisposeDesc(&replyEvent);
564 - (AEArrayDataPointer)sendAEWithSendStringForArray:(NSString*)string eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
566 //Add error checking...
567 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
568 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
570 const char *sendString = [string UTF8String];
571 AEArrayDataPointer result = NULL;
573 AppleEvent sendEvent, replyEvent;
575 AEBuildError buildError;
578 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
579 NSLog(@"Error getting PID of application! Exiting.");
583 //NSLog(@"_sendString: %s", sendString);
585 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
587 //[self printCarbonDesc:&sendEvent];
590 NSLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[string substringToIndex:buildError.fErrorPos]);
593 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
595 [self printCarbonDesc:&replyEvent];
598 NSLog(@"Send Error: %i",err);
600 SInt32 count, resultCount;
602 AECountItems(&replyEvent,&count);
603 result=malloc(sizeof(AEDesc)*count);
604 AEGetArray(&replyEvent, kAEDescArray, result, sizeof(AEDesc)*count, NULL, NULL, &resultCount);
609 AEDisposeDesc(&sendEvent);
610 AEDisposeDesc(&replyEvent);