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 - (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);
163 AEDisposeDesc(&sendEvent);
164 AEDisposeDesc(&replyEvent);
169 - (NSString*)sendTwoTierAEWithRequestedKey:(NSString*)key fromObjectByKey:(NSString*)object eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
171 //Add error checking...
172 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
173 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
175 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];
176 NSString *_finalString = nil;
178 AppleEvent sendEvent, replyEvent;
181 Size resultSize, charResultSize;
183 AEBuildError buildError;
187 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
188 NSLog(@"Error getting PID of application! Exiting.");
192 //NSLog(@"_sendString: %s", sendString);
194 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
196 //[self printCarbonDesc:&sendEvent];
199 NSLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[sendString substringToIndex:buildError.fErrorPos]);
202 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
204 //[self printCarbonDesc:&replyEvent];
207 NSLog(@"Send Error: %i",err);
211 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
212 result = malloc(resultSize);
215 NSLog(@"Error After AESizeOfParam: %i", err2);
217 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, result, resultSize, &charResultSize);
220 NSLog(@"Error After AEGetParamPtr: %i", err3);
222 _finalString = [NSString stringWithCharacters:result length:charResultSize/sizeof(unichar)];
228 AEDisposeDesc(&sendEvent);
229 AEDisposeDesc(&replyEvent);
234 - (long)sendTwoTierAEWithRequestedKeyForNumber:(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];
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);
274 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
277 NSLog(@"Error After AESizeOfParam: %i", err2);
279 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, &result, resultSize, &charResultSize);
282 NSLog(@"Error After AEGetParamPtr: %i", err3);
287 AEDisposeDesc(&sendEvent);
288 AEDisposeDesc(&replyEvent);
293 - (NSString *)sendAEWithRequestedArray:(NSArray *)array eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
295 NSString *buildString = [NSString stringWithFormat:@"{ form:'prop', want:type('prop'), seld:type('%s'), from:'null'() }", [[array objectAtIndex:0] UTF8String]];
296 const char *sendString;
298 NSString *_finalString = nil;
300 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
301 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
303 AppleEvent sendEvent, replyEvent;
306 Size resultSize, charResultSize;
308 AEBuildError buildError;
312 for (i = 1; i < [array count]; i++) {
313 NSString *from = [NSString stringWithFormat:@"{ form:'prop', want:type('prop'), seld:type('%s'), from:obj %@ }",
314 [[array objectAtIndex:i] UTF8String], buildString];
317 buildString = [@"'----':obj " stringByAppendingString:buildString];
318 sendString = [buildString UTF8String];
320 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
321 NSLog(@"Error getting PID of application! Exiting.");
325 //NSLog(@"_sendString: %s", sendString);
327 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
329 //[self printCarbonDesc:&sendEvent];
332 NSLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[sendString substringToIndex:buildError.fErrorPos]);
335 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
337 //[self printCarbonDesc:&replyEvent];
340 NSLog(@"Send Error: %i",err);
344 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
345 result=malloc(resultSize);
348 NSLog(@"Error After AESizeOfParam: %i", err2);
350 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, result, resultSize, &charResultSize);
352 NSLog(@"Error After AEGetParamPtr: %i", err3);
354 _finalString = [NSString stringWithCharacters:result length:charResultSize/sizeof(unichar)];
362 - (void)sendAEWithEventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
364 //Add error checking...
365 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
366 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
368 AppleEvent event, reply;
370 //AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, kAnyTransactionID, &event, nil, "");
371 AECreateDesc(typeProcessSerialNumber,(ProcessSerialNumber*)&psn,sizeof(ProcessSerialNumber),&dest);
372 AECreateAppleEvent(eClass,eID,&dest,kAutoGenerateReturnID,kAnyTransactionID,&event);
373 AESend(&event, &reply, kAENoReply, kAENormalPriority, kAEDefaultTimeout, idleUPP, nil);
375 AEDisposeDesc(&dest);
376 AEDisposeDesc(&event);
377 AEDisposeDesc(&reply);
380 - (void)printCarbonDesc:(AEDesc*)desc {
382 AEPrintDescToHandle(desc,&xx);
383 NSLog(@"Handle: %s", *xx);
388 - (AEArrayDataPointer)sendAEWithRequestedKeyForArray:(NSString*)key eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
390 //Add error checking...
391 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
392 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
394 const char *sendString = [[NSString stringWithFormat:@"'----':obj { form:'indx', want:'%s', seld:abso($616C6C20$), from:'null'() }", [key UTF8String]] UTF8String];
395 AEArrayDataPointer result = nil;
397 AppleEvent sendEvent, replyEvent;
400 Size resultSize, charResultSize;
402 AEBuildError buildError;
406 //NSLog(@"_sendString: %s", sendString);
408 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
410 //[self printCarbonDesc:&sendEvent];
413 NSLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[sendString substringToIndex:buildError.fErrorPos]);
416 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
418 //[self printCarbonDesc:&replyEvent];
421 NSLog(@"Send Error: %i",err);
423 SInt32 count, resultCount;
425 AECountItems(&replyEvent,&count);
426 result=malloc(sizeof(AEDesc)*count);
427 AEGetArray(&replyEvent, kAEDescArray, result, sizeof(AEDesc)*count, NULL, NULL, &resultCount);
432 AEDisposeDesc(&sendEvent);
433 AEDisposeDesc(&replyEvent);
438 - (long)sendAEWithSendStringForNumber:(NSString*)string eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
440 //Add error checking...
441 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
442 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
444 const char *sendString = [string UTF8String];
447 AppleEvent sendEvent, replyEvent;
450 Size resultSize, charResultSize;
452 AEBuildError buildError;
456 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
457 NSLog(@"Error getting PID of application! Exiting.");
461 //NSLog(@"_sendString: %s", sendString);
463 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
465 //[self printCarbonDesc:&sendEvent];
468 NSLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[sendString substringToIndex:buildError.fErrorPos]);
471 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
473 [self printCarbonDesc:&replyEvent];
476 NSLog(@"Send Error: %i",err);
478 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
481 NSLog(@"Error After AESizeOfParam: %i", err2);
483 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, &result, resultSize, &charResultSize);
486 NSLog(@"Error After AEGetParamPtr: %i", err3);
491 AEDisposeDesc(&sendEvent);
492 AEDisposeDesc(&replyEvent);