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*)sendAEWithRequestedKey:(NSString*)key eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
34 if ( (!key) || (!eventClass) || (!eventID) || (!psn) ) {
37 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
38 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
40 const char *sendString = [[NSString stringWithFormat:@"'----':obj { form:'prop', want:type('prop'), seld:type('%s'), from:'null'() }", [key UTF8String]] UTF8String];
41 NSString *_finalString;
43 AppleEvent sendEvent, replyEvent;
44 AEIdleUPP upp = NewAEIdleUPP(&MyAEIdleCallback);
48 Size resultSize, charResultSize;
51 AEBuildError buildError;
55 if (GetProcessPID(&psn, &pid) == noErr) {
57 NSLog(@"Process doesn't exist! Exiting.");
61 NSLog(@"Error getting PID of application to send to! Exiting.");
65 NSLog(@"_sendString: %s", sendString);
67 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
69 [self printCarbonDesc:&sendEvent];
72 NSLog(@"%d:%d at \"%@\"",buildError.fError,buildError.fErrorPos,[sendString substringToIndex:buildError.fErrorPos]);
75 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, upp, NULL);
77 [self printCarbonDesc:&replyEvent];
80 NSLog(@"Send Error: %i",err);
85 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
86 result=malloc(resultSize);
88 if (err2) { NSLog(@"Error After AESizeOfParam: %i", err2); } else {
90 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, result, resultSize, &charResultSize);
92 if (err3) { NSLog(@"Error After AEGetParamPtr: %i", err3); } else {
94 _finalString = [[NSString stringWithCharacters:result length:charResultSize/sizeof(unichar)] copy];
100 AEDisposeDesc(&sendEvent);
101 AEDisposeDesc(&replyEvent);
107 - (void)sendAEWithEventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
109 if ( (!eventClass) || (!eventID) || (!psn) ) {
112 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
113 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
114 AppleEvent event, reply;
116 AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, kAnyTransactionID, &event, nil, "");
118 AESend(&event, &reply, kAENoReply, kAENormalPriority, kAEDefaultTimeout, nil, nil);
120 AEDisposeDesc(&event);
121 AEDisposeDesc(&reply);
125 - (void)printCarbonDesc:(AEDesc*)desc {
127 AEPrintDescToHandle(desc,&xx);
128 NSLog(@"Handle: %s", *xx);