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 [self printCarbonDesc:&event];
443 AESend(&event, &reply, kAENoReply, kAENormalPriority, kAEDefaultTimeout, idleUPP, nil);
444 [self printCarbonDesc:&reply];
445 AEDisposeDesc(&dest);
446 AEDisposeDesc(&event);
447 AEDisposeDesc(&reply);
450 - (void)printCarbonDesc:(AEDesc*)desc {
452 AEPrintDescToHandle(desc,&xx);
453 NSLog(@"Handle: %s", *xx);
458 - (AEArrayDataPointer)sendAEWithRequestedKeyForArray:(NSString*)key eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
460 //Add error checking...
461 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
462 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
464 NSString *nssendString = [NSString stringWithFormat:@"'----':obj { form:'indx', want:'%s', seld:abso($616C6C20$), from:'null'() }", [key UTF8String]];
465 const char *sendString = [nssendString UTF8String];
466 AEArrayDataPointer result = nil;
468 AppleEvent sendEvent, replyEvent;
471 AEBuildError buildError;
474 //NSLog(@"_sendString: %s", sendString);
476 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
478 [self printCarbonDesc:&sendEvent];
481 //NSLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[nssendString substringToIndex:buildError.fErrorPos]);
484 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
486 [self printCarbonDesc:&replyEvent];
489 //NSLog(@"Send Error: %i",err);
491 SInt32 count, resultCount;
493 AECountItems(&replyEvent,&count);
494 result=malloc(sizeof(AEDesc)*count);
495 AEGetArray(&replyEvent, kAEDescArray, result, sizeof(AEDesc)*count, NULL, NULL, &resultCount);
500 AEDisposeDesc(&sendEvent);
501 AEDisposeDesc(&replyEvent);
506 - (long)sendAEWithSendStringForNumber:(NSString*)string eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
508 //Add error checking...
509 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
510 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
512 const char *sendString = [string UTF8String];
515 AppleEvent sendEvent, replyEvent;
518 Size resultSize, charResultSize;
520 AEBuildError buildError;
524 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
525 //NSLog(@"Error getting PID of application! Exiting.");
529 //NSLog(@"_sendString: %s", sendString);
531 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
533 [self printCarbonDesc:&sendEvent];
536 //NSLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[string substringToIndex:buildError.fErrorPos]);
539 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
541 [self printCarbonDesc:&replyEvent];
544 //NSLog(@"Send Error: %i",err);
546 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
549 //NSLog(@"Error After AESizeOfParam: %i", err2);
551 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, &result, resultSize, &charResultSize);
554 //NSLog(@"Error After AEGetParamPtr: %i", err3);
560 AEDisposeDesc(&sendEvent);
561 AEDisposeDesc(&replyEvent);
562 //NSLog(@"waffles say %d",result);
566 - (AEArrayDataPointer)sendAEWithSendStringForArray:(NSString*)string eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
568 //Add error checking...
569 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
570 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
572 const char *sendString = [string UTF8String];
573 AEArrayDataPointer result = NULL;
575 AppleEvent sendEvent, replyEvent;
577 AEBuildError buildError;
580 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
581 //NSLog(@"Error getting PID of application! Exiting.");
585 //NSLog(@"_sendString: %s", sendString);
587 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
589 [self printCarbonDesc:&sendEvent];
592 //NSLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[string substringToIndex:buildError.fErrorPos]);
595 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
597 [self printCarbonDesc:&replyEvent];
600 //NSLog(@"Send Error: %i",err);
602 SInt32 count, resultCount;
604 AECountItems(&replyEvent,&count);
605 result=malloc(sizeof(AEDesc)*count);
606 AEGetArray(&replyEvent, kAEDescArray, result, sizeof(AEDesc)*count, NULL, NULL, &resultCount);
611 AEDisposeDesc(&sendEvent);
612 AEDisposeDesc(&replyEvent);