+
+- (AEArrayDataPointer)sendAEWithRequestedKeyForArray:(NSString*)key eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
+{
+ AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
+ AEEventID eID = *((unsigned long*)[eventID UTF8String]);
+
+ NSString *nssendString = [NSString stringWithFormat:@"'----':obj { form:'indx', want:'%s', seld:type('all '), from:'null'() }", [key UTF8String]];
+ const char *sendString = [nssendString UTF8String];
+ AEArrayDataPointer result = nil;
+ int pid;
+
+ AppleEvent sendEvent, replyEvent;
+
+
+ AEBuildError buildError;
+ OSStatus err;
+
+if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
+ ITDebugLog(@"Error getting PID of application.");
+ return nil;
+ }
+ err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
+
+ [self printCarbonDesc:&sendEvent];
+
+ if (err) {
+ ITDebugLog(@"Error: %d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[nssendString substringToIndex:buildError.fErrorPos]);
+ }
+
+
+ err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
+
+ [self printCarbonDesc:&replyEvent];
+
+ if (err) {
+ ITDebugLog(@"Send Error: %i",err);
+ } else {
+ SInt32 count, resultCount;
+
+ AECountItems(&replyEvent,&count);
+ if ((sizeof(AEDesc)*count) != 0) {
+ result=malloc(sizeof(AEDesc)*count);
+ } else {
+ return nil;
+ }
+ AEGetArray(&replyEvent, kAEDescArray, result, sizeof(AEDesc)*count, NULL, NULL, &resultCount);
+
+ free(result);
+ }
+
+ AEDisposeDesc(&sendEvent);
+ AEDisposeDesc(&replyEvent);
+
+ return result;
+}
+
+- (long)sendAEWithSendStringForNumber:(NSString*)string eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
+{
+ //Add error checking...
+ AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
+ AEEventID eID = *((unsigned long*)[eventID UTF8String]);
+
+ const char *sendString = [string UTF8String];
+ SInt32 result = 0;
+ int pid;
+
+ AppleEvent sendEvent, replyEvent;
+
+ DescType resultType;
+ Size resultSize, charResultSize;
+
+ AEBuildError buildError;
+ OSStatus berr, err;
+ OSErr err2, err3;
+
+ if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
+ ITDebugLog(@"Error getting PID of application.");
+ return nil;
+ }
+
+ berr = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
+
+ [self printCarbonDesc:&sendEvent];
+
+ if (berr) {
+ ITDebugLog(@"Error: %d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[string substringToIndex:buildError.fErrorPos]);
+ }
+
+ err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
+
+ [self printCarbonDesc:&replyEvent];
+
+ if (err) {
+ ITDebugLog(@"Send Error: %i",err);
+ } else {
+ err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
+
+ if (err2) {
+ ITDebugLog(@"Error After AESizeOfParam: %i", err2);
+ } else {
+ err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, &result, resultSize, &charResultSize);
+
+ if (err3) {
+ ITDebugLog(@"Error After AEGetParamPtr: %i", err3);
+ }
+ }
+ }
+
+
+if (!berr) AEDisposeDesc(&sendEvent);
+if (!err) AEDisposeDesc(&replyEvent);
+ITDebugLog(@"waffles say %d",result);
+return result;
+}
+
+- (AEArrayDataPointer)sendAEWithSendStringForArray:(NSString*)string eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
+{
+ //Add error checking...
+ AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
+ AEEventID eID = *((unsigned long*)[eventID UTF8String]);
+ int pid;
+
+ const char *sendString = [string UTF8String];
+ AEArrayDataPointer result = NULL;
+
+ AppleEvent sendEvent, replyEvent;
+
+ AEBuildError buildError;
+ OSStatus berr, err;
+
+ if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
+ ITDebugLog(@"Error getting PID of application.");
+ return nil;
+ }
+
+ berr = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
+
+ [self printCarbonDesc:&sendEvent];
+
+ if (berr) {
+ ITDebugLog(@"Error: %d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[string substringToIndex:buildError.fErrorPos]);
+ }
+
+ err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
+
+ [self printCarbonDesc:&replyEvent];
+
+ if (err) {
+ ITDebugLog(@"Send Error: %i",err);
+ } else {
+ SInt32 count, resultCount;
+
+ AECountItems(&replyEvent,&count);
+ if ((sizeof(AEDesc)*count) != 0) {
+ result=malloc(sizeof(AEDesc)*count);
+ } else {
+ return nil;
+ }
+ AEGetArray(&replyEvent, kAEDescArray, result, sizeof(AEDesc)*count, NULL, NULL, &resultCount);
+
+ free(result);
+ }
+
+ if (!berr) AEDisposeDesc(&sendEvent);
+ if (!err) AEDisposeDesc(&replyEvent);
+
+ return result;
+}
+
+- (NSData*)sendAEWithSendStringForData:(NSString*)nssendString eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
+{
+ //Add error checking...
+ AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
+ AEEventID eID = *((unsigned long*)[eventID UTF8String]);
+ int pid;
+
+ const char *sendString = [nssendString UTF8String];
+ NSData *_finalData = nil;
+
+ AppleEvent sendEvent, replyEvent;
+
+ DescType resultType;
+ Size resultSize, charResultSize;
+
+ AEBuildError buildError;
+ OSStatus berr,err;
+ OSErr err2, err3;
+
+ if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
+ ITDebugLog(@"Error getting PID of application.");
+ return nil;
+ }
+
+ berr = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
+ if (!berr) [self printCarbonDesc:&sendEvent];
+
+ if (berr) {
+ ITDebugLog(@"Error: %d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[nssendString substringToIndex:buildError.fErrorPos]);
+ }
+
+ err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
+ if (!err) [self printCarbonDesc:&replyEvent];
+
+ if (err) {
+ ITDebugLog(@"Send Error: %i",err);
+ } else {
+ unichar *result = 0;
+
+ err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
+ if (resultSize != 0) {
+ result = malloc(resultSize);
+ } else {
+ return nil;
+ }
+
+ if (err2) {
+ ITDebugLog(@"Error After AESizeOfParam: %i", err2);
+ } else {
+ err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, result, resultSize, &charResultSize);
+
+ if (err3) {
+ ITDebugLog(@"Error After AEGetParamPtr: %i", err3);
+ } else {
+ _finalData = [NSData dataWithBytesNoCopy:result length:charResultSize freeWhenDone:YES];
+ }
+ }
+ }
+
+ if (!berr) AEDisposeDesc(&sendEvent);
+ if (!err) AEDisposeDesc(&replyEvent);
+
+ return _finalData;
+}
+
+@end
\ No newline at end of file