Upped timeout to 100 ticks...
[ITFoundation.git] / ITAppleEventCenter.m
index ed1ad83..9efe080 100755 (executable)
@@ -1,12 +1,7 @@
 #import "ITAppleEventCenter.h"
 #import "ITAppleEventCenter.h"
+#import "ITDebug.h"
 
 
-Boolean MyAEIdleCallback (
-                                        EventRecord * theEvent,
-                                        SInt32 * sleepTime,
-                                        RgnHandle * mouseRgn
-                                        );
-
-Boolean MyAEIdleCallback (
+static Boolean MyAEIdleCallback (
                                         EventRecord * theEvent,
                                         SInt32 * sleepTime,
                                         RgnHandle * mouseRgn
                                         EventRecord * theEvent,
                                         SInt32 * sleepTime,
                                         RgnHandle * mouseRgn
@@ -23,74 +18,154 @@ static ITAppleEventCenter *_sharedAECenter = nil;
 {
     if( _sharedAECenter ) {
         return _sharedAECenter;
 {
     if( _sharedAECenter ) {
         return _sharedAECenter;
-    } else {   
+    } else {
         return _sharedAECenter = [[ITAppleEventCenter alloc] init];
     }
 }
 
         return _sharedAECenter = [[ITAppleEventCenter alloc] init];
     }
 }
 
+- (id)init
+{
+    if ( ( self = [super init] ) ) {
+        idleUPP = NewAEIdleUPP(MyAEIdleCallback);
+    }
+    return self;
+}
+
+- (void)dealloc
+{
+    DisposeAEIdleUPP(idleUPP);
+}
+
 - (NSString*)sendAEWithRequestedKey:(NSString*)key eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
 {
     //Add error checking...
     AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
     AEEventID   eID    = *((unsigned long*)[eventID UTF8String]);
 - (NSString*)sendAEWithRequestedKey:(NSString*)key 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 = [[NSString stringWithFormat:@"'----':obj { form:'prop', want:type('prop'), seld:type('%s'), from:'null'() }", [key UTF8String]] UTF8String];
+    int pid;
+    NSString *nssendString = [NSString stringWithFormat:@"'----':obj { form:'prop', want:type('prop'), seld:type('%s'), from:'null'() }", [key UTF8String]];
+    const char *sendString = [nssendString UTF8String];
     NSString  *_finalString = nil;
     NSString  *_finalString = nil;
-    
+
     AppleEvent sendEvent, replyEvent;
     AppleEvent sendEvent, replyEvent;
-    AEIdleUPP upp = NewAEIdleUPP(&MyAEIdleCallback);
-    
+
     DescType resultType;
     Size resultSize, charResultSize;
     DescType resultType;
     Size resultSize, charResultSize;
-    
+
     AEBuildError buildError;
     OSStatus err;
     AEBuildError buildError;
     OSStatus err;
-    OSErr err2, err3;
-    /*
-    if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
-        NSLog(@"Error getting PID of application! Exiting.");
-        return nil;
-    }
-    */
-    //NSLog(@"_sendString: %s", sendString);
-    
-    err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
-    
-    //[self printCarbonDesc:&sendEvent];
+    OSErr berr, 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,[nssendString substringToIndex:buildError.fErrorPos]);
+    }
+
+    err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, /*kAEDefaultTimeout*/30, idleUPP, NULL);
+
+    [self printCarbonDesc:&replyEvent];
+
     if (err) {
     if (err) {
-        NSLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[sendString substringToIndex:buildError.fErrorPos]);
+        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 {
+                _finalString = [NSString stringWithCharacters:result length:charResultSize/sizeof(unichar)];
+            }
+        }
+        free(result);
     }
     }
+
+    if (!berr) AEDisposeDesc(&sendEvent);
+    if (!err) AEDisposeDesc(&replyEvent);
+
+    return _finalString;
+}
+
+- (NSString*)sendAEWithSendString:(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];
+    NSString  *_finalString = nil;
+
+    AppleEvent sendEvent, replyEvent;
+
+    DescType resultType;
+    Size resultSize, charResultSize;
+
+    AEBuildError buildError;
+    OSStatus berr,err;
+    OSErr err2, err3;
     
     
-    err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, upp, NULL);
-    
-    //[self printCarbonDesc:&replyEvent];
-    
+       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, /*kAEDefaultTimeout*/30, idleUPP, NULL);
+    if (!err) [self printCarbonDesc:&replyEvent];
+
     if (err) {
     if (err) {
-        NSLog(@"Send Error: %i",err);
+        ITDebugLog(@"Send Error: %i",err);
     } else {
         unichar *result = 0;
     } else {
         unichar *result = 0;
-        
+
         err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
         err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
-        result = malloc(resultSize);
-        
+        if (resultSize != 0) {
+            result = malloc(resultSize);
+        } else {
+            return nil;
+        }
+
         if (err2) {
         if (err2) {
-            NSLog(@"Error After AESizeOfParam: %i", err2);
+            ITDebugLog(@"Error After AESizeOfParam: %i", err2);
         } else {
             err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, result, resultSize, &charResultSize);
         } else {
             err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, result, resultSize, &charResultSize);
-            
+
             if (err3) {
             if (err3) {
-                NSLog(@"Error After AEGetParamPtr: %i", err3);
+                ITDebugLog(@"Error After AEGetParamPtr: %i", err3);
             } else {
             } else {
-                _finalString = [[NSString stringWithCharacters:result length:charResultSize/sizeof(unichar)] copy];
+                _finalString = [NSString stringWithCharacters:result length:charResultSize/sizeof(unichar)];
             }
         }
         free(result);
     }
             }
         }
         free(result);
     }
-    
-    AEDisposeDesc(&sendEvent);
-    AEDisposeDesc(&replyEvent);
-    
+
+    if (!berr) AEDisposeDesc(&sendEvent);
+    if (!err) AEDisposeDesc(&replyEvent);
+
     return _finalString;
 }
 
     return _finalString;
 }
 
@@ -99,188 +174,183 @@ static ITAppleEventCenter *_sharedAECenter = nil;
     //Add error checking...
     AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
     AEEventID    eID    = *((unsigned long*)[eventID UTF8String]);
     //Add error checking...
     AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
     AEEventID    eID    = *((unsigned long*)[eventID UTF8String]);
-    
-    const char *sendString = [[NSString stringWithFormat:@"'----':obj { form:'prop', want:type('prop'), seld:type('%s'), from:'null'() }", [key UTF8String]] UTF8String];
+    int pid;
+
+    NSString *nssendString = [NSString stringWithFormat:@"'----':obj { form:'prop', want:type('prop'), seld:type('%s'), from:'null'() }", [key UTF8String]];
+    const char *sendString = [nssendString UTF8String];
     long result = 0;
     long result = 0;
-    
+
     AppleEvent sendEvent, replyEvent;
     AppleEvent sendEvent, replyEvent;
-    AEIdleUPP upp = NewAEIdleUPP(&MyAEIdleCallback);
-    
+
     DescType resultType;
     Size resultSize, charResultSize;
     DescType resultType;
     Size resultSize, charResultSize;
-    
+
     AEBuildError buildError;
     OSStatus err;
     AEBuildError buildError;
     OSStatus err;
-    OSErr err2, err3;
-    /*
-    if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
-        NSLog(@"Error getting PID of application! Exiting.");
-        return nil;
-    }
-    */
-    //NSLog(@"_sendString: %s", sendString);
+    OSErr berr, err2, err3;
     
     
-    err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
-    
-    //[self printCarbonDesc:&sendEvent];
-    
-    if (err) {
-        NSLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[sendString substringToIndex:buildError.fErrorPos]);
+       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,[nssendString substringToIndex:buildError.fErrorPos]);
     }
     }
-    
-    err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, upp, NULL);
-    
+
+    err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, /*kAEDefaultTimeout*/30, idleUPP, NULL);
+
     [self printCarbonDesc:&replyEvent];
     [self printCarbonDesc:&replyEvent];
-    
+
     if (err) {
     if (err) {
-        NSLog(@"Send Error: %i",err);
+        ITDebugLog(@"Send Error: %i",err);
     } else {
         err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
     } else {
         err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
-        
+
         if (err2) {
         if (err2) {
-            NSLog(@"Error After AESizeOfParam: %i", err2);
+            ITDebugLog(@"Error After AESizeOfParam: %i", err2);
         } else {
             err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, &result, resultSize, &charResultSize);
         } else {
             err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, &result, resultSize, &charResultSize);
-            
+
             if (err3) {
             if (err3) {
-                NSLog(@"Error After AEGetParamPtr: %i", err3);
-            } else {
-                NSLog(@"%i", result);
+                ITDebugLog(@"Error After AEGetParamPtr: %i", err3);
             }
         }
     }
             }
         }
     }
-    
-    AEDisposeDesc(&sendEvent);
-    AEDisposeDesc(&replyEvent);
-    
+
+    if (!berr) AEDisposeDesc(&sendEvent);
+    if (!err) AEDisposeDesc(&replyEvent);
+
     return result;
 }
 
 - (NSString*)sendTwoTierAEWithRequestedKey:(NSString*)key fromObjectByKey:(NSString*)object eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
 {
     return result;
 }
 
 - (NSString*)sendTwoTierAEWithRequestedKey:(NSString*)key fromObjectByKey:(NSString*)object eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
 {
-    //Add error checking...
     AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
     AEEventID    eID    = *((unsigned long*)[eventID UTF8String]);
     AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
     AEEventID    eID    = *((unsigned long*)[eventID UTF8String]);
-    
-    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];
+    int pid;
+
+    NSString *nssendString = [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]];
+    const char *sendString = [nssendString UTF8String];
     NSString  *_finalString = nil;
     NSString  *_finalString = nil;
-    
+
     AppleEvent sendEvent, replyEvent;
     AppleEvent sendEvent, replyEvent;
-    AEIdleUPP upp = NewAEIdleUPP(&MyAEIdleCallback);
-    
+
     DescType resultType;
     Size resultSize, charResultSize;
     DescType resultType;
     Size resultSize, charResultSize;
-    
+
     AEBuildError buildError;
     AEBuildError buildError;
-    OSStatus err;
+    OSStatus berr,err;
     OSErr err2, err3;
     OSErr err2, err3;
-    /*
-    if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
-        NSLog(@"Error getting PID of application! Exiting.");
-        return nil;
-    }*/
-    
-    //NSLog(@"_sendString: %s", sendString);
-    
-    err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
-    
-    //[self printCarbonDesc:&sendEvent];
     
     
-    if (err) {
-        NSLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[sendString substringToIndex:buildError.fErrorPos]);
+       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,[nssendString substringToIndex:buildError.fErrorPos]);
     }
     }
-    
-    err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, upp, NULL);
-    
-    //[self printCarbonDesc:&replyEvent];
-    
+
+    err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, /*kAEDefaultTimeout*/30, idleUPP, NULL);
+
+    [self printCarbonDesc:&replyEvent];
+
     if (err) {
     if (err) {
-        NSLog(@"Send Error: %i",err);
+        ITDebugLog(@"Send Error: %i",err);
     } else {
         unichar *result = 0;
     } else {
         unichar *result = 0;
-        
+
         err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
         err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
-        result = malloc(resultSize);
+        if (resultSize != 0) {
+            result = malloc(resultSize);
+        } else {
+            return nil;
+        }
         
         if (err2) {
         
         if (err2) {
-            NSLog(@"Error After AESizeOfParam: %i", err2);
+            ITDebugLog(@"Error After AESizeOfParam: %i", err2);
         } else {
             err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, result, resultSize, &charResultSize);
         } else {
             err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, result, resultSize, &charResultSize);
-            
+
             if (err3) {
             if (err3) {
-                NSLog(@"Error After AEGetParamPtr: %i", err3);
+                ITDebugLog(@"Error After AEGetParamPtr: %i", err3);
             } else {
             } else {
-                _finalString = [[NSString stringWithCharacters:result length:charResultSize/sizeof(unichar)] copy];
+                _finalString = [NSString stringWithCharacters:result length:charResultSize/sizeof(unichar)];
             }
         }
         free(result);
     }
             }
         }
         free(result);
     }
-    
-    AEDisposeDesc(&sendEvent);
-    AEDisposeDesc(&replyEvent);
-    
+
+    if (!berr) AEDisposeDesc(&sendEvent);
+    if (!err) AEDisposeDesc(&replyEvent);
+
     return _finalString;
 }
 
 - (long)sendTwoTierAEWithRequestedKeyForNumber:(NSString*)key fromObjectByKey:(NSString*)object eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
 {
     return _finalString;
 }
 
 - (long)sendTwoTierAEWithRequestedKeyForNumber:(NSString*)key fromObjectByKey:(NSString*)object eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
 {
-    //Add error checking...
     AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
     AEEventID    eID    = *((unsigned long*)[eventID UTF8String]);
     AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
     AEEventID    eID    = *((unsigned long*)[eventID UTF8String]);
-    
-    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];
+
+    NSString *nssendString = [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]];
+    const char *sendString = [nssendString UTF8String];
     long result = 0;
     long result = 0;
-    
+
     AppleEvent sendEvent, replyEvent;
     AppleEvent sendEvent, replyEvent;
-    AEIdleUPP upp = NewAEIdleUPP(&MyAEIdleCallback);
-    
+    int pid;
+
     DescType resultType;
     Size resultSize, charResultSize;
     DescType resultType;
     Size resultSize, charResultSize;
-    
+
     AEBuildError buildError;
     AEBuildError buildError;
-    OSStatus err;
+    OSStatus berr, err;
     OSErr err2, err3;
     OSErr err2, err3;
-    /*
-    if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
-        NSLog(@"Error getting PID of application! Exiting.");
-        return nil;
-    }
-    */
-   // NSLog(@"_sendString: %s", sendString);
-    
-    err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
-    
-    //[self printCarbonDesc:&sendEvent];
     
     
-    if (err) {
-        NSLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[sendString substringToIndex:buildError.fErrorPos]);
+       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,[nssendString substringToIndex:buildError.fErrorPos]);
     }
     }
-    
-    err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, upp, NULL);
-    
+
+    err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, /*kAEDefaultTimeout*/30, idleUPP, NULL);
+
     [self printCarbonDesc:&replyEvent];
     [self printCarbonDesc:&replyEvent];
-    
+
     if (err) {
     if (err) {
-        NSLog(@"Send Error: %i",err);
+        ITDebugLog(@"Send Error: %i",err);
     } else {
         err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
     } else {
         err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
-        
+
         if (err2) {
         if (err2) {
-            NSLog(@"Error After AESizeOfParam: %i", err2);
+            ITDebugLog(@"Error After AESizeOfParam: %i", err2);
         } else {
             err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, &result, resultSize, &charResultSize);
         } else {
             err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, &result, resultSize, &charResultSize);
-            
+
             if (err3) {
             if (err3) {
-                NSLog(@"Error After AEGetParamPtr: %i", err3);
-            } else {
-                NSLog(@"%i", result);
+                ITDebugLog(@"Error After AEGetParamPtr: %i", err3);
             }
         }
     }
             }
         }
     }
-    
-    AEDisposeDesc(&sendEvent);
-    AEDisposeDesc(&replyEvent);
-    
+
+    if (!berr) AEDisposeDesc(&sendEvent);
+    if (!err) AEDisposeDesc(&replyEvent);
+
     return result;
 }
 
     return result;
 }
 
@@ -290,136 +360,149 @@ static ITAppleEventCenter *_sharedAECenter = nil;
     const char *sendString;
     int i;
     NSString  *_finalString = nil;
     const char *sendString;
     int i;
     NSString  *_finalString = nil;
-    
+    int pid;
+
     AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
     AEEventID    eID    = *((unsigned long*)[eventID UTF8String]);
     AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
     AEEventID    eID    = *((unsigned long*)[eventID UTF8String]);
-    
+
     AppleEvent sendEvent, replyEvent;
     AppleEvent sendEvent, replyEvent;
-    AEIdleUPP upp = NewAEIdleUPP(&MyAEIdleCallback);
-    
+
     DescType resultType;
     Size resultSize, charResultSize;
     DescType resultType;
     Size resultSize, charResultSize;
-    
+
     AEBuildError buildError;
     AEBuildError buildError;
-    OSStatus err;
+    OSStatus berr, err;
     OSErr err2, err3;
     OSErr err2, err3;
-    
+
     for (i = 1; i < [array count]; i++) {
         NSString *from = [NSString stringWithFormat:@"{ form:'prop', want:type('prop'), seld:type('%s'), from:obj %@ }",
     for (i = 1; i < [array count]; i++) {
         NSString *from = [NSString stringWithFormat:@"{ form:'prop', want:type('prop'), seld:type('%s'), from:obj %@ }",
-                    [[array objectAtIndex:i] UTF8String], buildString];
+                 [[array objectAtIndex:i] UTF8String], buildString];
         buildString = from;
     }
     buildString = [@"'----':obj " stringByAppendingString:buildString];
     sendString = [buildString UTF8String];
         buildString = from;
     }
     buildString = [@"'----':obj " stringByAppendingString:buildString];
     sendString = [buildString UTF8String];
-    /*
-    if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
-        NSLog(@"Error getting PID of application! Exiting.");
-        return nil;
-    }
-    */
-    //NSLog(@"_sendString: %s", sendString);
     
     
-    err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
-    
-    //[self printCarbonDesc:&sendEvent];
-    
-    if (err) {
-        NSLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[sendString substringToIndex:buildError.fErrorPos]);
+       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,[buildString substringToIndex:buildError.fErrorPos]);
     }
     }
-    
-    err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, upp, NULL);
-    
-    //[self printCarbonDesc:&replyEvent];
-    
+
+    err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, /*kAEDefaultTimeout*/30, idleUPP, NULL);
+
+    [self printCarbonDesc:&replyEvent];
+
     if (err) {
     if (err) {
-        NSLog(@"Send Error: %i",err);
+        ITDebugLog(@"Send Error: %i",err);
     } else {
         unichar *result = 0;
     } else {
         unichar *result = 0;
-        
+
         err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
         err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
-        result=malloc(resultSize);
-        
+        if (resultSize != 0) {
+            result = malloc(resultSize);
+        } else {
+            return nil;
+        }
         if (err2) {
         if (err2) {
-            NSLog(@"Error After AESizeOfParam: %i", err2);
+            ITDebugLog(@"Error After AESizeOfParam: %i", err2);
         } else {
             err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, result, resultSize, &charResultSize);
             if (err3) {
         } else {
             err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, result, resultSize, &charResultSize);
             if (err3) {
-                NSLog(@"Error After AEGetParamPtr: %i", err3);
+                ITDebugLog(@"Error After AEGetParamPtr: %i", err3);
             } else {
             } else {
-                _finalString = [[NSString stringWithCharacters:result length:charResultSize/sizeof(unichar)] copy];
+                _finalString = [NSString stringWithCharacters:result length:charResultSize/sizeof(unichar)];
             }
         }
         free(result);
     }
             }
         }
         free(result);
     }
+    if (!berr) AEDisposeDesc(&sendEvent);
+    if (!err) AEDisposeDesc(&replyEvent);
     return _finalString;
 }
 
 - (void)sendAEWithEventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
 {
     return _finalString;
 }
 
 - (void)sendAEWithEventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
 {
-    //Add error checking...
     AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
     AEEventID    eID    = *((unsigned long*)[eventID UTF8String]);
     AEDesc dest;
     AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
     AEEventID    eID    = *((unsigned long*)[eventID UTF8String]);
     AEDesc dest;
-    AppleEvent event, reply;
+    int pid;
 
 
+    AppleEvent event, reply;
+    OSStatus cerr,cerr2,err;
     //AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, kAnyTransactionID, &event, nil, "");
     //AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, kAnyTransactionID, &event, nil, "");
-    AECreateDesc(typeProcessSerialNumber,(ProcessSerialNumber*)&psn,sizeof(ProcessSerialNumber),&dest);
-    AECreateAppleEvent(eClass,eID,&dest,kAutoGenerateReturnID,kAnyTransactionID,&event);
-    AESend(&event, &reply, kAENoReply, kAENormalPriority, kAEDefaultTimeout, nil, nil);
-    
-    AEDisposeDesc(&dest);
-    AEDisposeDesc(&event);
-    AEDisposeDesc(&reply);
+    if ((GetProcessPID(&psn, &pid) == noErr) && (pid == 0)) {
+        ITDebugLog(@"Error getting PID of application.");
+       return;
+    }
+    cerr = AECreateDesc(typeProcessSerialNumber,(ProcessSerialNumber*)&psn,sizeof(ProcessSerialNumber),&dest);
+    cerr2 = AECreateAppleEvent(eClass,eID,&dest,kAutoGenerateReturnID,kAnyTransactionID,&event);
+    [self printCarbonDesc:&event];
+    err = AESend(&event, &reply, kAENoReply, kAENormalPriority, /*kAEDefaultTimeout*/30, idleUPP, nil);
+    [self printCarbonDesc:&reply];
+    if (!cerr2) AEDisposeDesc(&dest);
+    if (!cerr) AEDisposeDesc(&event);
+    if (!err) AEDisposeDesc(&reply);
 }
 
 - (void)printCarbonDesc:(AEDesc*)desc {
     Handle xx;
     AEPrintDescToHandle(desc,&xx);
 }
 
 - (void)printCarbonDesc:(AEDesc*)desc {
     Handle xx;
     AEPrintDescToHandle(desc,&xx);
-    NSLog(@"Handle: %s", *xx);
+    ITDebugLog(@"AE Descriptor: %s", *xx);
     DisposeHandle(xx);
 }
 
 
 - (AEArrayDataPointer)sendAEWithRequestedKeyForArray:(NSString*)key eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
 {
     DisposeHandle(xx);
 }
 
 
 - (AEArrayDataPointer)sendAEWithRequestedKeyForArray:(NSString*)key eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
 {
-    //Add error checking...
     AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
     AEEventID   eID    = *((unsigned long*)[eventID UTF8String]);
 
     AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
     AEEventID   eID    = *((unsigned long*)[eventID UTF8String]);
 
-    const char *sendString = [[NSString stringWithFormat:@"'----':obj { form:'prop', want:type('prop'), seld:type('%s'), from:'null'() }", [key UTF8String]] 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;
     AEArrayDataPointer result = nil;
+    int pid;
 
     AppleEvent sendEvent, replyEvent;
 
     AppleEvent sendEvent, replyEvent;
-    AEIdleUPP upp = NewAEIdleUPP(&MyAEIdleCallback);
 
 
-    DescType resultType;
-    Size resultSize, charResultSize;
 
     AEBuildError buildError;
     OSStatus err;
 
     AEBuildError buildError;
     OSStatus err;
-    OSErr err2, err3;
-
-    //NSLog(@"_sendString: %s", sendString);
 
 
+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);
 
     err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
 
-    //[self printCarbonDesc:&sendEvent];
+    [self printCarbonDesc:&sendEvent];
 
     if (err) {
 
     if (err) {
-        NSLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[sendString substringToIndex:buildError.fErrorPos]);
+        ITDebugLog(@"Error: %d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[nssendString substringToIndex:buildError.fErrorPos]);
     }
 
     }
 
-    err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, upp, NULL);
 
 
-    //[self printCarbonDesc:&replyEvent];
+    err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, /*kAEDefaultTimeout*/30, idleUPP, NULL);
+
+    [self printCarbonDesc:&replyEvent];
 
     if (err) {
 
     if (err) {
-        NSLog(@"Send Error: %i",err);
+        ITDebugLog(@"Send Error: %i",err);
     } else {
           SInt32 count, resultCount;
 
           AECountItems(&replyEvent,&count);
     } else {
           SInt32 count, resultCount;
 
           AECountItems(&replyEvent,&count);
-          result=malloc(sizeof(AEDesc)*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);
           AEGetArray(&replyEvent, kAEDescArray, result, sizeof(AEDesc)*count, NULL, NULL, &resultCount);
 
         free(result);
@@ -431,4 +514,182 @@ static ITAppleEventCenter *_sharedAECenter = nil;
     return result;
 }
 
     return result;
 }
 
-@end
+- (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, /*kAEDefaultTimeout*/30, 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, /*kAEDefaultTimeout*/30, 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, /*kAEDefaultTimeout*/30, 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