PageRenderTime 68ms CodeModel.GetById 17ms app.highlight 47ms RepoModel.GetById 2ms app.codeStats 0ms

/core/externals/update-engine/externals/google-toolbox-for-mac/SpotlightPlugins/AppleScript/GetMetadataForFile.m

http://macfuse.googlecode.com/
Objective C | 121 lines | 96 code | 6 blank | 19 comment | 13 complexity | 6e2e22888dcd6755d078b22f4901d8b6 MD5 | raw file
  1//
  2//  GetMetadataForFile.m
  3//
  4//  Copyright 2008 Google Inc.
  5//
  6//  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  7//  use this file except in compliance with the License.  You may obtain a copy
  8//  of the License at
  9// 
 10//  http://www.apache.org/licenses/LICENSE-2.0
 11// 
 12//  Unless required by applicable law or agreed to in writing, software
 13//  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 14//  WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 15//  License for the specific language governing permissions and limitations under
 16//  the License.
 17//
 18
 19#import <AppKit/AppKit.h>
 20#import <Carbon/Carbon.h>
 21
 22static BOOL ImportScriptBundle(NSMutableDictionary *attributes, 
 23                               NSString *path) {
 24  NSBundle *scriptBundle = [NSBundle bundleWithPath:path];
 25  NSString *descriptionPath = [scriptBundle pathForResource:@"description" 
 26                                                     ofType:@"rtfd"];
 27  NSAttributedString *attrString = nil;
 28  if (descriptionPath) {
 29    attrString = [[[NSAttributedString alloc] initWithPath:descriptionPath
 30                                        documentAttributes:NULL] autorelease];
 31  }
 32  BOOL wasGood = NO;
 33  if (attrString) {
 34    NSString *description = [attrString string];
 35    [attributes setObject:description forKey:(NSString*)kMDItemDescription];
 36    wasGood = YES;
 37  }
 38  
 39  NSArray *scripts = [scriptBundle pathsForResourcesOfType:@"scpt" 
 40                                               inDirectory:@"Scripts"];
 41  NSEnumerator *scriptEnum = [scripts objectEnumerator];
 42  NSString *scriptPath;
 43  NSMutableArray *scriptSources = [NSMutableArray array]; 
 44  while ((scriptPath = [scriptEnum nextObject])) {
 45    NSURL *scriptURL = [NSURL fileURLWithPath:scriptPath];
 46    NSDictionary *error;
 47    NSAppleScript *script 
 48      = [[[NSAppleScript alloc] initWithContentsOfURL:scriptURL
 49                                                error:&error]
 50         autorelease];
 51    NSString *scriptSource = [script source];
 52    if (scriptSource) {
 53      [scriptSources addObject:scriptSource];
 54    }
 55  }
 56  if ([scriptSources count]) {
 57    NSString *source = [scriptSources componentsJoinedByString:@"\n"];
 58    [attributes setObject:source forKey:(NSString*)kMDItemTextContent];
 59    wasGood = YES;
 60  }
 61  return wasGood;
 62}
 63
 64static BOOL ImportScript(NSMutableDictionary *attributes, 
 65                         NSString *path) {
 66  NSURL *fileURL = [NSURL fileURLWithPath:path];
 67  FSRef ref;
 68  BOOL wasGood = NO;
 69  if (CFURLGetFSRef((CFURLRef)fileURL, &ref)) {
 70    ResFileRefNum resFile = FSOpenResFile(&ref, fsRdPerm);
 71    if (resFile) {
 72      const ResID kScriptDescriptionResID = 1128;
 73      ResFileRefNum curResFile = CurResFile();
 74      UseResFile(resFile);
 75      Handle res = Get1Resource('TEXT', kScriptDescriptionResID);
 76      if (res) {
 77        NSString *descString 
 78          = [[[NSString alloc]initWithBytes:(char*)(*res)
 79                                     length:GetHandleSize(res) 
 80                                   encoding:NSMacOSRomanStringEncoding] autorelease];
 81        ReleaseResource(res);
 82        if (descString) {
 83          [attributes setObject:descString forKey:(NSString*)kMDItemDescription];
 84          wasGood = YES;
 85        }
 86      }
 87      UseResFile(curResFile);
 88      CloseResFile(resFile);
 89    }
 90   
 91    NSDictionary *error;
 92    NSAppleScript *script = [[[NSAppleScript alloc] initWithContentsOfURL:fileURL
 93                                                                    error:&error]
 94                             autorelease];
 95    NSString *scriptSource = [script source];
 96    if (scriptSource) {
 97      [attributes setObject:scriptSource forKey:(NSString*)kMDItemTextContent];
 98      wasGood = YES;
 99    }
100  }
101  return wasGood;
102}
103
104// Currently grabs the script description and puts it into kMDItemDescription.
105// Grabs the script code and puts it into kMDItemTextContent.
106Boolean GetMetadataForFile(void* interface, 
107                           CFMutableDictionaryRef cfAttributes, 
108                           CFStringRef cfContentTypeUTI,
109                           CFStringRef cfPathToFile) {
110  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
111  NSMutableDictionary *attributes = (NSMutableDictionary*)cfAttributes;
112  NSString *pathToFile = (NSString*)cfPathToFile;
113  BOOL wasGood = NO;
114  if (UTTypeConformsTo(cfContentTypeUTI, CFSTR("com.apple.applescript.scriptbundle"))) {
115    wasGood = ImportScriptBundle(attributes, pathToFile);
116  } else if (UTTypeConformsTo(cfContentTypeUTI, CFSTR("com.apple.applescript.script"))) {
117    wasGood = ImportScript(attributes, pathToFile);
118  }
119  [pool release];
120  return wasGood ? TRUE : FALSE;
121}