1 #import "ITAppleEventCenter.h"
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])
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;
58 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
59 //NSLog(@"Error getting PID of application! Exiting.");
63 //NSLog(@"_sendString: %s", sendString);
65 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
67 //[self printCarbonDesc:&sendEvent];
70 //NSLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[nssendString substringToIndex:buildError.fErrorPos]);
73 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
75 //[self printCarbonDesc:&replyEvent];
78 //NSLog(@"Send Error: %i",err);
82 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
83 result = malloc(resultSize);
86 //NSLog(@"Error After AESizeOfParam: %i", err2);
88 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, result, resultSize, &charResultSize);
91 //NSLog(@"Error After AEGetParamPtr: %i", err3);
93 _finalString = [NSString stringWithCharacters:result length:charResultSize/sizeof(unichar)];
99 AEDisposeDesc(&sendEvent);
100 AEDisposeDesc(&replyEvent);
105 - (NSString*)sendAEWithSendString:(NSString*)nssendString eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
107 //Add error checking...
108 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
109 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]);
176 NSString *nssendString = [NSString stringWithFormat:@"'----':obj { form:'prop', want:type('prop'), seld:type('%s'), from:'null'() }", [key UTF8String]];
177 const char *sendString = [nssendString UTF8String];
180 AppleEvent sendEvent, replyEvent;
183 Size resultSize, charResultSize;
185 AEBuildError buildError;
189 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
190 //NSLog(@"Error getting PID of application! Exiting.");
194 //NSLog(@"_sendString: %s", sendString);
196 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
198 //[self printCarbonDesc:&sendEvent];
201 //NSLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[nssendString substringToIndex:buildError.fErrorPos]);
204 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
206 //[self printCarbonDesc:&replyEvent];
209 //NSLog(@"Send Error: %i",err);
211 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
214 //NSLog(@"Error After AESizeOfParam: %i", err2);
216 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, &result, resultSize, &charResultSize);
219 //NSLog(@"Error After AEGetParamPtr: %i", err3);
224 AEDisposeDesc(&sendEvent);
225 AEDisposeDesc(&replyEvent);
230 - (NSString*)sendTwoTierAEWithRequestedKey:(NSString*)key fromObjectByKey:(NSString*)object eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
232 //Add error checking...
233 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
234 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
236 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]];
237 const char *sendString = [nssendString UTF8String];
238 NSString *_finalString = nil;
240 AppleEvent sendEvent, replyEvent;
243 Size resultSize, charResultSize;
245 AEBuildError buildError;
249 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
250 //NSLog(@"Error getting PID of application! Exiting.");
254 //NSLog(@"_sendString: %s", sendString);
256 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
258 //[self printCarbonDesc:&sendEvent];
261 //NSLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[nssendString substringToIndex:buildError.fErrorPos]);
264 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
266 //[self printCarbonDesc:&replyEvent];
269 //NSLog(@"Send Error: %i",err);
273 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
274 result = malloc(resultSize);
277 //NSLog(@"Error After AESizeOfParam: %i", err2);
279 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, result, resultSize, &charResultSize);
282 //NSLog(@"Error After AEGetParamPtr: %i", err3);
284 _finalString = [NSString stringWithCharacters:result length:charResultSize/sizeof(unichar)];
290 AEDisposeDesc(&sendEvent);
291 AEDisposeDesc(&replyEvent);
296 - (long)sendTwoTierAEWithRequestedKeyForNumber:(NSString*)key fromObjectByKey:(NSString*)object eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
298 //Add error checking...
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;
309 Size resultSize, charResultSize;
311 AEBuildError buildError;
315 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
316 //NSLog(@"Error getting PID of application! Exiting.");
320 // //NSLog(@"_sendString: %s", sendString);
322 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
324 //[self printCarbonDesc:&sendEvent];
327 //NSLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[nssendString substringToIndex:buildError.fErrorPos]);
330 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
332 //[self printCarbonDesc:&replyEvent];
335 //NSLog(@"Send Error: %i",err);
337 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
340 //NSLog(@"Error After AESizeOfParam: %i", err2);
342 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, &result, resultSize, &charResultSize);
345 //NSLog(@"Error After AEGetParamPtr: %i", err3);
350 AEDisposeDesc(&sendEvent);
351 AEDisposeDesc(&replyEvent);
356 - (NSString *)sendAEWithRequestedArray:(NSArray *)array eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
358 NSString *buildString = [NSString stringWithFormat:@"{ form:'prop', want:type('prop'), seld:type('%s'), from:'null'() }", [[array objectAtIndex:0] UTF8String]];
359 const char *sendString;
361 NSString *_finalString = nil;
363 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
364 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
366 AppleEvent sendEvent, replyEvent;
369 Size resultSize, charResultSize;
371 AEBuildError buildError;
375 for (i = 1; i < [array count]; i++) {
376 NSString *from = [NSString stringWithFormat:@"{ form:'prop', want:type('prop'), seld:type('%s'), from:obj %@ }",
377 [[array objectAtIndex:i] UTF8String], buildString];
380 buildString = [@"'----':obj " stringByAppendingString:buildString];
381 sendString = [buildString UTF8String];
383 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
384 //NSLog(@"Error getting PID of application! Exiting.");
388 //NSLog(@"_sendString: %s", sendString);
390 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
392 //[self printCarbonDesc:&sendEvent];
395 //NSLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[buildString substringToIndex:buildError.fErrorPos]);
398 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
400 //[self printCarbonDesc:&replyEvent];
403 //NSLog(@"Send Error: %i",err);
407 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
408 result=malloc(resultSize);
411 //NSLog(@"Error After AESizeOfParam: %i", err2);
413 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, result, resultSize, &charResultSize);
415 //NSLog(@"Error After AEGetParamPtr: %i", err3);
417 _finalString = [NSString stringWithCharacters:result length:charResultSize/sizeof(unichar)];
425 - (void)sendAEWithEventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
427 //Add error checking...
428 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
429 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
431 AppleEvent event, reply;
433 //AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, kAnyTransactionID, &event, nil, "");
434 AECreateDesc(typeProcessSerialNumber,(ProcessSerialNumber*)&psn,sizeof(ProcessSerialNumber),&dest);
435 AECreateAppleEvent(eClass,eID,&dest,kAutoGenerateReturnID,kAnyTransactionID,&event);
436 //[self printCarbonDesc:&event];
437 AESend(&event, &reply, kAENoReply, kAENormalPriority, kAEDefaultTimeout, idleUPP, nil);
438 //[self printCarbonDesc:&reply];
439 AEDisposeDesc(&dest);
440 AEDisposeDesc(&event);
441 AEDisposeDesc(&reply);
444 - (void)printCarbonDesc:(AEDesc*)desc {
446 AEPrintDescToHandle(desc,&xx);
447 NSLog(@"Handle: %s", *xx);
452 - (AEArrayDataPointer)sendAEWithRequestedKeyForArray:(NSString*)key eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
454 //Add error checking...
455 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
456 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
458 NSString *nssendString = [NSString stringWithFormat:@"'----':obj { form:'indx', want:'%s', seld:abso($616C6C20$), from:'null'() }", [key UTF8String]];
459 const char *sendString = [nssendString UTF8String];
460 AEArrayDataPointer result = nil;
462 AppleEvent sendEvent, replyEvent;
465 AEBuildError buildError;
468 //NSLog(@"_sendString: %s", sendString);
470 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
472 //[self printCarbonDesc:&sendEvent];
475 //NSLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[nssendString substringToIndex:buildError.fErrorPos]);
478 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
480 //[self printCarbonDesc:&replyEvent];
483 //NSLog(@"Send Error: %i",err);
485 SInt32 count, resultCount;
487 AECountItems(&replyEvent,&count);
488 result=malloc(sizeof(AEDesc)*count);
489 AEGetArray(&replyEvent, kAEDescArray, result, sizeof(AEDesc)*count, NULL, NULL, &resultCount);
494 AEDisposeDesc(&sendEvent);
495 AEDisposeDesc(&replyEvent);
500 - (long)sendAEWithSendStringForNumber:(NSString*)string eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
502 //Add error checking...
503 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
504 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
506 const char *sendString = [string UTF8String];
509 AppleEvent sendEvent, replyEvent;
512 Size resultSize, charResultSize;
514 AEBuildError buildError;
518 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
519 //NSLog(@"Error getting PID of application! Exiting.");
523 //NSLog(@"_sendString: %s", sendString);
525 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
527 //[self printCarbonDesc:&sendEvent];
530 //NSLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[string substringToIndex:buildError.fErrorPos]);
533 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
535 //[self printCarbonDesc:&replyEvent];
538 //NSLog(@"Send Error: %i",err);
540 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
543 //NSLog(@"Error After AESizeOfParam: %i", err2);
545 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, &result, resultSize, &charResultSize);
548 //NSLog(@"Error After AEGetParamPtr: %i", err3);
554 AEDisposeDesc(&sendEvent);
555 AEDisposeDesc(&replyEvent);
556 //NSLog(@"waffles say %d",result);
560 - (AEArrayDataPointer)sendAEWithSendStringForArray:(NSString*)string eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
562 //Add error checking...
563 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
564 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
566 const char *sendString = [string UTF8String];
567 AEArrayDataPointer result = NULL;
569 AppleEvent sendEvent, replyEvent;
571 AEBuildError buildError;
574 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
575 //NSLog(@"Error getting PID of application! Exiting.");
579 //NSLog(@"_sendString: %s", sendString);
581 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
583 //[self printCarbonDesc:&sendEvent];
586 //NSLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[string substringToIndex:buildError.fErrorPos]);
589 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
591 //[self printCarbonDesc:&replyEvent];
594 //NSLog(@"Send Error: %i",err);
596 SInt32 count, resultCount;
598 AECountItems(&replyEvent,&count);
599 result=malloc(sizeof(AEDesc)*count);
600 AEGetArray(&replyEvent, kAEDescArray, result, sizeof(AEDesc)*count, NULL, NULL, &resultCount);
605 AEDisposeDesc(&sendEvent);
606 AEDisposeDesc(&replyEvent);