1 #import "ITAppleEventCenter.h"
4 static Boolean MyAEIdleCallback (
5 EventRecord * theEvent,
13 @implementation ITAppleEventCenter
15 static ITAppleEventCenter *_sharedAECenter = nil;
19 if( _sharedAECenter ) {
20 return _sharedAECenter;
22 return _sharedAECenter = [[ITAppleEventCenter alloc] init];
28 if ( ( self = [super init] ) ) {
29 idleUPP = NewAEIdleUPP(MyAEIdleCallback);
36 DisposeAEIdleUPP(idleUPP);
39 - (NSString*)sendAEWithRequestedKey:(NSString*)key eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
41 //Add error checking...
42 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
43 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
45 NSString *nssendString = [NSString stringWithFormat:@"'----':obj { form:'prop', want:type('prop'), seld:type('%s'), from:'null'() }", [key UTF8String]];
46 const char *sendString = [nssendString UTF8String];
47 NSString *_finalString = nil;
49 AppleEvent sendEvent, replyEvent;
52 Size resultSize, charResultSize;
54 AEBuildError buildError;
56 OSErr berr, err2, err3;
58 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
59 ITDebugLog(@"Error getting PID of application.");
63 berr = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
64 [self printCarbonDesc:&sendEvent];
67 ITDebugLog(@"Error: %d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[nssendString substringToIndex:buildError.fErrorPos]);
70 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
72 [self printCarbonDesc:&replyEvent];
75 ITDebugLog(@"Send Error: %i",err);
79 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
80 if (resultSize != 0) {
81 result = malloc(resultSize);
87 ITDebugLog(@"Error After AESizeOfParam: %i", err2);
89 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, result, resultSize, &charResultSize);
92 ITDebugLog(@"Error After AEGetParamPtr: %i", err3);
94 _finalString = [NSString stringWithCharacters:result length:charResultSize/sizeof(unichar)];
100 if (!berr) AEDisposeDesc(&sendEvent);
101 if (!err) AEDisposeDesc(&replyEvent);
106 - (NSString*)sendAEWithSendString:(NSString*)nssendString eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
108 //Add error checking...
109 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
110 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
113 const char *sendString = [nssendString UTF8String];
114 NSString *_finalString = nil;
116 AppleEvent sendEvent, replyEvent;
119 Size resultSize, charResultSize;
121 AEBuildError buildError;
125 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
126 ITDebugLog(@"Error getting PID of application.");
130 berr = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
131 if (!berr) [self printCarbonDesc:&sendEvent];
134 ITDebugLog(@"Error: %d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[nssendString substringToIndex:buildError.fErrorPos]);
137 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
138 if (!err) [self printCarbonDesc:&replyEvent];
141 ITDebugLog(@"Send Error: %i",err);
145 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
146 if (resultSize != 0) {
147 result = malloc(resultSize);
153 ITDebugLog(@"Error After AESizeOfParam: %i", err2);
155 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, result, resultSize, &charResultSize);
158 ITDebugLog(@"Error After AEGetParamPtr: %i", err3);
160 _finalString = [NSString stringWithCharacters:result length:charResultSize/sizeof(unichar)];
166 if (!berr) AEDisposeDesc(&sendEvent);
167 if (!err) AEDisposeDesc(&replyEvent);
172 - (long)sendAEWithRequestedKeyForNumber:(NSString*)key eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
174 //Add error checking...
175 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
176 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
179 NSString *nssendString = [NSString stringWithFormat:@"'----':obj { form:'prop', want:type('prop'), seld:type('%s'), from:'null'() }", [key UTF8String]];
180 const char *sendString = [nssendString UTF8String];
183 AppleEvent sendEvent, replyEvent;
186 Size resultSize, charResultSize;
188 AEBuildError buildError;
190 OSErr berr, err2, err3;
192 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
193 ITDebugLog(@"Error getting PID of application.");
197 berr = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
199 [self printCarbonDesc:&sendEvent];
202 ITDebugLog(@"Error: %d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[nssendString substringToIndex:buildError.fErrorPos]);
205 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
207 [self printCarbonDesc:&replyEvent];
210 ITDebugLog(@"Send Error: %i",err);
212 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
215 ITDebugLog(@"Error After AESizeOfParam: %i", err2);
217 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, &result, resultSize, &charResultSize);
220 ITDebugLog(@"Error After AEGetParamPtr: %i", err3);
225 if (!berr) AEDisposeDesc(&sendEvent);
226 if (!err) AEDisposeDesc(&replyEvent);
231 - (NSString*)sendTwoTierAEWithRequestedKey:(NSString*)key fromObjectByKey:(NSString*)object eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
233 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
234 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
237 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]];
238 const char *sendString = [nssendString UTF8String];
239 NSString *_finalString = nil;
241 AppleEvent sendEvent, replyEvent;
244 Size resultSize, charResultSize;
246 AEBuildError buildError;
250 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
251 ITDebugLog(@"Error getting PID of application.");
255 berr = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
257 [self printCarbonDesc:&sendEvent];
260 ITDebugLog(@"Error: %d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[nssendString substringToIndex:buildError.fErrorPos]);
263 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
265 [self printCarbonDesc:&replyEvent];
268 ITDebugLog(@"Send Error: %i",err);
272 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
273 if (resultSize != 0) {
274 result = malloc(resultSize);
280 ITDebugLog(@"Error After AESizeOfParam: %i", err2);
282 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, result, resultSize, &charResultSize);
285 ITDebugLog(@"Error After AEGetParamPtr: %i", err3);
287 _finalString = [NSString stringWithCharacters:result length:charResultSize/sizeof(unichar)];
293 if (!berr) AEDisposeDesc(&sendEvent);
294 if (!err) AEDisposeDesc(&replyEvent);
299 - (long)sendTwoTierAEWithRequestedKeyForNumber:(NSString*)key fromObjectByKey:(NSString*)object eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
301 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
302 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
304 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]];
305 const char *sendString = [nssendString UTF8String];
308 AppleEvent sendEvent, replyEvent;
312 Size resultSize, charResultSize;
314 AEBuildError buildError;
318 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
319 ITDebugLog(@"Error getting PID of application.");
323 berr = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
325 [self printCarbonDesc:&sendEvent];
328 ITDebugLog(@"Error: %d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[nssendString substringToIndex:buildError.fErrorPos]);
331 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
333 [self printCarbonDesc:&replyEvent];
336 ITDebugLog(@"Send Error: %i",err);
338 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
341 ITDebugLog(@"Error After AESizeOfParam: %i", err2);
343 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, &result, resultSize, &charResultSize);
346 ITDebugLog(@"Error After AEGetParamPtr: %i", err3);
351 if (!berr) AEDisposeDesc(&sendEvent);
352 if (!err) AEDisposeDesc(&replyEvent);
357 - (NSString *)sendAEWithRequestedArray:(NSArray *)array eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
359 NSString *buildString = [NSString stringWithFormat:@"{ form:'prop', want:type('prop'), seld:type('%s'), from:'null'() }", [[array objectAtIndex:0] UTF8String]];
360 const char *sendString;
362 NSString *_finalString = nil;
365 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
366 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
368 AppleEvent sendEvent, replyEvent;
371 Size resultSize, charResultSize;
373 AEBuildError buildError;
377 for (i = 1; i < [array count]; i++) {
378 NSString *from = [NSString stringWithFormat:@"{ form:'prop', want:type('prop'), seld:type('%s'), from:obj %@ }",
379 [[array objectAtIndex:i] UTF8String], buildString];
382 buildString = [@"'----':obj " stringByAppendingString:buildString];
383 sendString = [buildString UTF8String];
385 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
386 ITDebugLog(@"Error getting PID of application.");
390 berr = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
392 [self printCarbonDesc:&sendEvent];
395 ITDebugLog(@"Error: %d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[buildString substringToIndex:buildError.fErrorPos]);
398 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
400 [self printCarbonDesc:&replyEvent];
403 ITDebugLog(@"Send Error: %i",err);
407 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
408 if (resultSize != 0) {
409 result = malloc(resultSize);
414 ITDebugLog(@"Error After AESizeOfParam: %i", err2);
416 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, result, resultSize, &charResultSize);
418 ITDebugLog(@"Error After AEGetParamPtr: %i", err3);
420 _finalString = [NSString stringWithCharacters:result length:charResultSize/sizeof(unichar)];
425 if (!berr) AEDisposeDesc(&sendEvent);
426 if (!err) AEDisposeDesc(&replyEvent);
430 - (void)sendAEWithEventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
432 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
433 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
437 AppleEvent event, reply;
438 OSStatus cerr,cerr2,err;
439 //AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, kAnyTransactionID, &event, nil, "");
440 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
441 ITDebugLog(@"Error getting PID of application.");
444 cerr = AECreateDesc(typeProcessSerialNumber,(ProcessSerialNumber*)&psn,sizeof(ProcessSerialNumber),&dest);
445 cerr2 = AECreateAppleEvent(eClass,eID,&dest,kAutoGenerateReturnID,kAnyTransactionID,&event);
446 [self printCarbonDesc:&event];
447 err = AESend(&event, &reply, kAENoReply, kAENormalPriority, kAEDefaultTimeout, idleUPP, nil);
448 [self printCarbonDesc:&reply];
449 if (!cerr2) AEDisposeDesc(&dest);
450 if (!cerr) AEDisposeDesc(&event);
451 if (!err) AEDisposeDesc(&reply);
454 - (void)printCarbonDesc:(AEDesc*)desc {
456 AEPrintDescToHandle(desc,&xx);
457 ITDebugLog(@"AE Descriptor: %s", *xx);
462 - (AEArrayDataPointer)sendAEWithRequestedKeyForArray:(NSString*)key eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
464 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
465 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
467 NSString *nssendString = [NSString stringWithFormat:@"'----':obj { form:'indx', want:'%s', seld:abso($616C6C20$), from:'null'() }", [key UTF8String]];
468 const char *sendString = [nssendString UTF8String];
469 AEArrayDataPointer result = nil;
472 AppleEvent sendEvent, replyEvent;
475 AEBuildError buildError;
478 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
479 ITDebugLog(@"Error getting PID of application.");
482 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
484 [self printCarbonDesc:&sendEvent];
487 ITDebugLog(@"Error: %d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[nssendString substringToIndex:buildError.fErrorPos]);
491 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
493 [self printCarbonDesc:&replyEvent];
496 ITDebugLog(@"Send Error: %i",err);
498 SInt32 count, resultCount;
500 AECountItems(&replyEvent,&count);
501 if ((sizeof(AEDesc)*count) != 0) {
502 result=malloc(sizeof(AEDesc)*count);
506 AEGetArray(&replyEvent, kAEDescArray, result, sizeof(AEDesc)*count, NULL, NULL, &resultCount);
511 AEDisposeDesc(&sendEvent);
512 AEDisposeDesc(&replyEvent);
517 - (long)sendAEWithSendStringForNumber:(NSString*)string eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
519 //Add error checking...
520 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
521 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
523 const char *sendString = [string UTF8String];
527 AppleEvent sendEvent, replyEvent;
530 Size resultSize, charResultSize;
532 AEBuildError buildError;
536 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
537 ITDebugLog(@"Error getting PID of application.");
541 berr = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
543 [self printCarbonDesc:&sendEvent];
546 ITDebugLog(@"Error: %d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[string substringToIndex:buildError.fErrorPos]);
549 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
551 [self printCarbonDesc:&replyEvent];
554 ITDebugLog(@"Send Error: %i",err);
556 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
559 ITDebugLog(@"Error After AESizeOfParam: %i", err2);
561 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, &result, resultSize, &charResultSize);
564 ITDebugLog(@"Error After AEGetParamPtr: %i", err3);
570 if (!berr) AEDisposeDesc(&sendEvent);
571 if (!err) AEDisposeDesc(&replyEvent);
572 ITDebugLog(@"waffles say %d",result);
576 - (AEArrayDataPointer)sendAEWithSendStringForArray:(NSString*)string eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
578 //Add error checking...
579 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
580 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
583 const char *sendString = [string UTF8String];
584 AEArrayDataPointer result = NULL;
586 AppleEvent sendEvent, replyEvent;
588 AEBuildError buildError;
591 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
592 ITDebugLog(@"Error getting PID of application.");
596 berr = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
598 [self printCarbonDesc:&sendEvent];
601 ITDebugLog(@"Error: %d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[string substringToIndex:buildError.fErrorPos]);
604 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
606 [self printCarbonDesc:&replyEvent];
609 ITDebugLog(@"Send Error: %i",err);
611 SInt32 count, resultCount;
613 AECountItems(&replyEvent,&count);
614 if ((sizeof(AEDesc)*count) != 0) {
615 result=malloc(sizeof(AEDesc)*count);
619 AEGetArray(&replyEvent, kAEDescArray, result, sizeof(AEDesc)*count, NULL, NULL, &resultCount);
624 if (!berr) AEDisposeDesc(&sendEvent);
625 if (!err) AEDisposeDesc(&replyEvent);
630 - (NSData*)sendAEWithSendStringForData:(NSString*)nssendString eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
632 //Add error checking...
633 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
634 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
637 const char *sendString = [nssendString UTF8String];
638 NSData *_finalData = nil;
640 AppleEvent sendEvent, replyEvent;
643 Size resultSize, charResultSize;
645 AEBuildError buildError;
649 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
650 ITDebugLog(@"Error getting PID of application.");
654 berr = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
655 if (!berr) [self printCarbonDesc:&sendEvent];
658 ITDebugLog(@"Error: %d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[nssendString substringToIndex:buildError.fErrorPos]);
661 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
662 if (!err) [self printCarbonDesc:&replyEvent];
665 ITDebugLog(@"Send Error: %i",err);
669 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
670 if (resultSize != 0) {
671 result = malloc(resultSize);
677 ITDebugLog(@"Error After AESizeOfParam: %i", err2);
679 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, result, resultSize, &charResultSize);
682 ITDebugLog(@"Error After AEGetParamPtr: %i", err3);
684 _finalData = [NSData dataWithBytesNoCopy:result length:charResultSize freeWhenDone:YES];
689 if (!berr) AEDisposeDesc(&sendEvent);
690 if (!err) AEDisposeDesc(&replyEvent);