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 != nil ) {
25 return _sharedAECenter;
27 _sharedAECenter = [[ITAppleEventCenter alloc] init];
28 return _sharedAECenter;
32 - (NSString*)runAEWithRequestedKey:(NSString*)key eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
34 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
35 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
37 const char *sendString = [[NSString stringWithFormat:@"'----':obj { form:'prop', want:type('prop'), seld:type('%s'), from:'null'() }", [key UTF8String]] UTF8String];
38 NSString *_finalString;
40 // Variables for building and sending the event
41 AppleEvent sendEvent, replyEvent;
43 AEIdleUPP upp = NewAEIdleUPP(&MyAEIdleCallback);
45 // Variables for getting the result
48 Size resultSize, charResultSize;
50 // Variables for error checking
51 AEBuildError buildError;
58 // ^ Most pointless comment EVAR!
59 // ^^ Nope, that one is
61 NSLog(@"_sendString: %s", sendString);
63 // The problem is, if I use eClass and eID in place of 'core' and 'getd' respectively, It won't build a valid AppleEvent :'(
64 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
66 [self printCarbonDesc:&sendEvent];
69 NSLog(@"%d:%d at \"%@\"",buildError.fError,buildError.fErrorPos,[sendString substringToIndex:buildError.fErrorPos]);
72 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, upp, NULL);
74 [self printCarbonDesc:&replyEvent];
77 NSLog(@"Send Error: %i",err);
82 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
83 result=malloc(resultSize);
84 //err2 = AEGetParamDesc(&replyEvent, keyDirectObject, typeWildCard, &replyDesc);
86 if (err2) { NSLog(@"Error After AESizeOfParam: %i", err2); } else {
87 //if (err2) { NSLog(@"Error After AEGetParamDesc: %i", err2); } else {
89 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, result, resultSize, &charResultSize);
90 //length = AEGetDescDataSize(&replyDesc);
91 //buffer = malloc(length);
93 //err3 = AEGetDescData(&replyDesc, buffer, length);
95 if (err3) { NSLog(@"Error After AEGetParamPtr: %i", err3); } else {
97 // if (err3) { NSLog(@"Error After AEGetDescData: %i", err3); } else {
99 _finalString = [[NSString stringWithCharacters:result length:charResultSize/sizeof(unichar)] copy];
101 /* _finalString = [NSString stringWithCString:buffer length:length];
103 if ( (! [_finalString isEqualToString:@""]) &&
104 ([_finalString characterAtIndex:0] == '\"') &&
105 ([_finalString characterAtIndex:[_finalString length] - 1] == '\"') ) {
106 _finalString = [_finalString substringWithRange:NSMakeRange(1, [_finalString length] - 2)];
115 //NSLog(@"Result Size: %i", charResultSize);
116 //NSLog(@"Result: %c", charResult);
118 AEDisposeDesc(&sendEvent);
119 AEDisposeDesc(&replyEvent);
124 - (void)printCarbonDesc:(AEDesc*)desc {
126 AEPrintDescToHandle(desc,&xx);
127 NSLog(@"Handle: %s", *xx);