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)];
104 AEDisposeDesc(&sendEvent);
105 AEDisposeDesc(&replyEvent);
110 - (NSString*)sendAEWithSendString:(NSString*)nssendString 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 = [nssendString UTF8String];
117 NSString *_finalString = nil;
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);
152 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
153 result = malloc(resultSize);
156 NSLog(@"Error After AESizeOfParam: %i", err2);
158 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, result, resultSize, &charResultSize);
161 NSLog(@"Error After AEGetParamPtr: %i", err3);
163 _finalString = [NSString stringWithCharacters:result length:charResultSize/sizeof(unichar)];
169 AEDisposeDesc(&sendEvent);
170 AEDisposeDesc(&replyEvent);
175 - (long)sendAEWithRequestedKeyForNumber:(NSString*)key eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
177 //Add error checking...
178 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
179 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
181 const char *sendString = [[NSString stringWithFormat:@"'----':obj { form:'prop', want:type('prop'), seld:type('%s'), from:'null'() }", [key UTF8String]] UTF8String];
184 AppleEvent sendEvent, replyEvent;
187 Size resultSize, charResultSize;
189 AEBuildError buildError;
193 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
194 NSLog(@"Error getting PID of application! Exiting.");
198 //NSLog(@"_sendString: %s", sendString);
200 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
202 //[self printCarbonDesc:&sendEvent];
205 NSLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[sendString substringToIndex:buildError.fErrorPos]);
208 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
210 [self printCarbonDesc:&replyEvent];
213 NSLog(@"Send Error: %i",err);
215 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
218 NSLog(@"Error After AESizeOfParam: %i", err2);
220 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, &result, resultSize, &charResultSize);
223 NSLog(@"Error After AEGetParamPtr: %i", err3);
228 AEDisposeDesc(&sendEvent);
229 AEDisposeDesc(&replyEvent);
234 - (NSString*)sendTwoTierAEWithRequestedKey:(NSString*)key fromObjectByKey:(NSString*)object eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
236 //Add error checking...
237 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
238 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
240 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];
241 NSString *_finalString = nil;
243 AppleEvent sendEvent, replyEvent;
246 Size resultSize, charResultSize;
248 AEBuildError buildError;
252 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
253 NSLog(@"Error getting PID of application! Exiting.");
257 //NSLog(@"_sendString: %s", sendString);
259 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
261 //[self printCarbonDesc:&sendEvent];
264 NSLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[sendString substringToIndex:buildError.fErrorPos]);
267 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
269 //[self printCarbonDesc:&replyEvent];
272 NSLog(@"Send Error: %i",err);
276 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
277 result = malloc(resultSize);
280 NSLog(@"Error After AESizeOfParam: %i", err2);
282 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, result, resultSize, &charResultSize);
285 NSLog(@"Error After AEGetParamPtr: %i", err3);
287 _finalString = [NSString stringWithCharacters:result length:charResultSize/sizeof(unichar)];
293 AEDisposeDesc(&sendEvent);
294 AEDisposeDesc(&replyEvent);
299 - (long)sendTwoTierAEWithRequestedKeyForNumber:(NSString*)key fromObjectByKey:(NSString*)object eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
301 //Add error checking...
302 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
303 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
305 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];
308 AppleEvent sendEvent, replyEvent;
311 Size resultSize, charResultSize;
313 AEBuildError buildError;
317 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
318 NSLog(@"Error getting PID of application! Exiting.");
322 // NSLog(@"_sendString: %s", sendString);
324 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
326 //[self printCarbonDesc:&sendEvent];
329 NSLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[sendString substringToIndex:buildError.fErrorPos]);
332 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
334 [self printCarbonDesc:&replyEvent];
337 NSLog(@"Send Error: %i",err);
339 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
342 NSLog(@"Error After AESizeOfParam: %i", err2);
344 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, &result, resultSize, &charResultSize);
347 NSLog(@"Error After AEGetParamPtr: %i", err3);
352 AEDisposeDesc(&sendEvent);
353 AEDisposeDesc(&replyEvent);
358 - (NSString *)sendAEWithRequestedArray:(NSArray *)array eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
360 NSString *buildString = [NSString stringWithFormat:@"{ form:'prop', want:type('prop'), seld:type('%s'), from:'null'() }", [[array objectAtIndex:0] UTF8String]];
361 const char *sendString;
363 NSString *_finalString = nil;
365 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
366 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
368 AppleEvent sendEvent, replyEvent;
371 Size resultSize, charResultSize;
373 AEBuildError buildError;
377 for (i = 1; i < [array count]; i++) {
378 NSString *from = [NSString stringWithFormat:@"{ form:'prop', want:type('prop'), seld:type('%s'), from:obj %@ }",
379 [[array objectAtIndex:i] UTF8String], buildString];
382 buildString = [@"'----':obj " stringByAppendingString:buildString];
383 sendString = [buildString UTF8String];
385 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
386 NSLog(@"Error getting PID of application! Exiting.");
390 //NSLog(@"_sendString: %s", sendString);
392 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
394 //[self printCarbonDesc:&sendEvent];
397 NSLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[sendString substringToIndex:buildError.fErrorPos]);
400 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
402 //[self printCarbonDesc:&replyEvent];
405 NSLog(@"Send Error: %i",err);
409 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
410 result=malloc(resultSize);
413 NSLog(@"Error After AESizeOfParam: %i", err2);
415 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, result, resultSize, &charResultSize);
417 NSLog(@"Error After AEGetParamPtr: %i", err3);
419 _finalString = [NSString stringWithCharacters:result length:charResultSize/sizeof(unichar)];
427 - (void)sendAEWithEventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
429 //Add error checking...
430 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
431 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
433 AppleEvent event, reply;
435 //AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, kAnyTransactionID, &event, nil, "");
436 AECreateDesc(typeProcessSerialNumber,(ProcessSerialNumber*)&psn,sizeof(ProcessSerialNumber),&dest);
437 AECreateAppleEvent(eClass,eID,&dest,kAutoGenerateReturnID,kAnyTransactionID,&event);
438 AESend(&event, &reply, kAENoReply, kAENormalPriority, kAEDefaultTimeout, idleUPP, nil);
440 AEDisposeDesc(&dest);
441 AEDisposeDesc(&event);
442 AEDisposeDesc(&reply);
445 - (void)printCarbonDesc:(AEDesc*)desc {
447 AEPrintDescToHandle(desc,&xx);
448 NSLog(@"Handle: %s", *xx);
453 - (AEArrayDataPointer)sendAEWithRequestedKeyForArray:(NSString*)key eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
455 //Add error checking...
456 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
457 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
459 const char *sendString = [[NSString stringWithFormat:@"'----':obj { form:'indx', want:'%s', seld:abso($616C6C20$), from:'null'() }", [key UTF8String]] UTF8String];
460 AEArrayDataPointer result = nil;
462 AppleEvent sendEvent, replyEvent;
465 Size resultSize, charResultSize;
467 AEBuildError buildError;
471 //NSLog(@"_sendString: %s", sendString);
473 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
475 //[self printCarbonDesc:&sendEvent];
478 NSLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[sendString substringToIndex:buildError.fErrorPos]);
481 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
483 //[self printCarbonDesc:&replyEvent];
486 NSLog(@"Send Error: %i",err);
488 SInt32 count, resultCount;
490 AECountItems(&replyEvent,&count);
491 result=malloc(sizeof(AEDesc)*count);
492 AEGetArray(&replyEvent, kAEDescArray, result, sizeof(AEDesc)*count, NULL, NULL, &resultCount);
497 AEDisposeDesc(&sendEvent);
498 AEDisposeDesc(&replyEvent);
503 - (long)sendAEWithSendStringForNumber:(NSString*)string eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
505 //Add error checking...
506 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
507 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
509 const char *sendString = [string UTF8String];
512 AppleEvent sendEvent, replyEvent;
515 Size resultSize, charResultSize;
517 AEBuildError buildError;
521 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
522 NSLog(@"Error getting PID of application! Exiting.");
526 //NSLog(@"_sendString: %s", sendString);
528 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
530 //[self printCarbonDesc:&sendEvent];
533 NSLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[sendString substringToIndex:buildError.fErrorPos]);
536 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
538 [self printCarbonDesc:&replyEvent];
541 NSLog(@"Send Error: %i",err);
543 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
546 NSLog(@"Error After AESizeOfParam: %i", err2);
548 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, &result, resultSize, &charResultSize);
551 NSLog(@"Error After AEGetParamPtr: %i", err3);
556 AEDisposeDesc(&sendEvent);
557 AEDisposeDesc(&replyEvent);