1 #import "ITAppleEventCenter.h"
2 //oh, wait, i forgot i had this open.
3 static Boolean MyAEIdleCallback (
4 EventRecord * theEvent,
12 @implementation ITAppleEventCenter
14 static ITAppleEventCenter *_sharedAECenter = nil;
18 if( _sharedAECenter ) {
19 return _sharedAECenter;
21 return _sharedAECenter = [[ITAppleEventCenter alloc] init];
27 if (self = [super init]) {
28 idleUPP = NewAEIdleUPP(MyAEIdleCallback);
35 DisposeAEIdleUPP(idleUPP);
38 - (NSString*)sendAEWithRequestedKey:(NSString*)key eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
40 //Add error checking...
41 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
42 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
44 NSString *nssendString = [NSString stringWithFormat:@"'----':obj { form:'prop', want:type('prop'), seld:type('%s'), from:'null'() }", [key UTF8String]];
45 const char *sendString = [nssendString UTF8String];
46 NSString *_finalString = nil;
48 AppleEvent sendEvent, replyEvent;
51 Size resultSize, charResultSize;
53 AEBuildError buildError;
55 OSErr berr, err2, err3;
57 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
58 //NSLog(@"Error getting PID of application! Exiting.");
62 //NSLog(@"_sendString: %s", sendString);
64 berr = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
65 //joe add the ITFDEBUG stuff back?
66 //[self printCarbonDesc:&sendEvent];
69 //NSLog(@"%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 //NSLog(@"Send Error: %i",err);
81 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
82 result = malloc(resultSize);
85 //NSLog(@"Error After AESizeOfParam: %i", err2);
87 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, result, resultSize, &charResultSize);
90 //NSLog(@"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 //NSLog(@"Error getting PID of application! Exiting.");
128 //NSLog(@"_sendString: %s", sendString);
130 berr = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
131 NSLog(@"sending...");
132 if (!berr) [self printCarbonDesc:&sendEvent];
135 //NSLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[nssendString substringToIndex:buildError.fErrorPos]);
138 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
139 NSLog(@"replying...");
140 if (!err) [self printCarbonDesc:&replyEvent];
143 //NSLog(@"Send Error: %i",err);
147 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
148 result = malloc(resultSize);
151 //NSLog(@"Error After AESizeOfParam: %i", err2);
153 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, result, resultSize, &charResultSize);
156 //NSLog(@"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 //NSLog(@"Error getting PID of application! Exiting.");
195 //NSLog(@"_sendString: %s", sendString);
197 berr = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
199 //[self printCarbonDesc:&sendEvent];
202 //NSLog(@"%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 //NSLog(@"Send Error: %i",err);
212 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
215 //NSLog(@"Error After AESizeOfParam: %i", err2);
217 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, &result, resultSize, &charResultSize);
220 //NSLog(@"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 //Add error checking...
234 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
235 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
238 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]];
239 const char *sendString = [nssendString UTF8String];
240 NSString *_finalString = nil;
242 AppleEvent sendEvent, replyEvent;
245 Size resultSize, charResultSize;
247 AEBuildError buildError;
251 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
252 //NSLog(@"Error getting PID of application! Exiting.");
256 //NSLog(@"_sendString: %s", sendString);
258 berr = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
260 //[self printCarbonDesc:&sendEvent];
263 //NSLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[nssendString substringToIndex:buildError.fErrorPos]);
266 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
268 //[self printCarbonDesc:&replyEvent];
271 //NSLog(@"Send Error: %i",err);
275 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
276 result = malloc(resultSize);
279 //NSLog(@"Error After AESizeOfParam: %i", err2);
281 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, result, resultSize, &charResultSize);
284 //NSLog(@"Error After AEGetParamPtr: %i", err3);
286 _finalString = [NSString stringWithCharacters:result length:charResultSize/sizeof(unichar)];
292 if (!berr) AEDisposeDesc(&sendEvent);
293 if (!err) AEDisposeDesc(&replyEvent);
298 - (long)sendTwoTierAEWithRequestedKeyForNumber:(NSString*)key fromObjectByKey:(NSString*)object eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
300 //Add error checking...
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 //NSLog(@"Error getting PID of application! Exiting.");
323 // //NSLog(@"_sendString: %s", sendString);
325 berr = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
327 //[self printCarbonDesc:&sendEvent];
330 //NSLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[nssendString substringToIndex:buildError.fErrorPos]);
333 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
335 //[self printCarbonDesc:&replyEvent];
338 //NSLog(@"Send Error: %i",err);
340 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
343 //NSLog(@"Error After AESizeOfParam: %i", err2);
345 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, &result, resultSize, &charResultSize);
348 //NSLog(@"Error After AEGetParamPtr: %i", err3);
353 if (!berr) AEDisposeDesc(&sendEvent);
354 if (!err) AEDisposeDesc(&replyEvent);
359 - (NSString *)sendAEWithRequestedArray:(NSArray *)array eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
361 NSString *buildString = [NSString stringWithFormat:@"{ form:'prop', want:type('prop'), seld:type('%s'), from:'null'() }", [[array objectAtIndex:0] UTF8String]];
362 const char *sendString;
364 NSString *_finalString = nil;
367 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
368 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
370 AppleEvent sendEvent, replyEvent;
373 Size resultSize, charResultSize;
375 AEBuildError buildError;
379 for (i = 1; i < [array count]; i++) {
380 NSString *from = [NSString stringWithFormat:@"{ form:'prop', want:type('prop'), seld:type('%s'), from:obj %@ }",
381 [[array objectAtIndex:i] UTF8String], buildString];
384 buildString = [@"'----':obj " stringByAppendingString:buildString];
385 sendString = [buildString UTF8String];
387 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
388 //NSLog(@"Error getting PID of application! Exiting.");
392 //NSLog(@"_sendString: %s", sendString);
394 berr = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
396 //[self printCarbonDesc:&sendEvent];
399 //NSLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[buildString substringToIndex:buildError.fErrorPos]);
402 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
404 //[self printCarbonDesc:&replyEvent];
407 //NSLog(@"Send Error: %i",err);
411 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
412 result=malloc(resultSize);
415 //NSLog(@"Error After AESizeOfParam: %i", err2);
417 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, result, resultSize, &charResultSize);
419 //NSLog(@"Error After AEGetParamPtr: %i", err3);
421 _finalString = [NSString stringWithCharacters:result length:charResultSize/sizeof(unichar)];
426 if (!berr) AEDisposeDesc(&sendEvent);
427 if (!err) AEDisposeDesc(&replyEvent);
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]);
439 AppleEvent event, reply;
440 OSStatus cerr,cerr2,err;
441 //AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, kAnyTransactionID, &event, nil, "");
442 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
443 //NSLog(@"Error getting PID of application! Exiting.");
446 cerr = AECreateDesc(typeProcessSerialNumber,(ProcessSerialNumber*)&psn,sizeof(ProcessSerialNumber),&dest);
447 cerr2 = AECreateAppleEvent(eClass,eID,&dest,kAutoGenerateReturnID,kAnyTransactionID,&event);
448 //[self printCarbonDesc:&event];
449 err = AESend(&event, &reply, kAENoReply, kAENormalPriority, kAEDefaultTimeout, idleUPP, nil);
450 //[self printCarbonDesc:&reply];
451 if (!cerr2) AEDisposeDesc(&dest);
452 if (!cerr) AEDisposeDesc(&event);
453 if (!err) AEDisposeDesc(&reply);
456 - (void)printCarbonDesc:(AEDesc*)desc {
458 AEPrintDescToHandle(desc,&xx);
459 NSLog(@"Handle: %s", *xx);
464 - (AEArrayDataPointer)sendAEWithRequestedKeyForArray:(NSString*)key eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
466 //Add error checking...
467 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
468 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
470 NSString *nssendString = [NSString stringWithFormat:@"'----':obj { form:'indx', want:'%s', seld:abso($616C6C20$), from:'null'() }", [key UTF8String]];
471 const char *sendString = [nssendString UTF8String];
472 AEArrayDataPointer result = nil;
475 AppleEvent sendEvent, replyEvent;
478 AEBuildError buildError;
481 //NSLog(@"_sendString: %s", sendString);
482 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
483 //NSLog(@"Error getting PID of application! Exiting.");
486 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
488 //[self printCarbonDesc:&sendEvent];
491 //NSLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[nssendString substringToIndex:buildError.fErrorPos]);
495 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
497 //[self printCarbonDesc:&replyEvent];
500 //NSLog(@"Send Error: %i",err);
502 SInt32 count, resultCount;
504 AECountItems(&replyEvent,&count);
505 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 //NSLog(@"Error getting PID of application! Exiting.");
541 //NSLog(@"_sendString: %s", sendString);
543 berr = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
545 //[self printCarbonDesc:&sendEvent];
548 //NSLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[string substringToIndex:buildError.fErrorPos]);
551 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
553 //[self printCarbonDesc:&replyEvent];
556 //NSLog(@"Send Error: %i",err);
558 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
561 //NSLog(@"Error After AESizeOfParam: %i", err2);
563 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, &result, resultSize, &charResultSize);
566 //NSLog(@"Error After AEGetParamPtr: %i", err3);
572 if (!berr) AEDisposeDesc(&sendEvent);
573 if (!err) AEDisposeDesc(&replyEvent);
574 //NSLog(@"waffles say %d",result);
578 - (AEArrayDataPointer)sendAEWithSendStringForArray:(NSString*)string eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
580 //Add error checking...
581 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
582 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
585 const char *sendString = [string UTF8String];
586 AEArrayDataPointer result = NULL;
588 AppleEvent sendEvent, replyEvent;
590 AEBuildError buildError;
593 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
594 //NSLog(@"Error getting PID of application! Exiting.");
598 //NSLog(@"_sendString: %s", sendString);
600 berr = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
602 //[self printCarbonDesc:&sendEvent];
605 //NSLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[string substringToIndex:buildError.fErrorPos]);
608 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
610 //[self printCarbonDesc:&replyEvent];
613 //NSLog(@"Send Error: %i",err);
615 SInt32 count, resultCount;
617 AECountItems(&replyEvent,&count);
618 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);