Updating ITMac header to include public headers.
[ITMac.git] / ITAppleEventTools.m
1 #import "ITAppleEventTools.h"
2 #import <ITFoundation/ITCarbonSupport.h>
3
4 NSString *_ITAEDescCarbonDescription(AEDesc desc) {
5         Handle descHandle;
6         NSString *carbonDescription;
7         AEPrintDescToHandle(&desc,&descHandle);
8         carbonDescription = [NSString stringWithUTF8String:*descHandle];
9         DisposeHandle(descHandle);
10         return carbonDescription;
11 }
12
13 NSAppleEventDescriptor *ITSendAE(FourCharCode eClass, FourCharCode eID, const ProcessSerialNumber *psn) {
14         AEDesc dest;
15         int pid;
16         AppleEvent event, reply;
17         OSStatus cerr, err;
18         NSAppleEventDescriptor *cocoaReply;
19         
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);
22                 return nil;
23         }
24         
25         AECreateDesc(typeProcessSerialNumber, psn,sizeof(ProcessSerialNumber),&dest);
26         cerr = AECreateAppleEvent(eClass,eID,&dest,kAutoGenerateReturnID,kAnyTransactionID,&event);
27         
28         if (cerr) {
29                 return nil;
30         }
31         
32         err = AESend(&event, &reply, kAENoReply, kAENormalPriority, /*kAEDefaultTimeout*/60, NULL, NULL);
33         
34         if (err) {
35                 return nil;
36         }
37         
38         return [[[NSAppleEventDescriptor alloc] initWithAEDescNoCopy:&reply] autorelease];
39 }
40
41 NSAppleEventDescriptor *ITSendAEWithKey(FourCharCode reqKey, FourCharCode evClass, FourCharCode evID, const ProcessSerialNumber *psn) {
42         return ITSendAEWithString([NSString stringWithFormat:@"'----':obj { form:'prop', want:type('prop'), seld:type('%@'), from:'null'() }", NSStringFromFourCharCode(reqKey)], evClass, evID, psn);
43 }
44
45 NSAppleEventDescriptor *ITSendAEWithString(NSString *sendString, FourCharCode evClass, FourCharCode evID, const ProcessSerialNumber *psn) {
46         pid_t pid;
47         AppleEvent sendEvent, replyEvent;
48         AEDesc resultDesc;
49         DescType resultType;
50         Size resultSize;
51         
52         AEBuildError buildError;
53         OSStatus berr,err;
54         
55         if ((GetProcessPID(psn, &pid) == noErr) && (pid == 0)) {
56                 ITDebugLog(@"ITSendAEWithString(%@, %@, %@, {%i, %i}): Error getting PID of application.", sendString, NSStringFromFourCharCode(evClass), NSStringFromFourCharCode(evID), psn->highLongOfPSN, psn->lowLongOfPSN);
57                 return nil;
58         }
59         
60         berr = AEBuildAppleEvent(evClass, evID, typeProcessSerialNumber,psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, [sendString UTF8String]);
61         
62         if (berr) {
63                 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]);
64                 return nil;
65         }
66         
67         err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, /*kAEDefaultTimeout*/60, NULL, NULL);
68         
69         if (err) {
70                 ITDebugLog(@"ITSendAEWithString(%@, %@, %@, {%i, %i}): Send Error: %i", sendString, NSStringFromFourCharCode(evClass), NSStringFromFourCharCode(evID), psn->highLongOfPSN, psn->lowLongOfPSN, err);
71                 return nil;
72         }
73         
74         err = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
75         
76         if (resultSize == 0 || err != 0) {
77                 return nil;
78         }
79         
80         AEGetParamDesc(&replyEvent, keyDirectObject, resultType, &resultDesc);
81         
82         return [[[NSAppleEventDescriptor alloc] initWithAEDescNoCopy:&resultDesc] autorelease];
83 }
84
85 NSAppleEventDescriptor *ITSendAEWithStringAndObject(NSString *sendString, const AEDesc *object, FourCharCode evClass, FourCharCode evID, const ProcessSerialNumber *psn) {
86         pid_t pid;
87         AppleEvent sendEvent, replyEvent;
88         NSAppleEventDescriptor *recv;
89         AEDesc resultDesc;
90         DescType resultType;
91         Size resultSize;
92         
93         AEBuildError buildError;
94         OSStatus berr,err;
95         
96         if ((GetProcessPID(psn, &pid) == noErr) && (pid == 0)) {
97                 ITDebugLog(@"ITSendAEWithStringAndObject(%@, <%@>, %@, %@, {%i, %i}): Error getting PID of application.", sendString, _ITAEDescCarbonDescription(*object), NSStringFromFourCharCode(evClass), NSStringFromFourCharCode(evID), psn->highLongOfPSN, psn->lowLongOfPSN);
98                 return nil;
99         }
100         
101         berr = AEBuildAppleEvent(evClass, evID, typeProcessSerialNumber,psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, [sendString UTF8String]);
102         
103         if (berr) {
104                 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]);
105                 return nil;
106         }
107         
108         AEPutParamDesc(&sendEvent, keyDirectObject, object);
109         
110         err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, /*kAEDefaultTimeout*/60, NULL, NULL);
111         
112         if (err) {
113                 ITDebugLog(@"ITSendAEWithStringAndObject(%@, <%@>, %@, %@, {%i, %i}): Send Error: %i", sendString, _ITAEDescCarbonDescription(*object), NSStringFromFourCharCode(evClass), NSStringFromFourCharCode(evID), psn->highLongOfPSN, psn->lowLongOfPSN, err);
114                 return nil;
115         }
116         
117         err = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
118         
119         if (resultSize == 0 || err != 0) {
120                 return nil;
121         }
122         
123         AEGetParamDesc(&replyEvent, keyDirectObject, resultType, &resultDesc);
124         
125         return [[[NSAppleEventDescriptor alloc] initWithAEDescNoCopy:&resultDesc] autorelease];
126 }