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;
51 // Variables for error checking
52 AEBuildError buildError;
59 // ^ Most pointless comment EVAR!
60 // ^^ Nope, that one is
62 if (GetProcessPID(&psn, &pid) == noErr) {
64 NSLog(@"Process doesn't exist! Exiting.");
68 NSLog(@"Error getting PID of application to send to! Exiting.");
72 NSLog(@"_sendString: %s", sendString);
74 // The problem is, if I use eClass and eID in place of 'core' and 'getd' respectively, It won't build a valid AppleEvent :'(
75 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
77 [self printCarbonDesc:&sendEvent];
80 NSLog(@"%d:%d at \"%@\"",buildError.fError,buildError.fErrorPos,[sendString substringToIndex:buildError.fErrorPos]);
83 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, upp, NULL);
85 [self printCarbonDesc:&replyEvent];
88 NSLog(@"Send Error: %i",err);
93 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
94 result=malloc(resultSize);
95 //err2 = AEGetParamDesc(&replyEvent, keyDirectObject, typeWildCard, &replyDesc);
97 if (err2) { NSLog(@"Error After AESizeOfParam: %i", err2); } else {
98 //if (err2) { NSLog(@"Error After AEGetParamDesc: %i", err2); } else {
100 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, result, resultSize, &charResultSize);
101 //length = AEGetDescDataSize(&replyDesc);
102 //buffer = malloc(length);
104 //err3 = AEGetDescData(&replyDesc, buffer, length);
106 if (err3) { NSLog(@"Error After AEGetParamPtr: %i", err3); } else {
108 // if (err3) { NSLog(@"Error After AEGetDescData: %i", err3); } else {
110 _finalString = [[NSString stringWithCharacters:result length:charResultSize/sizeof(unichar)] copy];
112 /* _finalString = [NSString stringWithCString:buffer length:length];
114 if ( (! [_finalString isEqualToString:@""]) &&
115 ([_finalString characterAtIndex:0] == '\"') &&
116 ([_finalString characterAtIndex:[_finalString length] - 1] == '\"') ) {
117 _finalString = [_finalString substringWithRange:NSMakeRange(1, [_finalString length] - 2)];
126 //NSLog(@"Result Size: %i", charResultSize);
127 //NSLog(@"Result: %c", charResult);
129 AEDisposeDesc(&sendEvent);
130 AEDisposeDesc(&replyEvent);
135 - (void)printCarbonDesc:(AEDesc*)desc {
137 AEPrintDescToHandle(desc,&xx);
138 NSLog(@"Handle: %s", *xx);