Fixed the AppleEvent tools to actually work.
[ITFoundation.git] / ITAppleEventTools.m
1 /*
2  *  ITAppleEventTools.c
3  *  ITFoundation
4  *
5  *  Created by Alexander Strange on Wed Feb 11 2004.
6  *  Copyright (c) 2004 __MyCompanyName__. All rights reserved.
7  *
8  */
9
10 #import "ITDebug.h"
11 #import "ITAppleEventTools.h"
12
13 NSAppleEventDescriptor *ITSendAEWithString(NSString *sendString, FourCharCode evClass, FourCharCode evID,const ProcessSerialNumber *psn)
14 {
15     //Add error checking...
16     pid_t pid;
17     
18     const char *usendString = [sendString UTF8String];
19     
20     AppleEvent sendEvent, replyEvent;
21     NSAppleEventDescriptor *send, *recv;
22     
23     AEBuildError buildError;
24     OSStatus berr,err;
25     
26     if ((GetProcessPID(psn, &pid) == noErr) && (pid == 0)) {
27         ITDebugLog(@"Error getting PID of application.");
28         return nil;
29     }
30     
31     berr = AEBuildAppleEvent(evClass, evID, typeProcessSerialNumber,psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, usendString);
32     send = [[[NSAppleEventDescriptor alloc] initWithAEDescNoCopy:&sendEvent] autorelease];
33     if (!berr) [send logDesc];
34     
35     if (berr) {
36         ITDebugLog(@"Error: %d:%d at \"%@\"",(int)buildError.fError,buildError.fErrorPos,[sendString substringToIndex:buildError.fErrorPos]);
37     }
38     
39     err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, NULL, NULL);
40     recv = [[[NSAppleEventDescriptor alloc] initWithAEDescNoCopy:&replyEvent] autorelease];
41     if (!err) [recv logDesc];
42     
43     if (err) {
44         ITDebugLog(@"Send Error: %i",err);
45     }
46     return recv;
47 }
48
49 NSAppleEventDescriptor *ITSendAEWithKey(FourCharCode reqKey, FourCharCode evClass, FourCharCode evID,const ProcessSerialNumber *psn)
50 {
51     return nil;
52 }
53
54 NSAppleEventDescriptor *ITSendAE(FourCharCode eClass, FourCharCode eID,const ProcessSerialNumber *psn)
55 {
56     AEDesc dest;
57     int pid;
58     
59     AppleEvent event, reply;
60     OSStatus cerr,cerr2,err;
61     NSAppleEventDescriptor *nsd, *nse, *nsr;
62     if ((GetProcessPID(psn, &pid) == noErr) && (pid == 0)) {
63         ITDebugLog(@"Error getting PID of application.");
64         return nil;
65     }
66     cerr = AECreateDesc(typeProcessSerialNumber,psn,sizeof(ProcessSerialNumber),&dest);
67     nsd = [[[NSAppleEventDescriptor alloc] initWithAEDescNoCopy:&dest] autorelease];
68     cerr2 = AECreateAppleEvent(eClass,eID,&dest,kAutoGenerateReturnID,kAnyTransactionID,&event);
69     nse = [[[NSAppleEventDescriptor alloc] initWithAEDescNoCopy:&event] autorelease];
70     if (!cerr2) [nse logDesc];
71     err = AESend(&event, &reply, kAENoReply, kAENormalPriority, kAEDefaultTimeout, NULL, NULL);
72     nsr = [[[NSAppleEventDescriptor alloc] initWithAEDescNoCopy:&reply] autorelease];
73     [nsr logDesc];
74     return nsr;
75 }
76
77 @implementation NSAppleEventDescriptor (ITAELogging)
78 -(void) logDesc
79 {
80     Handle xx;
81     AEPrintDescToHandle([self aeDesc],&xx);
82     ITDebugLog(@"AE Descriptor: %s", *xx);
83     DisposeHandle(xx);
84 }
85 @end