Licensing ITMac under the GNU Lesser General Public License version 2.1.
[ITMac.git] / ITAppleEventTools.m
index 81f858a..163ff0a 100644 (file)
@@ -30,11 +30,13 @@ NSAppleEventDescriptor *ITSendAE(FourCharCode eClass, FourCharCode eID, const Pr
        }
        
        err = AESend(&event, &reply, kAENoReply, kAENormalPriority, /*kAEDefaultTimeout*/60, NULL, NULL);
+       AEDisposeDesc(&dest);
+       AEDisposeDesc(&event);
        
        if (err) {
+               AEDisposeDesc(&reply);
                return nil;
        }
-       
        return [[[NSAppleEventDescriptor alloc] initWithAEDescNoCopy:&reply] autorelease];
 }
 
@@ -43,6 +45,10 @@ NSAppleEventDescriptor *ITSendAEWithKey(FourCharCode reqKey, FourCharCode evClas
 }
 
 NSAppleEventDescriptor *ITSendAEWithString(NSString *sendString, FourCharCode evClass, FourCharCode evID, const ProcessSerialNumber *psn) {
+       return ITSendAEWithStringAndTimeout(sendString, evClass, evID, psn, 60);
+}
+
+NSAppleEventDescriptor *ITSendAEWithStringAndTimeout(NSString *sendString, FourCharCode evClass, FourCharCode evID, const ProcessSerialNumber *psn, long timeout) {
        pid_t pid;
        AppleEvent sendEvent, replyEvent;
        AEDesc resultDesc;
@@ -64,7 +70,8 @@ NSAppleEventDescriptor *ITSendAEWithString(NSString *sendString, FourCharCode ev
                return nil;
        }
        
-       err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, /*kAEDefaultTimeout*/60, NULL, NULL);
+       err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, /*kAEDefaultTimeout*/timeout, NULL, NULL);
+       AEDisposeDesc(&sendEvent);
        
        if (err) {
                ITDebugLog(@"ITSendAEWithString(%@, %@, %@, {%i, %i}): Send Error: %i", sendString, NSStringFromFourCharCode(evClass), NSStringFromFourCharCode(evID), psn->highLongOfPSN, psn->lowLongOfPSN, err);
@@ -74,11 +81,12 @@ NSAppleEventDescriptor *ITSendAEWithString(NSString *sendString, FourCharCode ev
        err = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
        
        if (resultSize == 0 || err != 0) {
+               AEDisposeDesc(&replyEvent);
                return nil;
        }
        
        AEGetParamDesc(&replyEvent, keyDirectObject, resultType, &resultDesc);
-       
+       AEDisposeDesc(&replyEvent);
        return [[[NSAppleEventDescriptor alloc] initWithAEDescNoCopy:&resultDesc] autorelease];
 }
 
@@ -108,6 +116,7 @@ NSAppleEventDescriptor *ITSendAEWithStringAndObject(NSString *sendString, const
        AEPutParamDesc(&sendEvent, keyDirectObject, object);
        
        err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, /*kAEDefaultTimeout*/60, NULL, NULL);
+       AEDisposeDesc(&sendEvent);
        
        if (err) {
                ITDebugLog(@"ITSendAEWithStringAndObject(%@, <%@>, %@, %@, {%i, %i}): Send Error: %i", sendString, _ITAEDescCarbonDescription(*object), NSStringFromFourCharCode(evClass), NSStringFromFourCharCode(evID), psn->highLongOfPSN, psn->lowLongOfPSN, err);
@@ -117,10 +126,11 @@ NSAppleEventDescriptor *ITSendAEWithStringAndObject(NSString *sendString, const
        err = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
        
        if (resultSize == 0 || err != 0) {
+               AEDisposeDesc(&replyEvent);
                return nil;
        }
        
        AEGetParamDesc(&replyEvent, keyDirectObject, resultType, &resultDesc);
-       
+       AEDisposeDesc(&replyEvent);
        return [[[NSAppleEventDescriptor alloc] initWithAEDescNoCopy:&resultDesc] autorelease];
 }
\ No newline at end of file