aboutsummaryrefslogtreecommitdiff
path: root/Foundation/GTMNSAppleScript+Handler.m
blob: ec6f5d07eaa21b493a6882aaa1d9c77949ddf6b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
//
//  GTMNSAppleScript+Handler.m
//
//  Copyright 2008 Google Inc.
//
//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
//  use this file except in compliance with the License.  You may obtain a copy
//  of the License at
// 
//  http://www.apache.org/licenses/LICENSE-2.0
// 
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
//  License for the specific language governing permissions and limitations under
//  the License.
//

#import <Carbon/Carbon.h>
#import "GTMNSAppleScript+Handler.h"
#import "GTMNSAppleEventDescriptor+Foundation.h"
#import "GTMNSAppleEventDescriptor+Handler.h"
#import "GTMFourCharCode.h"
#import "GTMMethodCheck.h"

// Some private methods that we need to call
@interface NSAppleScript (NSPrivate)
+ (ComponentInstance)_defaultScriptingComponent;
- (OSAID) _compiledScriptID;
- (id)_initWithData:(NSData*)data error:(NSDictionary**)error;
- (id)_initWithScriptIDNoCopy:(OSAID)osaID;
+ (id)_infoForOSAError:(OSAError)error;
@end

@interface NSMethodSignature (NSPrivate)
+ (id)signatureWithObjCTypes:(const char *)fp8;
@end

// Our own private interfaces.
@interface NSAppleScript (GTMAppleScriptHandlerAdditionsPrivate)

// Return an descriptor for a property. Properties are only supposed to be
// of type NSString or GTMFourCharCode. GTMFourCharCode's need special handling
// as they must be turned into NSAppleEventDescriptors of typeProperty.
- (NSAppleEventDescriptor*)gtm_descriptorForPropertyValue:(id)property;

// Return an NSAppleEventDescriptor for a given property.
// |property| must be kind of class GTMFourCharCode
- (NSAppleEventDescriptor*)gtm_valueDescriptorForProperty:(id)property;

// Utility routine for extracting multiple values in scripts and their
// parents.
- (NSSet*)gtm_allValuesUsingSelector:(SEL)selector;

// Utility routine for extracting the handlers for a specific script without
// referring to parent scripts.
- (NSSet*)gtm_scriptHandlers;

// Utility routine for extracting the properties for a specific script without
// referring to parent scripts.
- (NSSet*)gtm_scriptProperties;

// Handles creating an NSAppleEventDescriptor from an OSAID
- (NSAppleEventDescriptor*)descForScriptID:(OSAID)scriptID 
                                 component:(ComponentInstance)component;

// Utility methods for converting between real and generic OSAIDs.
- (OSAID)gtm_genericID:(OSAID)osaID forComponent:(ComponentInstance)component;
- (OSAID)gtm_realIDAndComponent:(ComponentInstance*)component;
@end

@implementation NSAppleScript(GTMAppleScriptHandlerAdditions)
GTM_METHOD_CHECK(NSAppleEventDescriptor, gtm_descriptorWithPositionalHandler:parametersArray:);  // COV_NF_LINE
GTM_METHOD_CHECK(NSAppleEventDescriptor, gtm_descriptorWithLabeledHandler:labels:parameters:count:);  // COV_NF_LINE
GTM_METHOD_CHECK(NSAppleEventDescriptor, gtm_registerSelector:forTypes:count:);  // COV_NF_LINE

+ (void)load {
  DescType types[] = { 
    typeScript
  };
  
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  [NSAppleEventDescriptor gtm_registerSelector:@selector(gtm_scriptValue)
                                      forTypes:types
                                         count:sizeof(types)/sizeof(DescType)];
  
  DescType types2[] = { 
    'evnt'  // No type code for this one
  };
  
  [NSAppleEventDescriptor gtm_registerSelector:@selector(gtm_eventValue)
                                      forTypes:types2
                                         count:sizeof(types2)/sizeof(DescType)];
  
  DescType types3[] = {
    typeGTMOSAID
  };
  
  [NSAppleEventDescriptor gtm_registerSelector:@selector(gtm_osaIDValue)
                                      forTypes:types3
                                         count:sizeof(types3)/sizeof(DescType)];
  [pool release];
}

- (NSAppleEventDescriptor *)gtm_executeAppleEvent:(NSAppleEventDescriptor *)event 
                                            error:(NSDictionary **)error {
  if (![self isCompiled]) {
    if (![self compileAndReturnError:error]) {
      return nil;
    }
  }
  NSAppleEventDescriptor *desc = nil;
  ComponentInstance component;
  OSAID scriptID = [self gtm_realIDAndComponent:&component];
  OSAID valueID;
  OSAError err = OSAExecuteEvent(component, [event aeDesc], scriptID, 
                                 kOSAModeNull, &valueID);
  if (err == noErr) {
    // descForScriptID:component: is what sets this apart from the
    // standard executeAppelEvent:error: in that it handles
    // taking script results and turning them into AEDescs of typeGTMOSAID 
    // instead of typeScript.
    desc = [self descForScriptID:valueID component:component];
  }
  if (err && error) {
    *error = [NSAppleScript _infoForOSAError:err];
  }
  return desc;
}

- (NSAppleEventDescriptor*)gtm_executePositionalHandler:(NSString*)handler 
                                             parameters:(NSArray*)params 
                                                  error:(NSDictionary**)error {
  NSAppleEventDescriptor *event 
    = [NSAppleEventDescriptor gtm_descriptorWithPositionalHandler:handler 
                                                  parametersArray:params];
  return [self gtm_executeAppleEvent:event error:error];
} 

- (NSAppleEventDescriptor*)gtm_executeLabeledHandler:(NSString*)handler
                                              labels:(AEKeyword*)labels
                                          parameters:(id*)params
                                               count:(NSUInteger)count
                                               error:(NSDictionary **)error {
  NSAppleEventDescriptor *event 
    = [NSAppleEventDescriptor gtm_descriptorWithLabeledHandler:handler 
                                                        labels:labels
                                                    parameters:params
                                                         count:count];
  return [self gtm_executeAppleEvent:event error:error];
} 

- (NSSet*)gtm_handlers {
  return [self gtm_allValuesUsingSelector:@selector(gtm_scriptHandlers)];
}
  
- (NSSet*)gtm_properties {
  return [self gtm_allValuesUsingSelector:@selector(gtm_scriptProperties)];
}

// Set a value for a property by type (eg pASTopLevelScript)
- (BOOL)gtm_setValue:(id)value 
     forPropertyEnum:(DescType)property
    addingDefinition:(BOOL)adding {
  GTMFourCharCode *fcc 
    = [GTMFourCharCode fourCharCodeWithFourCharCode:property];
  return [self gtm_setValue:value forProperty:fcc addingDefinition:adding];
}

- (BOOL)gtm_setValue:(id)value 
         forProperty:(id)property 
    addingDefinition:(BOOL)adding{
  OSAError error = paramErr;
  BOOL wasGood = NO;
  NSAppleEventDescriptor *propertyName 
    = [self gtm_descriptorForPropertyValue:property];
  NSAppleEventDescriptor *desc = [value gtm_appleEventDescriptor];
  if (propertyName && desc) {
    NSAppleScript *script = self;
    OSAID valueID = kOSANullScript;
    ComponentInstance component;
    OSAID scriptID = [script gtm_realIDAndComponent:&component];
    error = OSACoerceFromDesc(component,
                              [desc aeDesc], 
                              kOSAModeNull, 
                              &valueID);
    if (error == noErr) {
      error = OSASetProperty(component, 
                             adding ? kOSAModeNull : kOSAModeDontDefine, 
                             scriptID, 
                             [propertyName aeDesc], 
                             valueID); 
      if (error == noErr) {
        wasGood = YES;
      }
    }
  }
  if (!wasGood) {
    _GTMDevLog(@"Unable to setValue:%@ forProperty:%@ from %@ (%d)", 
               value, property, self, error);
  }
  return wasGood;
}

- (id)gtm_valueForProperty:(id)property {
  return [[self gtm_valueDescriptorForProperty:property] gtm_objectValue];
}

- (id)gtm_valueForPropertyEnum:(DescType)property {
  GTMFourCharCode *fcc = [GTMFourCharCode fourCharCodeWithFourCharCode:property];
  return [self gtm_valueForProperty:fcc];
}

- (NSAppleEventDescriptor*)gtm_appleEventDescriptor {
  ComponentInstance component;
  OSAID osaID = [self gtm_realIDAndComponent:&component];
  AEDesc result = { typeNull, NULL };
  NSAppleEventDescriptor *desc = nil;
  OSAError error = OSACoerceToDesc(component, 
                                   osaID, 
                                   typeScript,
                                   kOSAModeNull,
                                   &result);
  if (error == noErr) {
    desc = [[[NSAppleEventDescriptor alloc] initWithAEDescNoCopy:&result] 
            autorelease];
  } else {
    _GTMDevLog(@"Unable to coerce script %d", error);  // COV_NF_LINE
  }
  return desc;
}

- (NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector {
  NSMethodSignature *signature = [super methodSignatureForSelector:aSelector];
  if (!signature) {
    NSMutableString *types = [NSMutableString stringWithString:@"@@:"];
    NSString *selName = NSStringFromSelector(aSelector);
    NSArray *selArray = [selName componentsSeparatedByString:@":"];
    NSUInteger count = [selArray count];
    for (NSUInteger i = 1; i < count; i++) {
      [types appendString:@"@"];
    }
    signature = [NSMethodSignature signatureWithObjCTypes:[types UTF8String]];    
  }
  return signature;
}

- (void)forwardInvocation:(NSInvocation *)invocation {
  SEL sel = [invocation selector];
  NSMutableString *handlerName = [NSStringFromSelector(sel) mutableCopy];
  NSUInteger handlerOrigLength = [handlerName length];
  [handlerName replaceOccurrencesOfString:@":" 
                               withString:@""
                                  options:0
                                    range:NSMakeRange(0,handlerOrigLength)];
  NSUInteger argCount = handlerOrigLength - [handlerName length];
  NSMutableArray *args = [NSMutableArray arrayWithCapacity:argCount];
  for (NSUInteger i = 0; i < argCount; ++i) {
    id arg;
    // +2 to ignore _sel and _cmd
    [invocation getArgument:&arg atIndex:i + 2];
    [args addObject:arg];
  }
  NSDictionary *error = nil;
  NSAppleEventDescriptor *desc = [self gtm_executePositionalHandler:handlerName 
                                                         parameters:args 
                                                              error:&error];  
  if ([[invocation methodSignature] methodReturnLength] > 0) {
    id returnValue = [desc gtm_objectValue];
    [invocation setReturnValue:&returnValue];
  }
}
@end

@implementation NSAppleScript (GTMAppleScriptHandlerAdditionsPrivate)

- (NSAppleEventDescriptor*)gtm_descriptorForPropertyValue:(id)property {
  NSAppleEventDescriptor *propDesc = nil;
  if ([property isKindOfClass:[GTMFourCharCode class]]) {
    propDesc = [property gtm_appleEventDescriptorOfType:typeProperty];
  } else if ([property isKindOfClass:[NSString class]]) {
    propDesc = [property gtm_appleEventDescriptor];
  }    
  return propDesc;
}

- (NSAppleEventDescriptor*)gtm_valueDescriptorForProperty:(id)property {
  OSAError error = paramErr;
  NSAppleEventDescriptor *desc = nil;
  NSAppleEventDescriptor *propertyName 
    = [self gtm_descriptorForPropertyValue:property];
  if (propertyName) {
    ComponentInstance component;
    OSAID scriptID = [self gtm_realIDAndComponent:&component];
    OSAID valueID = kOSANullScript;
    error = OSAGetProperty(component, 
                           kOSAModeNull, 
                           scriptID, 
                           [propertyName aeDesc], 
                           &valueID);
    if (error == noErr) {
      desc = [self descForScriptID:valueID component:component];
    }
  }
  if (error) {
    _GTMDevLog(@"Unable to get valueForProperty:%@ from %@ (%d)", 
               property, self, error);
  }
  return desc;
}

- (NSSet*)gtm_allValuesUsingSelector:(SEL)selector {
  NSMutableSet *resultSet = [NSMutableSet set];
  NSAppleEventDescriptor *scriptDesc = [self gtm_appleEventDescriptor];
  NSMutableSet *scriptDescsWeveSeen = [NSMutableSet set];
  GTMFourCharCode *fcc = [GTMFourCharCode fourCharCodeWithFourCharCode:pASParent];
  Class appleScriptClass = [NSAppleScript class];
  while (scriptDesc) {
    NSAppleScript *script = [scriptDesc gtm_objectValue];
    if ([script isKindOfClass:appleScriptClass]) {
      NSData *data = [scriptDesc data];
      if (!data || [scriptDescsWeveSeen containsObject:data]) {
        break;
      } else {
        [scriptDescsWeveSeen addObject:data];
      }
      NSSet *newSet = [script performSelector:selector];
      [resultSet unionSet:newSet];
      scriptDesc = [script gtm_valueDescriptorForProperty:fcc];
    } else {
      break;
    }
  }
  return resultSet;
}

- (NSSet*)gtm_scriptHandlers {
  AEDescList names = { typeNull, NULL };
  NSArray *array = nil;
  ComponentInstance component;
  OSAID osaID = [self gtm_realIDAndComponent:&component];
  OSAError error = OSAGetHandlerNames(component, kOSAModeNull, osaID, &names);
  if (error == noErr) {
    NSAppleEventDescriptor *desc 
      = [[[NSAppleEventDescriptor alloc] initWithAEDescNoCopy:&names] 
         autorelease];
    array = [desc gtm_objectValue];
  }
  if (error != noErr) {
    _GTMDevLog(@"Error getting handlers: %d", error); // COV_NF_LINE
  }
  return [NSSet setWithArray:array];
}

- (NSSet*)gtm_scriptProperties {
  AEDescList names = { typeNull, NULL };
  NSArray *array = nil;
  ComponentInstance component;
  OSAID osaID = [self gtm_realIDAndComponent:&component];
  OSAError error = OSAGetPropertyNames(component, kOSAModeNull, osaID, &names);
  if (error == noErr) {
    NSAppleEventDescriptor *desc 
      = [[[NSAppleEventDescriptor alloc] initWithAEDescNoCopy:&names] 
         autorelease];
    array = [desc gtm_objectValue];
  }
  if (error != noErr) {
    _GTMDevLog(@"Error getting properties: %d", error); // COV_NF_LINE
  }
  return [NSSet setWithArray:array];
}

- (OSAID)gtm_genericID:(OSAID)osaID forComponent:(ComponentInstance)component {
  ComponentInstance genericComponent = [NSAppleScript _defaultScriptingComponent];
  OSAID exactID = osaID;
  OSAError error = OSARealToGenericID(genericComponent, &exactID, component);
  if (error != noErr) {
    _GTMDevLog(@"Unable to get real id script: %@ %d", self, error); // COV_NF_LINE
    exactID = kOSANullScript; // COV_NF_LINE
  }
  return exactID;
}

- (NSAppleEventDescriptor*)descForScriptID:(OSAID)osaID 
                                 component:(ComponentInstance)component {
  NSAppleEventDescriptor *desc = nil;
  // If we have a script, return a typeGTMOSAID, otherwise convert it to
  // it's default AEDesc using OSACoerceToDesc with typeWildCard.
  long value = 0;
  OSAError err = noErr;
  if (osaID == 0) {
    desc = [NSAppleEventDescriptor nullDescriptor];
  } else {
    err = OSAGetScriptInfo(component,
                           osaID,
                           kOSAScriptBestType,
                           &value);
    if (err == noErr) {
      if (value == typeScript) {
        osaID = [self gtm_genericID:osaID forComponent:component];
        desc = [NSAppleEventDescriptor descriptorWithDescriptorType:typeGTMOSAID
                                                              bytes:&osaID 
                                                             length:sizeof(osaID)];
      } else {
        AEDesc aeDesc;
        err = OSACoerceToDesc(component, 
                                   osaID, 
                                   typeWildCard, 
                                   kOSAModeNull, 
                                   &aeDesc);
        if (err == noErr) {
          desc = [[[NSAppleEventDescriptor alloc] 
                   initWithAEDescNoCopy:&aeDesc] autorelease];
        }
      }
    }
  }
  if (err != noErr) {
    _GTMDevLog(@"Unable to create desc for id:%d (%d)", osaID, err); // COV_NF_LINE
  }
  return desc;
}

- (OSAID)gtm_realIDAndComponent:(ComponentInstance*)component {
  if (![self isCompiled]) {
    NSDictionary *error;
    if (![self compileAndReturnError:&error]) {
      _GTMDevLog(@"Unable to compile script: %@ %@", self, error);
      return kOSANullScript;
    }
  }
  OSAID genericID = [self _compiledScriptID];
  ComponentInstance genericComponent = [NSAppleScript _defaultScriptingComponent];
  OSAError error = OSAGenericToRealID(genericComponent, &genericID, component);
  if (error != noErr) {
    _GTMDevLog(@"Unable to get real id script: %@ %d", self, error); // COV_NF_LINE
    genericID = kOSANullScript; // COV_NF_LINE
  }
  return genericID;
}

@end

// Private methods for dealing with Scripts/Events and NSAppleEventDescriptors
@interface NSAppleEventDescriptor (GTMAppleEventDescriptorScriptAdditions)

// Return an NSAppleScript for a desc of typeScript. This will create a new
// Applescript that is a copy of the script that you want.
// Returns nil on failure.
- (NSAppleScript*)gtm_scriptValue;

// Return an NSAppleScript for a desc of typeGTMOSAID. This will not copy the
// script, but will create an NSAppleScript wrapping the script represented
// by the OSAID.
// Returns nil on failure.
- (NSAppleScript*)gtm_osaIDValue;

// Return a NSString with [eventClass][eventID] for typeEvent 'evnt'
- (NSString*)gtm_eventValue;
@end


@implementation NSAppleEventDescriptor (GMAppleEventDescriptorScriptAdditions)

- (NSAppleScript*)gtm_scriptValue {
  NSDictionary *error;
  NSAppleScript *script = [[[NSAppleScript alloc] _initWithData:[self data] 
                                                          error:&error] autorelease];
  if (!script) {
    _GTMDevLog(@"Unable to create script: %@", error);  // COV_NF_LINE
  }
  return script;
}

- (NSAppleScript*)gtm_osaIDValue {
  _GTMDevAssert([[self data] length] == sizeof(OSAID), nil);
  OSAID osaID = *(const OSAID*)[[self data] bytes];
  return [[[NSAppleScript alloc] _initWithScriptIDNoCopy:osaID] autorelease];
}

- (NSString*)gtm_eventValue {
  struct AEEventRecordStruct {
    AEEventClass eventClass;
    AEEventID eventID;
  };
  NSData *data = [self data];
  const struct AEEventRecordStruct *record 
    = (const struct AEEventRecordStruct*)[data bytes];
  NSString *eClass = [GTMFourCharCode stringWithFourCharCode:record->eventClass];
  NSString *eID = [GTMFourCharCode stringWithFourCharCode:record->eventID];
  return [eClass stringByAppendingString:eID];
}
@end