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! Exiting.");
63 ITDebugLog(@"_sendString: %s", sendString);
65 berr = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
66 [self printCarbonDesc:&sendEvent];
69 ITDebugLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[nssendString substringToIndex:buildError.fErrorPos]);
72 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
74 [self printCarbonDesc:&replyEvent];
77 ITDebugLog(@"Send Error: %i",err);
81 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
82 result = malloc(resultSize);
85 ITDebugLog(@"Error After AESizeOfParam: %i", err2);
87 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, result, resultSize, &charResultSize);
90 ITDebugLog(@"Error After AEGetParamPtr: %i", err3);
92 _finalString = [NSString stringWithCharacters:result length:charResultSize/sizeof(unichar)];
98 if (!berr) AEDisposeDesc(&sendEvent);
99 if (!err) AEDisposeDesc(&replyEvent);
104 - (NSString*)sendAEWithSendString:(NSString*)nssendString eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
106 //Add error checking...
107 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
108 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
111 const char *sendString = [nssendString UTF8String];
112 NSString *_finalString = nil;
114 AppleEvent sendEvent, replyEvent;
117 Size resultSize, charResultSize;
119 AEBuildError buildError;
123 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
124 ITDebugLog(@"Error getting PID of application! Exiting.");
128 ITDebugLog(@"_sendString: %s", sendString);
130 berr = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
131 ITDebugLog(@"sending...");
132 if (!berr) [self printCarbonDesc:&sendEvent];
135 ITDebugLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[nssendString substringToIndex:buildError.fErrorPos]);
138 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
139 ITDebugLog(@"replying...");
140 if (!err) [self printCarbonDesc:&replyEvent];
143 ITDebugLog(@"Send Error: %i",err);
147 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
148 result = malloc(resultSize);
151 ITDebugLog(@"Error After AESizeOfParam: %i", err2);
153 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, result, resultSize, &charResultSize);
156 ITDebugLog(@"Error After AEGetParamPtr: %i", err3);
158 _finalString = [NSString stringWithCharacters:result length:charResultSize/sizeof(unichar)];
164 if (!berr) AEDisposeDesc(&sendEvent);
165 if (!err) AEDisposeDesc(&replyEvent);
170 - (long)sendAEWithRequestedKeyForNumber:(NSString*)key eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
172 //Add error checking...
173 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
174 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
177 NSString *nssendString = [NSString stringWithFormat:@"'----':obj { form:'prop', want:type('prop'), seld:type('%s'), from:'null'() }", [key UTF8String]];
178 const char *sendString = [nssendString UTF8String];
181 AppleEvent sendEvent, replyEvent;
184 Size resultSize, charResultSize;
186 AEBuildError buildError;
188 OSErr berr, err2, err3;
190 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
191 ITDebugLog(@"Error getting PID of application! Exiting.");
195 ITDebugLog(@"_sendString: %s", sendString);
197 berr = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
199 [self printCarbonDesc:&sendEvent];
202 ITDebugLog(@"%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! Exiting.");
255 ITDebugLog(@"_sendString: %s", sendString);
257 berr = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
259 [self printCarbonDesc:&sendEvent];
262 ITDebugLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[nssendString substringToIndex:buildError.fErrorPos]);
265 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
267 [self printCarbonDesc:&replyEvent];
270 ITDebugLog(@"Send Error: %i",err);
274 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
275 result = malloc(resultSize);
278 ITDebugLog(@"Error After AESizeOfParam: %i", err2);
280 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, result, resultSize, &charResultSize);
283 ITDebugLog(@"Error After AEGetParamPtr: %i", err3);
285 _finalString = [NSString stringWithCharacters:result length:charResultSize/sizeof(unichar)];
291 if (!berr) AEDisposeDesc(&sendEvent);
292 if (!err) AEDisposeDesc(&replyEvent);
297 - (long)sendTwoTierAEWithRequestedKeyForNumber:(NSString*)key fromObjectByKey:(NSString*)object eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
299 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
300 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
302 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]];
303 const char *sendString = [nssendString UTF8String];
306 AppleEvent sendEvent, replyEvent;
310 Size resultSize, charResultSize;
312 AEBuildError buildError;
316 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
317 ITDebugLog(@"Error getting PID of application! Exiting.");
321 ITDebugLog(@"_sendString: %s", sendString);
323 berr = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
325 [self printCarbonDesc:&sendEvent];
328 ITDebugLog(@"%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! Exiting.");
390 ITDebugLog(@"_sendString: %s", sendString);
392 berr = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
394 [self printCarbonDesc:&sendEvent];
397 ITDebugLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[buildString substringToIndex:buildError.fErrorPos]);
400 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
402 [self printCarbonDesc:&replyEvent];
405 ITDebugLog(@"Send Error: %i",err);
409 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
410 result=malloc(resultSize);
413 ITDebugLog(@"Error After AESizeOfParam: %i", err2);
415 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, result, resultSize, &charResultSize);
417 ITDebugLog(@"Error After AEGetParamPtr: %i", err3);
419 _finalString = [NSString stringWithCharacters:result length:charResultSize/sizeof(unichar)];
424 if (!berr) AEDisposeDesc(&sendEvent);
425 if (!err) AEDisposeDesc(&replyEvent);
429 - (void)sendAEWithEventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
431 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
432 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
436 AppleEvent event, reply;
437 OSStatus cerr,cerr2,err;
438 //AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, kAnyTransactionID, &event, nil, "");
439 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
440 ITDebugLog(@"Error getting PID of application! Exiting.");
443 cerr = AECreateDesc(typeProcessSerialNumber,(ProcessSerialNumber*)&psn,sizeof(ProcessSerialNumber),&dest);
444 cerr2 = AECreateAppleEvent(eClass,eID,&dest,kAutoGenerateReturnID,kAnyTransactionID,&event);
445 [self printCarbonDesc:&event];
446 err = AESend(&event, &reply, kAENoReply, kAENormalPriority, kAEDefaultTimeout, idleUPP, nil);
447 [self printCarbonDesc:&reply];
448 if (!cerr2) AEDisposeDesc(&dest);
449 if (!cerr) AEDisposeDesc(&event);
450 if (!err) AEDisposeDesc(&reply);
453 - (void)printCarbonDesc:(AEDesc*)desc {
455 AEPrintDescToHandle(desc,&xx);
456 ITDebugLog(@"Handle: %s", *xx);
461 - (AEArrayDataPointer)sendAEWithRequestedKeyForArray:(NSString*)key eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
463 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
464 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
466 NSString *nssendString = [NSString stringWithFormat:@"'----':obj { form:'indx', want:'%s', seld:abso($616C6C20$), from:'null'() }", [key UTF8String]];
467 const char *sendString = [nssendString UTF8String];
468 AEArrayDataPointer result = nil;
471 AppleEvent sendEvent, replyEvent;
474 AEBuildError buildError;
477 ITDebugLog(@"_sendString: %s", sendString);
478 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
479 ITDebugLog(@"Error getting PID of application! Exiting.");
482 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
484 [self printCarbonDesc:&sendEvent];
487 ITDebugLog(@"%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 result=malloc(sizeof(AEDesc)*count);
502 AEGetArray(&replyEvent, kAEDescArray, result, sizeof(AEDesc)*count, NULL, NULL, &resultCount);
507 AEDisposeDesc(&sendEvent);
508 AEDisposeDesc(&replyEvent);
513 - (long)sendAEWithSendStringForNumber:(NSString*)string eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
515 //Add error checking...
516 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
517 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
519 const char *sendString = [string UTF8String];
523 AppleEvent sendEvent, replyEvent;
526 Size resultSize, charResultSize;
528 AEBuildError buildError;
532 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
533 ITDebugLog(@"Error getting PID of application! Exiting.");
537 ITDebugLog(@"_sendString: %s", sendString);
539 berr = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
541 [self printCarbonDesc:&sendEvent];
544 ITDebugLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[string substringToIndex:buildError.fErrorPos]);
547 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
549 [self printCarbonDesc:&replyEvent];
552 ITDebugLog(@"Send Error: %i",err);
554 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
557 ITDebugLog(@"Error After AESizeOfParam: %i", err2);
559 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, &result, resultSize, &charResultSize);
562 ITDebugLog(@"Error After AEGetParamPtr: %i", err3);
568 if (!berr) AEDisposeDesc(&sendEvent);
569 if (!err) AEDisposeDesc(&replyEvent);
570 ITDebugLog(@"waffles say %d",result);
574 - (AEArrayDataPointer)sendAEWithSendStringForArray:(NSString*)string eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
576 //Add error checking...
577 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
578 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
581 const char *sendString = [string UTF8String];
582 AEArrayDataPointer result = NULL;
584 AppleEvent sendEvent, replyEvent;
586 AEBuildError buildError;
589 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
590 ITDebugLog(@"Error getting PID of application! Exiting.");
594 ITDebugLog(@"_sendString: %s", sendString);
596 berr = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
598 [self printCarbonDesc:&sendEvent];
601 ITDebugLog(@"%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 result=malloc(sizeof(AEDesc)*count);
615 AEGetArray(&replyEvent, kAEDescArray, result, sizeof(AEDesc)*count, NULL, NULL, &resultCount);
620 if (!berr) AEDisposeDesc(&sendEvent);
621 if (!err) AEDisposeDesc(&replyEvent);