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;
57 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
58 //NSLog(@"Error getting PID of application! Exiting.");
62 //NSLog(@"_sendString: %s", sendString);
64 err = 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 AEDisposeDesc(&sendEvent);
99 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 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
132 //[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);
140 //[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 AEDisposeDesc(&sendEvent);
165 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;
190 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
191 //NSLog(@"Error getting PID of application! Exiting.");
195 //NSLog(@"_sendString: %s", sendString);
197 err = 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 AEDisposeDesc(&sendEvent);
226 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 err = 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 AEDisposeDesc(&sendEvent);
293 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 err = 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 AEDisposeDesc(&sendEvent);
354 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 err = 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)];
429 - (void)sendAEWithEventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
431 //Add error checking...
432 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
433 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
437 AppleEvent event, reply;
439 //AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, kAnyTransactionID, &event, nil, "");
440 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
441 //NSLog(@"Error getting PID of application! Exiting.");
444 AECreateDesc(typeProcessSerialNumber,(ProcessSerialNumber*)&psn,sizeof(ProcessSerialNumber),&dest);
445 AECreateAppleEvent(eClass,eID,&dest,kAutoGenerateReturnID,kAnyTransactionID,&event);
446 //[self printCarbonDesc:&event];
447 AESend(&event, &reply, kAENoReply, kAENormalPriority, kAEDefaultTimeout, idleUPP, nil);
448 //[self printCarbonDesc:&reply];
449 AEDisposeDesc(&dest);
450 AEDisposeDesc(&event);
451 AEDisposeDesc(&reply);
454 - (void)printCarbonDesc:(AEDesc*)desc {
456 AEPrintDescToHandle(desc,&xx);
457 NSLog(@"Handle: %s", *xx);
462 - (AEArrayDataPointer)sendAEWithRequestedKeyForArray:(NSString*)key eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
464 //Add error checking...
465 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
466 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
468 NSString *nssendString = [NSString stringWithFormat:@"'----':obj { form:'indx', want:'%s', seld:abso($616C6C20$), from:'null'() }", [key UTF8String]];
469 const char *sendString = [nssendString UTF8String];
470 AEArrayDataPointer result = nil;
473 AppleEvent sendEvent, replyEvent;
476 AEBuildError buildError;
479 //NSLog(@"_sendString: %s", sendString);
480 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
481 //NSLog(@"Error getting PID of application! Exiting.");
484 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
486 //[self printCarbonDesc:&sendEvent];
489 //NSLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[nssendString substringToIndex:buildError.fErrorPos]);
493 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
495 //[self printCarbonDesc:&replyEvent];
498 //NSLog(@"Send Error: %i",err);
500 SInt32 count, resultCount;
502 AECountItems(&replyEvent,&count);
503 result=malloc(sizeof(AEDesc)*count);
504 AEGetArray(&replyEvent, kAEDescArray, result, sizeof(AEDesc)*count, NULL, NULL, &resultCount);
509 AEDisposeDesc(&sendEvent);
510 AEDisposeDesc(&replyEvent);
515 - (long)sendAEWithSendStringForNumber:(NSString*)string eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
517 //Add error checking...
518 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
519 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
521 const char *sendString = [string UTF8String];
525 AppleEvent sendEvent, replyEvent;
528 Size resultSize, charResultSize;
530 AEBuildError buildError;
534 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
535 //NSLog(@"Error getting PID of application! Exiting.");
539 //NSLog(@"_sendString: %s", sendString);
541 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
543 //[self printCarbonDesc:&sendEvent];
546 //NSLog(@"%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 //NSLog(@"Send Error: %i",err);
556 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
559 //NSLog(@"Error After AESizeOfParam: %i", err2);
561 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, &result, resultSize, &charResultSize);
564 //NSLog(@"Error After AEGetParamPtr: %i", err3);
570 AEDisposeDesc(&sendEvent);
571 AEDisposeDesc(&replyEvent);
572 //NSLog(@"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 //NSLog(@"Error getting PID of application! Exiting.");
596 //NSLog(@"_sendString: %s", sendString);
598 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
600 //[self printCarbonDesc:&sendEvent];
603 //NSLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[string substringToIndex:buildError.fErrorPos]);
606 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
608 //[self printCarbonDesc:&replyEvent];
611 //NSLog(@"Send Error: %i",err);
613 SInt32 count, resultCount;
615 AECountItems(&replyEvent,&count);
616 result=malloc(sizeof(AEDesc)*count);
617 AEGetArray(&replyEvent, kAEDescArray, result, sizeof(AEDesc)*count, NULL, NULL, &resultCount);
622 AEDisposeDesc(&sendEvent);
623 AEDisposeDesc(&replyEvent);