1 #import "ITAppleEventTools.h"
2 #import <ITFoundation/ITCarbonSupport.h>
4 NSString *_ITAEDescCarbonDescription(AEDesc desc) {
6 NSString *carbonDescription;
7 AEPrintDescToHandle(&desc,&descHandle);
8 carbonDescription = [NSString stringWithUTF8String:*descHandle];
9 DisposeHandle(descHandle);
10 return carbonDescription;
13 NSAppleEventDescriptor *ITSendAE(FourCharCode eClass, FourCharCode eID, const ProcessSerialNumber *psn) {
16 AppleEvent event, reply;
18 NSAppleEventDescriptor *cocoaReply;
20 if ((GetProcessPID(psn, &pid) == noErr) && (pid == 0)) {
21 ITDebugLog(@"ITSendAE(%@, %@, {%i, %i}): Error getting PID of application.", NSStringFromFourCharCode(eClass), NSStringFromFourCharCode(eID), psn->highLongOfPSN, psn->lowLongOfPSN);
25 AECreateDesc(typeProcessSerialNumber, psn,sizeof(ProcessSerialNumber),&dest);
26 cerr = AECreateAppleEvent(eClass,eID,&dest,kAutoGenerateReturnID,kAnyTransactionID,&event);
32 err = AESend(&event, &reply, kAENoReply, kAENormalPriority, /*kAEDefaultTimeout*/60, NULL, NULL);
34 AEDisposeDesc(&event);
37 AEDisposeDesc(&reply);
40 return [[[NSAppleEventDescriptor alloc] initWithAEDescNoCopy:&reply] autorelease];
43 NSAppleEventDescriptor *ITSendAEWithKey(FourCharCode reqKey, FourCharCode evClass, FourCharCode evID, const ProcessSerialNumber *psn) {
44 return ITSendAEWithString([NSString stringWithFormat:@"'----':obj { form:'prop', want:type('prop'), seld:type('%@'), from:'null'() }", NSStringFromFourCharCode(reqKey)], evClass, evID, psn);
47 NSAppleEventDescriptor *ITSendAEWithString(NSString *sendString, FourCharCode evClass, FourCharCode evID, const ProcessSerialNumber *psn) {
48 return ITSendAEWithStringAndTimeout(sendString, evClass, evID, psn, 60);
51 NSAppleEventDescriptor *ITSendAEWithStringAndTimeout(NSString *sendString, FourCharCode evClass, FourCharCode evID, const ProcessSerialNumber *psn, long timeout) {
53 AppleEvent sendEvent, replyEvent;
58 AEBuildError buildError;
61 if ((GetProcessPID(psn, &pid) == noErr) && (pid == 0)) {
62 ITDebugLog(@"ITSendAEWithString(%@, %@, %@, {%i, %i}): Error getting PID of application.", sendString, NSStringFromFourCharCode(evClass), NSStringFromFourCharCode(evID), psn->highLongOfPSN, psn->lowLongOfPSN);
66 berr = AEBuildAppleEvent(evClass, evID, typeProcessSerialNumber,psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, [sendString UTF8String]);
69 ITDebugLog(@"ITSendAEWithString(%@, %@, %@, {%i, %i}): Build Error: %d:%d at \"%@\"", sendString, NSStringFromFourCharCode(evClass), NSStringFromFourCharCode(evID), psn->highLongOfPSN, psn->lowLongOfPSN, (int)buildError.fError, buildError.fErrorPos, [sendString substringToIndex:buildError.fErrorPos]);
73 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, /*kAEDefaultTimeout*/timeout, NULL, NULL);
74 AEDisposeDesc(&sendEvent);
77 ITDebugLog(@"ITSendAEWithString(%@, %@, %@, {%i, %i}): Send Error: %i", sendString, NSStringFromFourCharCode(evClass), NSStringFromFourCharCode(evID), psn->highLongOfPSN, psn->lowLongOfPSN, err);
81 err = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
83 if (resultSize == 0 || err != 0) {
84 AEDisposeDesc(&replyEvent);
88 AEGetParamDesc(&replyEvent, keyDirectObject, resultType, &resultDesc);
89 AEDisposeDesc(&replyEvent);
90 return [[[NSAppleEventDescriptor alloc] initWithAEDescNoCopy:&resultDesc] autorelease];
93 NSAppleEventDescriptor *ITSendAEWithStringAndObject(NSString *sendString, const AEDesc *object, FourCharCode evClass, FourCharCode evID, const ProcessSerialNumber *psn) {
95 AppleEvent sendEvent, replyEvent;
96 NSAppleEventDescriptor *recv;
101 AEBuildError buildError;
104 if ((GetProcessPID(psn, &pid) == noErr) && (pid == 0)) {
105 ITDebugLog(@"ITSendAEWithStringAndObject(%@, <%@>, %@, %@, {%i, %i}): Error getting PID of application.", sendString, _ITAEDescCarbonDescription(*object), NSStringFromFourCharCode(evClass), NSStringFromFourCharCode(evID), psn->highLongOfPSN, psn->lowLongOfPSN);
109 berr = AEBuildAppleEvent(evClass, evID, typeProcessSerialNumber,psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, [sendString UTF8String]);
112 ITDebugLog(@"ITSendAEWithStringAndObject(%@, <%@>, %@, %@, {%i, %i}): Build Error: %d:%d at \"%@\"", sendString, _ITAEDescCarbonDescription(*object), NSStringFromFourCharCode(evClass), NSStringFromFourCharCode(evID), psn->highLongOfPSN, psn->lowLongOfPSN, (int)buildError.fError, buildError.fErrorPos, [sendString substringToIndex:buildError.fErrorPos]);
116 AEPutParamDesc(&sendEvent, keyDirectObject, object);
118 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, /*kAEDefaultTimeout*/60, NULL, NULL);
119 AEDisposeDesc(&sendEvent);
122 ITDebugLog(@"ITSendAEWithStringAndObject(%@, <%@>, %@, %@, {%i, %i}): Send Error: %i", sendString, _ITAEDescCarbonDescription(*object), NSStringFromFourCharCode(evClass), NSStringFromFourCharCode(evID), psn->highLongOfPSN, psn->lowLongOfPSN, err);
126 err = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
128 if (resultSize == 0 || err != 0) {
129 AEDisposeDesc(&replyEvent);
133 AEGetParamDesc(&replyEvent, keyDirectObject, resultType, &resultDesc);
134 AEDisposeDesc(&replyEvent);
135 return [[[NSAppleEventDescriptor alloc] initWithAEDescNoCopy:&resultDesc] autorelease];