5 * Created by Alexander Strange on Wed Feb 11 2004.
6 * Copyright (c) 2004 __MyCompanyName__. All rights reserved.
11 #import "ITAppleEventTools.h"
13 NSAppleEventDescriptor *ITSendAEWithString(NSString *sendString, FourCharCode evClass, FourCharCode evID,const ProcessSerialNumber *psn)
15 //Add error checking...
18 const char *usendString = [sendString UTF8String];
20 AppleEvent sendEvent, replyEvent;
21 NSAppleEventDescriptor *send, *recv;
26 AEBuildError buildError;
29 if ((GetProcessPID(psn, &pid) == noErr) && (pid == 0)) {
30 ITDebugLog(@"Error getting PID of application.");
34 berr = AEBuildAppleEvent(evClass, evID, typeProcessSerialNumber,psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, usendString);
35 send = [[[NSAppleEventDescriptor alloc] initWithAEDescNoCopy:&sendEvent] autorelease];
36 if (!berr) [send logDesc];
39 ITDebugLog(@"Error: %d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[sendString substringToIndex:buildError.fErrorPos]);
42 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, /*kAEDefaultTimeout*/60, NULL, NULL);
44 err = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
45 if (resultSize == 0 || err != 0) {
49 AEGetParamDesc(&replyEvent, keyDirectObject, resultType, &resultDesc);
51 recv = [[[NSAppleEventDescriptor alloc] initWithAEDescNoCopy:&resultDesc] autorelease];
52 [[[NSAppleEventDescriptor alloc] initWithAEDescNoCopy:&replyEvent] autorelease];
53 if (!err) [recv logDesc];
56 ITDebugLog(@"Send Error: %i",err);
61 NSAppleEventDescriptor *ITSendAEWithStringAndObject(NSString *sendString, const AEDesc *object, FourCharCode evClass, FourCharCode evID,const ProcessSerialNumber *psn)
63 //Add error checking...
66 const char *usendString = [sendString UTF8String];
68 AppleEvent sendEvent, replyEvent;
69 NSAppleEventDescriptor *send, *recv;
74 AEBuildError buildError;
77 if ((GetProcessPID(psn, &pid) == noErr) && (pid == 0)) {
78 ITDebugLog(@"Error getting PID of application.");
82 berr = AEBuildAppleEvent(evClass, evID, typeProcessSerialNumber,psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, usendString);
83 send = [[[NSAppleEventDescriptor alloc] initWithAEDescNoCopy:&sendEvent] autorelease];
84 if (!berr) [send logDesc];
87 ITDebugLog(@"Error: %d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[sendString substringToIndex:buildError.fErrorPos]);
89 err = AEPutParamDesc(&sendEvent, keyDirectObject, object);
90 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, /*kAEDefaultTimeout*/60, NULL, NULL);
92 err = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
93 if (resultSize == 0 || err != 0) {
97 AEGetParamDesc(&replyEvent, keyDirectObject, resultType, &resultDesc);
99 recv = [[[NSAppleEventDescriptor alloc] initWithAEDescNoCopy:&resultDesc] autorelease];
100 [[[NSAppleEventDescriptor alloc] initWithAEDescNoCopy:&replyEvent] autorelease];
101 if (!err) [recv logDesc];
104 ITDebugLog(@"Send Error: %i",err);
109 NSAppleEventDescriptor *ITSendAEWithKey(FourCharCode reqKey, FourCharCode evClass, FourCharCode evID,const ProcessSerialNumber *psn)
111 //Add error checking...
113 NSString *sendString = [NSString stringWithFormat:@"'----':obj { form:'prop', want:type('prop'), seld:type('%c%c%c%c'), from:'null'() }", (reqKey >> 24), (reqKey >> 16) & 0xff, (reqKey >> 8) & 0xff, reqKey & 0xff];
114 const char *usendString = [sendString UTF8String];
116 AppleEvent sendEvent, replyEvent;
117 NSAppleEventDescriptor *send, *recv;
122 AEBuildError buildError;
125 if ((GetProcessPID(psn, &pid) == noErr) && (pid == 0)) {
126 ITDebugLog(@"Error getting PID of application.");
130 berr = AEBuildAppleEvent(evClass, evID, typeProcessSerialNumber,psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, usendString);
131 send = [[[NSAppleEventDescriptor alloc] initWithAEDescNoCopy:&sendEvent] autorelease];
132 if (!berr) [send logDesc];
135 ITDebugLog(@"Error: %d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[sendString substringToIndex:buildError.fErrorPos]);
138 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, /*kAEDefaultTimeout*/60, NULL, NULL);
140 err = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
141 if (resultSize == 0 || err != 0) {
145 AEGetParamDesc(&replyEvent, keyDirectObject, resultType, &resultDesc);
147 recv = [[[NSAppleEventDescriptor alloc] initWithAEDescNoCopy:&resultDesc] autorelease];
148 [[[NSAppleEventDescriptor alloc] initWithAEDescNoCopy:&replyEvent] autorelease];
149 if (!err) [recv logDesc];
152 ITDebugLog(@"Send Error: %i",err);
157 NSAppleEventDescriptor *ITSendAE(FourCharCode eClass, FourCharCode eID,const ProcessSerialNumber *psn)
162 AppleEvent event, reply;
163 OSStatus cerr,cerr2,err;
164 NSAppleEventDescriptor *nsd, *nse, *nsr;
165 if ((GetProcessPID(psn, &pid) == noErr) && (pid == 0)) {
166 ITDebugLog(@"Error getting PID of application.");
169 cerr = AECreateDesc(typeProcessSerialNumber,psn,sizeof(ProcessSerialNumber),&dest);
170 nsd = [[[NSAppleEventDescriptor alloc] initWithAEDescNoCopy:&dest] autorelease];
171 cerr2 = AECreateAppleEvent(eClass,eID,&dest,kAutoGenerateReturnID,kAnyTransactionID,&event);
172 nse = [[[NSAppleEventDescriptor alloc] initWithAEDescNoCopy:&event] autorelease];
173 if (!cerr2) [nse logDesc];
174 err = AESend(&event, &reply, kAENoReply, kAENormalPriority, /*kAEDefaultTimeout*/60, NULL, NULL);
175 nsr = [[[NSAppleEventDescriptor alloc] initWithAEDescNoCopy:&reply] autorelease];
180 @implementation NSAppleEventDescriptor (ITAELogging)
184 AEPrintDescToHandle([self aeDesc],&xx);
185 ITDebugLog(@"AE Descriptor: %s", *xx);