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.highLongOfPSN == kNoProcess) ) {
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 (resultType == typeUInt32) {
92 if (err2) { NSLog(@"Error After AESizeOfParam: %i", err2); } else {
94 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, result, resultSize, &charResultSize);
96 if (err3) { NSLog(@"Error After AEGetParamPtr: %i", err3); } else {
98 _finalString = [[NSString stringWithCharacters:result length:charResultSize/sizeof(unichar)] copy];
104 AEDisposeDesc(&sendEvent);
105 AEDisposeDesc(&replyEvent);
111 - (NSString*)sendTwoTierAEWithRequestedKey:(NSString*)key fromObjectByKey:(NSString*)object eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
113 /*if ( (!key) || (!object) || (!eventClass) || (!eventID) || (psn.highLongOfPSN == kNoProcess) ) {
116 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
117 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
119 const char *sendString = [[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]] UTF8String];
120 NSString *_finalString;
122 AppleEvent sendEvent, replyEvent;
123 AEIdleUPP upp = NewAEIdleUPP(&MyAEIdleCallback);
127 Size resultSize, charResultSize;
130 AEBuildError buildError;
134 if (GetProcessPID(&psn, &pid) == noErr) {
136 NSLog(@"Process doesn't exist! Exiting.");
140 NSLog(@"Error getting PID of application to send to! Exiting.");
144 NSLog(@"_sendString: %s", sendString);
146 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
148 [self printCarbonDesc:&sendEvent];
151 NSLog(@"%d:%d at \"%@\"",buildError.fError,buildError.fErrorPos,[sendString substringToIndex:buildError.fErrorPos]);
154 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, upp, NULL);
156 [self printCarbonDesc:&replyEvent];
159 NSLog(@"Send Error: %i",err);
163 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
164 result=malloc(resultSize);
166 if (err2) { NSLog(@"Error After AESizeOfParam: %i", err2); } else {
168 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, result, resultSize, &charResultSize);
170 if (err3) { NSLog(@"Error After AEGetParamPtr: %i", err3); } else {
172 _finalString = [[NSString stringWithCharacters:result length:charResultSize/sizeof(unichar)] copy];
178 AEDisposeDesc(&sendEvent);
179 AEDisposeDesc(&replyEvent);
186 - (void)sendAEWithEventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
188 /*if ( (!eventClass) || (!eventID) || (psn.highLongOfPSN == kNoProcess) ) {
191 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
192 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
193 AppleEvent event, reply;
195 AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, kAnyTransactionID, &event, nil, "");
197 AESend(&event, &reply, kAENoReply, kAENormalPriority, kAEDefaultTimeout, nil, nil);
199 AEDisposeDesc(&event);
200 AEDisposeDesc(&reply);
204 - (void)printCarbonDesc:(AEDesc*)desc {
206 AEPrintDescToHandle(desc,&xx);
207 NSLog(@"Handle: %s", *xx);