Some Error Checking. Also, added a method for sending non-returning AppleEvents.
[ITFoundation.git] / ITAppleEventCenter.m
1 #import "ITAppleEventCenter.h"
2
3 Boolean MyAEIdleCallback (
4                                          EventRecord * theEvent,
5                                          SInt32 * sleepTime,
6                                          RgnHandle * mouseRgn
7                                          );
8
9 Boolean MyAEIdleCallback (
10                                          EventRecord * theEvent,
11                                          SInt32 * sleepTime,
12                                          RgnHandle * mouseRgn
13                                          )
14 {
15     return FALSE;
16 }
17
18 @implementation ITAppleEventCenter
19
20 static ITAppleEventCenter *_sharedAECenter = nil;
21
22 + (id)sharedCenter
23 {
24     if( _sharedAECenter != nil ) {
25         return _sharedAECenter;
26     } else {    
27         _sharedAECenter = [[ITAppleEventCenter alloc] init];
28         return _sharedAECenter;
29     }
30 }
31
32 - (NSString*)sendAEWithRequestedKey:(NSString*)key eventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
33 {
34     if ( (!key) || (!eventClass) || (!eventID) || (!psn) ) {
35         return @"";
36     } else {
37     AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
38     AEEventID    eID    = *((unsigned long*)[eventID UTF8String]);
39     
40     const char *sendString = [[NSString stringWithFormat:@"'----':obj { form:'prop', want:type('prop'), seld:type('%s'), from:'null'() }", [key UTF8String]] UTF8String];
41     NSString  *_finalString;
42     
43     AppleEvent sendEvent, replyEvent;
44     AEIdleUPP upp = NewAEIdleUPP(&MyAEIdleCallback);
45     
46     char *charResult;
47     DescType resultType;
48     Size resultSize, charResultSize;
49     int pid;
50     
51     AEBuildError buildError;
52     OSStatus err;
53     OSErr err2, err3;
54     
55     if (GetProcessPID(&psn, &pid) == noErr) {
56         if (pid ==0) {
57             NSLog(@"Process doesn't exist! Exiting.");
58             return;
59         }
60     } else {
61         NSLog(@"Error getting PID of application to send to! Exiting.");
62         return;
63     }
64     
65     NSLog(@"_sendString: %s", sendString);
66     
67     err = AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, 0, &sendEvent, &buildError, sendString);
68     
69     [self printCarbonDesc:&sendEvent];
70     
71     if (err) {
72         NSLog(@"%d:%d at \"%@\"",buildError.fError,buildError.fErrorPos,[sendString substringToIndex:buildError.fErrorPos]);
73     }
74     
75     err = AESend(&sendEvent, &replyEvent, kAEWaitReply, kAENormalPriority, kNoTimeOut, upp, NULL);
76     
77     [self printCarbonDesc:&replyEvent];
78     
79     if (err) {
80         NSLog(@"Send Error: %i",err);
81     } else {
82
83         unichar* result=0;
84         
85         err2 = AESizeOfParam(&replyEvent, keyDirectObject, &resultType, &resultSize);
86         result=malloc(resultSize);
87         
88         if (err2) { NSLog(@"Error After AESizeOfParam: %i", err2); } else {
89         
90         err3 = AEGetParamPtr(&replyEvent, keyDirectObject, resultType, NULL, result, resultSize, &charResultSize);
91         
92         if (err3) { NSLog(@"Error After AEGetParamPtr: %i", err3); } else {
93         
94         _finalString = [[NSString stringWithCharacters:result length:charResultSize/sizeof(unichar)] copy];
95         
96         }
97         }
98     }
99     
100     AEDisposeDesc(&sendEvent);
101     AEDisposeDesc(&replyEvent);
102     
103     return _finalString;
104     }
105 }
106
107 - (void)sendAEWithEventClass:(NSString*)eventClass eventID:(NSString*)eventID appPSN:(ProcessSerialNumber)psn
108 {
109     if ( (!eventClass) || (!eventID) || (!psn) ) {
110         return;
111     } else {
112         AEEventClass eClass = *((unsigned long*)[eventClass UTF8String]);
113         AEEventID    eID    = *((unsigned long*)[eventID UTF8String]);
114         AppleEvent event, reply;
115     
116         AEBuildAppleEvent(eClass, eID, typeProcessSerialNumber,(ProcessSerialNumber*)&psn, sizeof(ProcessSerialNumber), kAutoGenerateReturnID, kAnyTransactionID, &event, nil, "");
117     
118         AESend(&event, &reply, kAENoReply, kAENormalPriority, kAEDefaultTimeout, nil, nil);
119     
120         AEDisposeDesc(&event);
121         AEDisposeDesc(&reply);
122     }
123 }
124
125 - (void)printCarbonDesc:(AEDesc*)desc {
126     Handle xx;
127     AEPrintDescToHandle(desc,&xx);
128     NSLog(@"Handle: %s", *xx);
129     DisposeHandle(xx);
130 }
131
132 @end