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) {
49 AppleEvent sendEvent, replyEvent;
54 AEBuildError buildError;
57 if ((GetProcessPID(psn, &pid) == noErr) && (pid == 0)) {
58 ITDebugLog(@"ITSendAEWithString(%@, %@, %@, {%i, %i}): Error getting PID of application.", sendString, NSStringFromFourCharCode(evClass), NSStringFromFourCharCode(evID), psn->highLongOfPSN, psn->lowLongOfPSN);
62 berr = AEBuildAppleEvent(evClass, evID, typeProcessSerialNumber,psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, [sendString UTF8String]);
65 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]);
69 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, /*kAEDefaultTimeout*/60, NULL, NULL);
70 AEDisposeDesc(&sendEvent);
73 ITDebugLog(@"ITSendAEWithString(%@, %@, %@, {%i, %i}): Send Error: %i", sendString, NSStringFromFourCharCode(evClass), NSStringFromFourCharCode(evID), psn->highLongOfPSN, psn->lowLongOfPSN, err);
77 err = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
79 if (resultSize == 0 || err != 0) {
80 AEDisposeDesc(&replyEvent);
84 AEGetParamDesc(&replyEvent, keyDirectObject, resultType, &resultDesc);
85 AEDisposeDesc(&replyEvent);
86 return [[[NSAppleEventDescriptor alloc] initWithAEDescNoCopy:&resultDesc] autorelease];
89 NSAppleEventDescriptor *ITSendAEWithStringAndObject(NSString *sendString, const AEDesc *object, FourCharCode evClass, FourCharCode evID, const ProcessSerialNumber *psn) {
91 AppleEvent sendEvent, replyEvent;
92 NSAppleEventDescriptor *recv;
97 AEBuildError buildError;
100 if ((GetProcessPID(psn, &pid) == noErr) && (pid == 0)) {
101 ITDebugLog(@"ITSendAEWithStringAndObject(%@, <%@>, %@, %@, {%i, %i}): Error getting PID of application.", sendString, _ITAEDescCarbonDescription(*object), NSStringFromFourCharCode(evClass), NSStringFromFourCharCode(evID), psn->highLongOfPSN, psn->lowLongOfPSN);
105 berr = AEBuildAppleEvent(evClass, evID, typeProcessSerialNumber,psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, [sendString UTF8String]);
108 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]);
112 AEPutParamDesc(&sendEvent, keyDirectObject, object);
114 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, /*kAEDefaultTimeout*/60, NULL, NULL);
115 AEDisposeDesc(&sendEvent);
118 ITDebugLog(@"ITSendAEWithStringAndObject(%@, <%@>, %@, %@, {%i, %i}): Send Error: %i", sendString, _ITAEDescCarbonDescription(*object), NSStringFromFourCharCode(evClass), NSStringFromFourCharCode(evID), psn->highLongOfPSN, psn->lowLongOfPSN, err);
122 err = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
124 if (resultSize == 0 || err != 0) {
125 AEDisposeDesc(&replyEvent);
129 AEGetParamDesc(&replyEvent, keyDirectObject, resultType, &resultDesc);
130 AEDisposeDesc(&replyEvent);
131 return [[[NSAppleEventDescriptor alloc] initWithAEDescNoCopy:&resultDesc] autorelease];