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]);
110 const char *sendString = [nssendString UTF8String];
111 NSString *_finalString = nil;
113 AppleEvent sendEvent, replyEvent;
116 Size resultSize, charResultSize;
118 AEBuildError buildError;
122 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
123 //NSLog(@"Error getting PID of application! Exiting.");
127 //NSLog(@"_sendString: %s", sendString);
129 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
131 //[self printCarbonDesc:&sendEvent];
134 //NSLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[nssendString substringToIndex:buildError.fErrorPos]);
137 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
139 //[self printCarbonDesc:&replyEvent];
142 //NSLog(@"Send Error: %i",err);
146 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
147 result = malloc(resultSize);
150 //NSLog(@"Error After AESizeOfParam: %i", err2);
152 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, result, resultSize, &charResultSize);
155 //NSLog(@"Error After AEGetParamPtr: %i", err3);
157 _finalString = [NSString stringWithCharacters:result length:charResultSize/sizeof(unichar)];
163 AEDisposeDesc(&sendEvent);
164 AEDisposeDesc(&replyEvent);
169 - (long)sendAEWithRequestedKeyForNumber:(NSString*)key eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
171 //Add error checking...
172 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
173 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
175 NSString *nssendString = [NSString stringWithFormat:@"'----':obj { form:'prop', want:type('prop'), seld:type('%s'), from:'null'() }", [key UTF8String]];
176 const char *sendString = [nssendString UTF8String];
179 AppleEvent sendEvent, replyEvent;
182 Size resultSize, charResultSize;
184 AEBuildError buildError;
188 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
189 //NSLog(@"Error getting PID of application! Exiting.");
193 //NSLog(@"_sendString: %s", sendString);
195 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
197 //[self printCarbonDesc:&sendEvent];
200 //NSLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[nssendString substringToIndex:buildError.fErrorPos]);
203 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
205 //[self printCarbonDesc:&replyEvent];
208 //NSLog(@"Send Error: %i",err);
210 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
213 //NSLog(@"Error After AESizeOfParam: %i", err2);
215 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, &result, resultSize, &charResultSize);
218 //NSLog(@"Error After AEGetParamPtr: %i", err3);
223 AEDisposeDesc(&sendEvent);
224 AEDisposeDesc(&replyEvent);
229 - (NSString*)sendTwoTierAEWithRequestedKey:(NSString*)key fromObjectByKey:(NSString*)object eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
231 //Add error checking...
232 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
233 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
235 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]];
236 const char *sendString = [nssendString UTF8String];
237 NSString *_finalString = nil;
239 AppleEvent sendEvent, replyEvent;
242 Size resultSize, charResultSize;
244 AEBuildError buildError;
248 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
249 //NSLog(@"Error getting PID of application! Exiting.");
253 //NSLog(@"_sendString: %s", sendString);
255 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
257 //[self printCarbonDesc:&sendEvent];
260 //NSLog(@"%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 //NSLog(@"Send Error: %i",err);
272 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
273 result = malloc(resultSize);
276 //NSLog(@"Error After AESizeOfParam: %i", err2);
278 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, result, resultSize, &charResultSize);
281 //NSLog(@"Error After AEGetParamPtr: %i", err3);
283 _finalString = [NSString stringWithCharacters:result length:charResultSize/sizeof(unichar)];
289 AEDisposeDesc(&sendEvent);
290 AEDisposeDesc(&replyEvent);
295 - (long)sendTwoTierAEWithRequestedKeyForNumber:(NSString*)key fromObjectByKey:(NSString*)object eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
297 //Add error checking...
298 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
299 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
301 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]];
302 const char *sendString = [nssendString UTF8String];
305 AppleEvent sendEvent, replyEvent;
308 Size resultSize, charResultSize;
310 AEBuildError buildError;
314 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
315 //NSLog(@"Error getting PID of application! Exiting.");
319 // //NSLog(@"_sendString: %s", sendString);
321 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
323 //[self printCarbonDesc:&sendEvent];
326 //NSLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[nssendString substringToIndex:buildError.fErrorPos]);
329 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
331 //[self printCarbonDesc:&replyEvent];
334 //NSLog(@"Send Error: %i",err);
336 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
339 //NSLog(@"Error After AESizeOfParam: %i", err2);
341 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, &result, resultSize, &charResultSize);
344 //NSLog(@"Error After AEGetParamPtr: %i", err3);
349 AEDisposeDesc(&sendEvent);
350 AEDisposeDesc(&replyEvent);
355 - (NSString *)sendAEWithRequestedArray:(NSArray *)array eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
357 NSString *buildString = [NSString stringWithFormat:@"{ form:'prop', want:type('prop'), seld:type('%s'), from:'null'() }", [[array objectAtIndex:0] UTF8String]];
358 const char *sendString;
360 NSString *_finalString = nil;
362 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
363 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
365 AppleEvent sendEvent, replyEvent;
368 Size resultSize, charResultSize;
370 AEBuildError buildError;
374 for (i = 1; i < [array count]; i++) {
375 NSString *from = [NSString stringWithFormat:@"{ form:'prop', want:type('prop'), seld:type('%s'), from:obj %@ }",
376 [[array objectAtIndex:i] UTF8String], buildString];
379 buildString = [@"'----':obj " stringByAppendingString:buildString];
380 sendString = [buildString UTF8String];
382 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
383 //NSLog(@"Error getting PID of application! Exiting.");
387 //NSLog(@"_sendString: %s", sendString);
389 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
391 //[self printCarbonDesc:&sendEvent];
394 //NSLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[buildString substringToIndex:buildError.fErrorPos]);
397 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
399 //[self printCarbonDesc:&replyEvent];
402 //NSLog(@"Send Error: %i",err);
406 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
407 result=malloc(resultSize);
410 //NSLog(@"Error After AESizeOfParam: %i", err2);
412 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, result, resultSize, &charResultSize);
414 //NSLog(@"Error After AEGetParamPtr: %i", err3);
416 _finalString = [NSString stringWithCharacters:result length:charResultSize/sizeof(unichar)];
424 - (void)sendAEWithEventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
426 //Add error checking...
427 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
428 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
430 AppleEvent event, reply;
432 //AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, kAnyTransactionID, &event, nil, "");
433 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
434 //NSLog(@"Error getting PID of application! Exiting.");
437 AECreateDesc(typeProcessSerialNumber,(ProcessSerialNumber*)&psn,sizeof(ProcessSerialNumber),&dest);
438 AECreateAppleEvent(eClass,eID,&dest,kAutoGenerateReturnID,kAnyTransactionID,&event);
439 //[self printCarbonDesc:&event];
440 AESend(&event, &reply, kAENoReply, kAENormalPriority, kAEDefaultTimeout, idleUPP, nil);
441 //[self printCarbonDesc:&reply];
442 AEDisposeDesc(&dest);
443 AEDisposeDesc(&event);
444 AEDisposeDesc(&reply);
447 - (void)printCarbonDesc:(AEDesc*)desc {
449 AEPrintDescToHandle(desc,&xx);
450 NSLog(@"Handle: %s", *xx);
455 - (AEArrayDataPointer)sendAEWithRequestedKeyForArray:(NSString*)key eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
457 //Add error checking...
458 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
459 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
461 NSString *nssendString = [NSString stringWithFormat:@"'----':obj { form:'indx', want:'%s', seld:abso($616C6C20$), from:'null'() }", [key UTF8String]];
462 const char *sendString = [nssendString UTF8String];
463 AEArrayDataPointer result = nil;
465 AppleEvent sendEvent, replyEvent;
468 AEBuildError buildError;
471 //NSLog(@"_sendString: %s", sendString);
472 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
473 //NSLog(@"Error getting PID of application! Exiting.");
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]);
485 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
487 //[self printCarbonDesc:&replyEvent];
490 //NSLog(@"Send Error: %i",err);
492 SInt32 count, resultCount;
494 AECountItems(&replyEvent,&count);
495 result=malloc(sizeof(AEDesc)*count);
496 AEGetArray(&replyEvent, kAEDescArray, result, sizeof(AEDesc)*count, NULL, NULL, &resultCount);
501 AEDisposeDesc(&sendEvent);
502 AEDisposeDesc(&replyEvent);
507 - (long)sendAEWithSendStringForNumber:(NSString*)string eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
509 //Add error checking...
510 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
511 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
513 const char *sendString = [string UTF8String];
516 AppleEvent sendEvent, replyEvent;
519 Size resultSize, charResultSize;
521 AEBuildError buildError;
525 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
526 //NSLog(@"Error getting PID of application! Exiting.");
530 //NSLog(@"_sendString: %s", sendString);
532 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
534 //[self printCarbonDesc:&sendEvent];
537 //NSLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[string substringToIndex:buildError.fErrorPos]);
540 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
542 //[self printCarbonDesc:&replyEvent];
545 //NSLog(@"Send Error: %i",err);
547 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
550 //NSLog(@"Error After AESizeOfParam: %i", err2);
552 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, &result, resultSize, &charResultSize);
555 //NSLog(@"Error After AEGetParamPtr: %i", err3);
561 AEDisposeDesc(&sendEvent);
562 AEDisposeDesc(&replyEvent);
563 //NSLog(@"waffles say %d",result);
567 - (AEArrayDataPointer)sendAEWithSendStringForArray:(NSString*)string eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
569 //Add error checking...
570 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
571 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
573 const char *sendString = [string UTF8String];
574 AEArrayDataPointer result = NULL;
576 AppleEvent sendEvent, replyEvent;
578 AEBuildError buildError;
581 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
582 //NSLog(@"Error getting PID of application! Exiting.");
586 //NSLog(@"_sendString: %s", sendString);
588 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
590 //[self printCarbonDesc:&sendEvent];
593 //NSLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[string substringToIndex:buildError.fErrorPos]);
596 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
598 //[self printCarbonDesc:&replyEvent];
601 //NSLog(@"Send Error: %i",err);
603 SInt32 count, resultCount;
605 AECountItems(&replyEvent,&count);
606 result=malloc(sizeof(AEDesc)*count);
607 AEGetArray(&replyEvent, kAEDescArray, result, sizeof(AEDesc)*count, NULL, NULL, &resultCount);
612 AEDisposeDesc(&sendEvent);
613 AEDisposeDesc(&replyEvent);