Uses slightly less CPU now. Still too much.
[ITFoundation.git] / ITAppleEventCenter.m
index 11671b5..fa7dc28 100755 (executable)
@@ -95,7 +95,7 @@ static ITAppleEventCenter *_sharedAECenter = nil;
             if (err3) {
                 NSLog(@"Error After AEGetParamPtr: %i", err3);
             } else {
             if (err3) {
                 NSLog(@"Error After AEGetParamPtr: %i", err3);
             } else {
-                _finalString = [[NSString stringWithCharacters:result length:charResultSize/sizeof(unichar)] copy];
+                _finalString = [NSString stringWithCharacters:result length:charResultSize/sizeof(unichar)];
             }
         }
         free(result);
             }
         }
         free(result);
@@ -156,8 +156,6 @@ static ITAppleEventCenter *_sharedAECenter = nil;
 
             if (err3) {
                 NSLog(@"Error After AEGetParamPtr: %i", err3);
 
             if (err3) {
                 NSLog(@"Error After AEGetParamPtr: %i", err3);
-            } else {
-                NSLog(@"%i", result);
             }
         }
     }
             }
         }
     }
@@ -221,7 +219,7 @@ static ITAppleEventCenter *_sharedAECenter = nil;
             if (err3) {
                 NSLog(@"Error After AEGetParamPtr: %i", err3);
             } else {
             if (err3) {
                 NSLog(@"Error After AEGetParamPtr: %i", err3);
             } else {
-                _finalString = [[NSString stringWithCharacters:result length:charResultSize/sizeof(unichar)] copy];
+                _finalString = [NSString stringWithCharacters:result length:charResultSize/sizeof(unichar)];
             }
         }
         free(result);
             }
         }
         free(result);
@@ -282,8 +280,6 @@ static ITAppleEventCenter *_sharedAECenter = nil;
 
             if (err3) {
                 NSLog(@"Error After AEGetParamPtr: %i", err3);
 
             if (err3) {
                 NSLog(@"Error After AEGetParamPtr: %i", err3);
-            } else {
-                NSLog(@"%i", result);
             }
         }
     }
             }
         }
     }
@@ -355,7 +351,7 @@ static ITAppleEventCenter *_sharedAECenter = nil;
             if (err3) {
                 NSLog(@"Error After AEGetParamPtr: %i", err3);
             } else {
             if (err3) {
                 NSLog(@"Error After AEGetParamPtr: %i", err3);
             } else {
-                _finalString = [[NSString stringWithCharacters:result length:charResultSize/sizeof(unichar)] copy];
+                _finalString = [NSString stringWithCharacters:result length:charResultSize/sizeof(unichar)];
             }
         }
         free(result);
             }
         }
         free(result);
@@ -382,10 +378,10 @@ static ITAppleEventCenter *_sharedAECenter = nil;
 }
 
 - (void)printCarbonDesc:(AEDesc*)desc {
 }
 
 - (void)printCarbonDesc:(AEDesc*)desc {
-    Handle xx;
+    /*Handle xx;
     AEPrintDescToHandle(desc,&xx);
     NSLog(@"Handle: %s", *xx);
     AEPrintDescToHandle(desc,&xx);
     NSLog(@"Handle: %s", *xx);
-    DisposeHandle(xx);
+    DisposeHandle(xx);*/
 }
 
 
 }
 
 
@@ -439,4 +435,63 @@ static ITAppleEventCenter *_sharedAECenter = nil;
     return result;
 }
 
     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];
+    long result = 0;
+
+    AppleEvent sendEvent, replyEvent;
+
+    DescType resultType;
+    Size resultSize, charResultSize;
+    
+    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];
+
+    if (err) {
+        NSLog(@"%d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[sendString substringToIndex:buildError.fErrorPos]);
+    }
+
+    err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, idleUPP, NULL);
+
+    [self printCarbonDesc:&replyEvent];
+
+    if (err) {
+        NSLog(@"Send Error: %i",err);
+    } else {
+        err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
+
+        if (err2) {
+            NSLog(@"Error After AESizeOfParam: %i", err2);
+        } else {
+            err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, &result, resultSize, &charResultSize);
+
+            if (err3) {
+                NSLog(@"Error After AEGetParamPtr: %i", err3);
+            }
+        }
+    }
+
+    AEDisposeDesc(&sendEvent);
+    AEDisposeDesc(&replyEvent);
+
+    return result;
+}
+
 @end
 @end