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 ) {
25 return _sharedAECenter;
27 return _sharedAECenter = [[ITAppleEventCenter alloc] init];
33 if (self = [super init])
35 idleUPP = NewAEIdleUPP(MyAEIdleCallback);
42 DisposeAEIdleUPP(idleUPP);
45 - (NSString*)sendAEWithRequestedKey:(NSString*)key eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
47 //Add error checking...
48 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
49 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
51 const char *sendString = [[NSString stringWithFormat:@"'----':obj { form:'prop', want:type('prop'), seld:type('%s'), from:'null'() }", [key UTF8String]] UTF8String];
52 NSString *_finalString = nil;
54 AppleEvent sendEvent, replyEvent;
57 Size resultSize, charResultSize;
59 AEBuildError buildError;
63 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
64 NSLog(@"Error getting PID of application! Exiting.");
68 //NSLog(@"_sendString: %s", sendString);
70 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
72 //[self printCarbonDesc:&sendEvent];
75 NSLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[sendString substringToIndex:buildError.fErrorPos]);
78 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
80 //[self printCarbonDesc:&replyEvent];
83 NSLog(@"Send Error: %i",err);
87 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
88 result = malloc(resultSize);
91 NSLog(@"Error After AESizeOfParam: %i", err2);
93 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, result, resultSize, &charResultSize);
96 NSLog(@"Error After AEGetParamPtr: %i", err3);
98 _finalString = [[NSString stringWithCharacters:result length:charResultSize/sizeof(unichar)] copy];
104 AEDisposeDesc(&sendEvent);
105 AEDisposeDesc(&replyEvent);
110 - (long)sendAEWithRequestedKeyForNumber:(NSString*)key eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
112 //Add error checking...
113 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
114 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
116 const char *sendString = [[NSString stringWithFormat:@"'----':obj { form:'prop', want:type('prop'), seld:type('%s'), from:'null'() }", [key UTF8String]] UTF8String];
119 AppleEvent sendEvent, replyEvent;
122 Size resultSize, charResultSize;
124 AEBuildError buildError;
128 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
129 NSLog(@"Error getting PID of application! Exiting.");
133 //NSLog(@"_sendString: %s", sendString);
135 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
137 //[self printCarbonDesc:&sendEvent];
140 NSLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[sendString substringToIndex:buildError.fErrorPos]);
143 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
145 [self printCarbonDesc:&replyEvent];
148 NSLog(@"Send Error: %i",err);
150 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
153 NSLog(@"Error After AESizeOfParam: %i", err2);
155 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, &result, resultSize, &charResultSize);
158 NSLog(@"Error After AEGetParamPtr: %i", err3);
160 NSLog(@"%i", result);
165 AEDisposeDesc(&sendEvent);
166 AEDisposeDesc(&replyEvent);
171 - (NSString*)sendTwoTierAEWithRequestedKey:(NSString*)key fromObjectByKey:(NSString*)object eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
173 //Add error checking...
174 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
175 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
177 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];
178 NSString *_finalString = nil;
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,[sendString substringToIndex:buildError.fErrorPos]);
204 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
206 //[self printCarbonDesc:&replyEvent];
209 NSLog(@"Send Error: %i",err);
213 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
214 result = malloc(resultSize);
217 NSLog(@"Error After AESizeOfParam: %i", err2);
219 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, result, resultSize, &charResultSize);
222 NSLog(@"Error After AEGetParamPtr: %i", err3);
224 _finalString = [[NSString stringWithCharacters:result length:charResultSize/sizeof(unichar)] copy];
230 AEDisposeDesc(&sendEvent);
231 AEDisposeDesc(&replyEvent);
236 - (long)sendTwoTierAEWithRequestedKeyForNumber:(NSString*)key fromObjectByKey:(NSString*)object eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
238 //Add error checking...
239 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
240 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
242 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];
245 AppleEvent sendEvent, replyEvent;
248 Size resultSize, charResultSize;
250 AEBuildError buildError;
254 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
255 NSLog(@"Error getting PID of application! Exiting.");
259 // NSLog(@"_sendString: %s", sendString);
261 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
263 //[self printCarbonDesc:&sendEvent];
266 NSLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[sendString substringToIndex:buildError.fErrorPos]);
269 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
271 [self printCarbonDesc:&replyEvent];
274 NSLog(@"Send Error: %i",err);
276 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
279 NSLog(@"Error After AESizeOfParam: %i", err2);
281 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, &result, resultSize, &charResultSize);
284 NSLog(@"Error After AEGetParamPtr: %i", err3);
286 NSLog(@"%i", result);
291 AEDisposeDesc(&sendEvent);
292 AEDisposeDesc(&replyEvent);
297 - (NSString *)sendAEWithRequestedArray:(NSArray *)array eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
299 NSString *buildString = [NSString stringWithFormat:@"{ form:'prop', want:type('prop'), seld:type('%s'), from:'null'() }", [[array objectAtIndex:0] UTF8String]];
300 const char *sendString;
302 NSString *_finalString = nil;
304 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
305 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
307 AppleEvent sendEvent, replyEvent;
310 Size resultSize, charResultSize;
312 AEBuildError buildError;
316 for (i = 1; i < [array count]; i++) {
317 NSString *from = [NSString stringWithFormat:@"{ form:'prop', want:type('prop'), seld:type('%s'), from:obj %@ }",
318 [[array objectAtIndex:i] UTF8String], buildString];
321 buildString = [@"'----':obj " stringByAppendingString:buildString];
322 sendString = [buildString UTF8String];
324 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
325 NSLog(@"Error getting PID of application! Exiting.");
329 //NSLog(@"_sendString: %s", sendString);
331 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
333 //[self printCarbonDesc:&sendEvent];
336 NSLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[sendString substringToIndex:buildError.fErrorPos]);
339 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
341 //[self printCarbonDesc:&replyEvent];
344 NSLog(@"Send Error: %i",err);
348 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
349 result=malloc(resultSize);
352 NSLog(@"Error After AESizeOfParam: %i", err2);
354 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, result, resultSize, &charResultSize);
356 NSLog(@"Error After AEGetParamPtr: %i", err3);
358 _finalString = [[NSString stringWithCharacters:result length:charResultSize/sizeof(unichar)] copy];
366 - (void)sendAEWithEventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
368 //Add error checking...
369 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
370 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
372 AppleEvent event, reply;
374 //AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, kAnyTransactionID, &event, nil, "");
375 AECreateDesc(typeProcessSerialNumber,(ProcessSerialNumber*)&psn,sizeof(ProcessSerialNumber),&dest);
376 AECreateAppleEvent(eClass,eID,&dest,kAutoGenerateReturnID,kAnyTransactionID,&event);
377 AESend(&event, &reply, kAENoReply, kAENormalPriority, kAEDefaultTimeout, idleUPP, nil);
379 AEDisposeDesc(&dest);
380 AEDisposeDesc(&event);
381 AEDisposeDesc(&reply);
384 - (void)printCarbonDesc:(AEDesc*)desc {
386 AEPrintDescToHandle(desc,&xx);
387 NSLog(@"Handle: %s", *xx);
392 - (AEArrayDataPointer)sendAEWithRequestedKeyForArray:(NSString*)key eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
394 //Add error checking...
395 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
396 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
398 const char *sendString = [[NSString stringWithFormat:@"'----':obj { form:'indx', want:'%s', seld:abso($616C6C20$), from:'null'() }", [key UTF8String]] UTF8String];
399 AEArrayDataPointer result = nil;
401 AppleEvent sendEvent, replyEvent;
404 Size resultSize, charResultSize;
406 AEBuildError buildError;
410 //NSLog(@"_sendString: %s", sendString);
412 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
414 //[self printCarbonDesc:&sendEvent];
417 NSLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[sendString substringToIndex:buildError.fErrorPos]);
420 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
422 //[self printCarbonDesc:&replyEvent];
425 NSLog(@"Send Error: %i",err);
427 SInt32 count, resultCount;
429 AECountItems(&replyEvent,&count);
430 result=malloc(sizeof(AEDesc)*count);
431 AEGetArray(&replyEvent, kAEDescArray, result, sizeof(AEDesc)*count, NULL, NULL, &resultCount);
436 AEDisposeDesc(&sendEvent);
437 AEDisposeDesc(&replyEvent);