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];
31 - (NSString*)sendAEWithRequestedKey:(NSString*)key eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
33 //Add error checking...
34 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
35 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
37 const char *sendString = [[NSString stringWithFormat:@"'----':obj { form:'prop', want:type('prop'), seld:type('%s'), from:'null'() }", [key UTF8String]] UTF8String];
38 NSString *_finalString = nil;
40 AppleEvent sendEvent, replyEvent;
41 AEIdleUPP upp = NewAEIdleUPP(&MyAEIdleCallback);
44 Size resultSize, charResultSize;
46 AEBuildError buildError;
50 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
51 NSLog(@"Error getting PID of application! Exiting.");
55 //NSLog(@"_sendString: %s", sendString);
57 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
59 //[self printCarbonDesc:&sendEvent];
62 NSLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[sendString substringToIndex:buildError.fErrorPos]);
65 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, upp, NULL);
67 //[self printCarbonDesc:&replyEvent];
70 NSLog(@"Send Error: %i",err);
74 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
75 result = malloc(resultSize);
78 NSLog(@"Error After AESizeOfParam: %i", err2);
80 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, result, resultSize, &charResultSize);
83 NSLog(@"Error After AEGetParamPtr: %i", err3);
85 _finalString = [[NSString stringWithCharacters:result length:charResultSize/sizeof(unichar)] copy];
91 AEDisposeDesc(&sendEvent);
92 AEDisposeDesc(&replyEvent);
97 - (long)sendAEWithRequestedKeyForNumber:(NSString*)key eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
99 //Add error checking...
100 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
101 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
103 const char *sendString = [[NSString stringWithFormat:@"'----':obj { form:'prop', want:type('prop'), seld:type('%s'), from:'null'() }", [key UTF8String]] UTF8String];
106 AppleEvent sendEvent, replyEvent;
107 AEIdleUPP upp = NewAEIdleUPP(&MyAEIdleCallback);
110 Size resultSize, charResultSize;
112 AEBuildError buildError;
116 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
117 NSLog(@"Error getting PID of application! Exiting.");
121 //NSLog(@"_sendString: %s", sendString);
123 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
125 //[self printCarbonDesc:&sendEvent];
128 NSLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[sendString substringToIndex:buildError.fErrorPos]);
131 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, upp, NULL);
133 [self printCarbonDesc:&replyEvent];
136 NSLog(@"Send Error: %i",err);
138 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
141 NSLog(@"Error After AESizeOfParam: %i", err2);
143 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, &result, resultSize, &charResultSize);
146 NSLog(@"Error After AEGetParamPtr: %i", err3);
148 NSLog(@"%i", result);
153 AEDisposeDesc(&sendEvent);
154 AEDisposeDesc(&replyEvent);
159 - (NSString*)sendTwoTierAEWithRequestedKey:(NSString*)key fromObjectByKey:(NSString*)object eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
161 //Add error checking...
162 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
163 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
165 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];
166 NSString *_finalString = nil;
168 AppleEvent sendEvent, replyEvent;
169 AEIdleUPP upp = NewAEIdleUPP(&MyAEIdleCallback);
172 Size resultSize, charResultSize;
174 AEBuildError buildError;
178 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
179 NSLog(@"Error getting PID of application! Exiting.");
183 //NSLog(@"_sendString: %s", sendString);
185 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
187 //[self printCarbonDesc:&sendEvent];
190 NSLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[sendString substringToIndex:buildError.fErrorPos]);
193 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, upp, NULL);
195 //[self printCarbonDesc:&replyEvent];
198 NSLog(@"Send Error: %i",err);
202 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
203 result = malloc(resultSize);
206 NSLog(@"Error After AESizeOfParam: %i", err2);
208 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, result, resultSize, &charResultSize);
211 NSLog(@"Error After AEGetParamPtr: %i", err3);
213 _finalString = [[NSString stringWithCharacters:result length:charResultSize/sizeof(unichar)] copy];
219 AEDisposeDesc(&sendEvent);
220 AEDisposeDesc(&replyEvent);
225 - (long)sendTwoTierAEWithRequestedKeyForNumber:(NSString*)key fromObjectByKey:(NSString*)object eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
227 //Add error checking...
228 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
229 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
231 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];
234 AppleEvent sendEvent, replyEvent;
235 AEIdleUPP upp = NewAEIdleUPP(&MyAEIdleCallback);
238 Size resultSize, charResultSize;
240 AEBuildError buildError;
244 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
245 NSLog(@"Error getting PID of application! Exiting.");
249 // NSLog(@"_sendString: %s", sendString);
251 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
253 //[self printCarbonDesc:&sendEvent];
256 NSLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[sendString substringToIndex:buildError.fErrorPos]);
259 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, upp, NULL);
261 [self printCarbonDesc:&replyEvent];
264 NSLog(@"Send Error: %i",err);
266 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
269 NSLog(@"Error After AESizeOfParam: %i", err2);
271 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, &result, resultSize, &charResultSize);
274 NSLog(@"Error After AEGetParamPtr: %i", err3);
276 NSLog(@"%i", result);
281 AEDisposeDesc(&sendEvent);
282 AEDisposeDesc(&replyEvent);
287 - (NSString *)sendAEWithRequestedArray:(NSArray *)array eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
289 NSString *buildString = [NSString stringWithFormat:@"{ form:'prop', want:type('prop'), seld:type('%s'), from:'null'() }", [[array objectAtIndex:0] UTF8String]];
290 const char *sendString;
292 NSString *_finalString = nil;
294 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
295 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
297 AppleEvent sendEvent, replyEvent;
298 AEIdleUPP upp = NewAEIdleUPP(&MyAEIdleCallback);
301 Size resultSize, charResultSize;
303 AEBuildError buildError;
307 for (i = 1; i < [array count]; i++) {
308 NSString *from = [NSString stringWithFormat:@"{ form:'prop', want:type('prop'), seld:type('%s'), from:obj %@ }",
309 [[array objectAtIndex:i] UTF8String], buildString];
312 buildString = [@"'----':obj " stringByAppendingString:buildString];
313 sendString = [buildString UTF8String];
315 if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
316 NSLog(@"Error getting PID of application! Exiting.");
320 //NSLog(@"_sendString: %s", sendString);
322 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
324 //[self printCarbonDesc:&sendEvent];
327 NSLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[sendString substringToIndex:buildError.fErrorPos]);
330 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, upp, NULL);
332 //[self printCarbonDesc:&replyEvent];
335 NSLog(@"Send Error: %i",err);
339 err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
340 result=malloc(resultSize);
343 NSLog(@"Error After AESizeOfParam: %i", err2);
345 err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, result, resultSize, &charResultSize);
347 NSLog(@"Error After AEGetParamPtr: %i", err3);
349 _finalString = [[NSString stringWithCharacters:result length:charResultSize/sizeof(unichar)] copy];
357 - (void)sendAEWithEventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
359 //Add error checking...
360 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
361 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
363 AppleEvent event, reply;
365 //AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, kAnyTransactionID, &event, nil, "");
366 AECreateDesc(typeProcessSerialNumber,(ProcessSerialNumber*)&psn,sizeof(ProcessSerialNumber),&dest);
367 AECreateAppleEvent(eClass,eID,&dest,kAutoGenerateReturnID,kAnyTransactionID,&event);
368 AESend(&event, &reply, kAENoReply, kAENormalPriority, kAEDefaultTimeout, nil, nil);
370 AEDisposeDesc(&dest);
371 AEDisposeDesc(&event);
372 AEDisposeDesc(&reply);
375 - (void)printCarbonDesc:(AEDesc*)desc {
377 AEPrintDescToHandle(desc,&xx);
378 NSLog(@"Handle: %s", *xx);
383 - (AEArrayDataPointer)sendAEWithRequestedKeyForArray:(NSString*)key eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
385 //Add error checking...
386 AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
387 AEEventID eID = *((unsigned long*)[eventID UTF8String]);
389 const char *sendString = [[NSString stringWithFormat:@"'----':obj { form:'prop', want:type('prop'), seld:type('%s'), from:'null'() }", [key UTF8String]] UTF8String];
390 AEArrayDataPointer result = nil;
392 AppleEvent sendEvent, replyEvent;
393 AEIdleUPP upp = NewAEIdleUPP(&MyAEIdleCallback);
396 Size resultSize, charResultSize;
398 AEBuildError buildError;
402 //NSLog(@"_sendString: %s", sendString);
404 err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
406 //[self printCarbonDesc:&sendEvent];
409 NSLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[sendString substringToIndex:buildError.fErrorPos]);
412 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, upp, NULL);
414 //[self printCarbonDesc:&replyEvent];
417 NSLog(@"Send Error: %i",err);
419 SInt32 count, resultCount;
421 AECountItems(&replyEvent,&count);
422 result=malloc(sizeof(AEDesc)*count);
423 AEGetArray(&replyEvent, kAEDescArray, result, sizeof(AEDesc)*count, NULL, NULL, &resultCount);
428 AEDisposeDesc(&sendEvent);
429 AEDisposeDesc(&replyEvent);