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*/45, 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 if (!err) [recv logDesc];
55 ITDebugLog(@"Send Error: %i",err);
60 NSAppleEventDescriptor *ITSendAEWithStringAndObject(NSString *sendString, const AEDesc *object, FourCharCode evClass, FourCharCode evID,const ProcessSerialNumber *psn)
62 //Add error checking...
65 const char *usendString = [sendString UTF8String];
67 AppleEvent sendEvent, replyEvent;
68 NSAppleEventDescriptor *send, *recv;
73 AEBuildError buildError;
76 if ((GetProcessPID(psn, &pid) == noErr) && (pid == 0)) {
77 ITDebugLog(@"Error getting PID of application.");
81 berr = AEBuildAppleEvent(evClass, evID, typeProcessSerialNumber,psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, usendString);
82 send = [[[NSAppleEventDescriptor alloc] initWithAEDescNoCopy:&sendEvent] autorelease];
83 if (!berr) [send logDesc];
86 ITDebugLog(@"Error: %d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[sendString substringToIndex:buildError.fErrorPos]);
88 err = AEPutParamDesc(&sendEvent, keyDirectObject, object);
89 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, /*kAEDefaultTimeout*/45, NULL, NULL);
91 err = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
92 if (resultSize == 0 || err != 0) {
96 AEGetParamDesc(&replyEvent, keyDirectObject, resultType, &resultDesc);
98 recv = [[[NSAppleEventDescriptor alloc] initWithAEDescNoCopy:&resultDesc] autorelease];
99 if (!err) [recv logDesc];
102 ITDebugLog(@"Send Error: %i",err);
107 NSAppleEventDescriptor *ITSendAEWithKey(FourCharCode reqKey, FourCharCode evClass, FourCharCode evID,const ProcessSerialNumber *psn)
109 //Add error checking...
111 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];
112 const char *usendString = [sendString UTF8String];
114 AppleEvent sendEvent, replyEvent;
115 NSAppleEventDescriptor *send, *recv;
120 AEBuildError buildError;
123 if ((GetProcessPID(psn, &pid) == noErr) && (pid == 0)) {
124 ITDebugLog(@"Error getting PID of application.");
128 berr = AEBuildAppleEvent(evClass, evID, typeProcessSerialNumber,psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, usendString);
129 send = [[[NSAppleEventDescriptor alloc] initWithAEDescNoCopy:&sendEvent] autorelease];
130 if (!berr) [send logDesc];
133 ITDebugLog(@"Error: %d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[sendString substringToIndex:buildError.fErrorPos]);
136 err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, /*kAEDefaultTimeout*/45, NULL, NULL);
138 err = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
139 if (resultSize == 0 || err != 0) {
143 AEGetParamDesc(&replyEvent, keyDirectObject, resultType, &resultDesc);
145 recv = [[[NSAppleEventDescriptor alloc] initWithAEDescNoCopy:&resultDesc] autorelease];
146 if (!err) [recv logDesc];
149 ITDebugLog(@"Send Error: %i",err);
154 NSAppleEventDescriptor *ITSendAE(FourCharCode eClass, FourCharCode eID,const ProcessSerialNumber *psn)
159 AppleEvent event, reply;
160 OSStatus cerr,cerr2,err;
161 NSAppleEventDescriptor *nsd, *nse, *nsr;
162 if ((GetProcessPID(psn, &pid) == noErr) && (pid == 0)) {
163 ITDebugLog(@"Error getting PID of application.");
166 cerr = AECreateDesc(typeProcessSerialNumber,psn,sizeof(ProcessSerialNumber),&dest);
167 nsd = [[[NSAppleEventDescriptor alloc] initWithAEDescNoCopy:&dest] autorelease];
168 cerr2 = AECreateAppleEvent(eClass,eID,&dest,kAutoGenerateReturnID,kAnyTransactionID,&event);
169 nse = [[[NSAppleEventDescriptor alloc] initWithAEDescNoCopy:&event] autorelease];
170 if (!cerr2) [nse logDesc];
171 err = AESend(&event, &reply, kAENoReply, kAENormalPriority, /*kAEDefaultTimeout*/45, NULL, NULL);
172 nsr = [[[NSAppleEventDescriptor alloc] initWithAEDescNoCopy:&reply] autorelease];
177 @implementation NSAppleEventDescriptor (ITAELogging)
181 AEPrintDescToHandle([self aeDesc],&xx);
182 ITDebugLog(@"AE Descriptor: %s", *xx);