/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. #import <AppKit/AppKit.h>
  19. #import <Carbon/Carbon.h>
  20. static BOOL ImportScriptBundle(NSMutableDictionary *attributes,
  21. NSString *path) {
  22. NSBundle *scriptBundle = [NSBundle bundleWithPath:path];
  23. NSString *descriptionPath = [scriptBundle pathForResource:@"description"
  24. ofType:@"rtfd"];
  25. NSAttributedString *attrString = nil;
  26. if (descriptionPath) {
  27. attrString = [[[NSAttributedString alloc] initWithPath:descriptionPath
  28. documentAttributes:NULL] autorelease];
  29. }
  30. BOOL wasGood = NO;
  31. if (attrString) {
  32. NSString *description = [attrString string];
  33. [attributes setObject:description forKey:(NSString*)kMDItemDescription];
  34. wasGood = YES;
  35. }
  36. NSArray *scripts = [scriptBundle pathsForResourcesOfType:@"scpt"
  37. inDirectory:@"Scripts"];
  38. NSEnumerator *scriptEnum = [scripts objectEnumerator];
  39. NSString *scriptPath;
  40. NSMutableArray *scriptSources = [NSMutableArray array];
  41. while ((scriptPath = [scriptEnum nextObject])) {
  42. NSURL *scriptURL = [NSURL fileURLWithPath:scriptPath];
  43. NSDictionary *error;
  44. NSAppleScript *script
  45. = [[[NSAppleScript alloc] initWithContentsOfURL:scriptURL
  46. error:&error]
  47. autorelease];
  48. NSString *scriptSource = [script source];
  49. if (scriptSource) {
  50. [scriptSources addObject:scriptSource];
  51. }
  52. }
  53. if ([scriptSources count]) {
  54. NSString *source = [scriptSources componentsJoinedByString:@"\n"];
  55. [attributes setObject:source forKey:(NSString*)kMDItemTextContent];
  56. wasGood = YES;
  57. }
  58. return wasGood;
  59. }
  60. static BOOL ImportScript(NSMutableDictionary *attributes,
  61. NSString *path) {
  62. NSURL *fileURL = [NSURL fileURLWithPath:path];
  63. FSRef ref;
  64. BOOL wasGood = NO;
  65. if (CFURLGetFSRef((CFURLRef)fileURL, &ref)) {
  66. ResFileRefNum resFile = FSOpenResFile(&ref, fsRdPerm);
  67. if (resFile) {
  68. const ResID kScriptDescriptionResID = 1128;
  69. ResFileRefNum curResFile = CurResFile();
  70. UseResFile(resFile);
  71. Handle res = Get1Resource('TEXT', kScriptDescriptionResID);
  72. if (res) {
  73. NSString *descString
  74. = [[[NSString alloc]initWithBytes:(char*)(*res)
  75. length:GetHandleSize(res)
  76. encoding:NSMacOSRomanStringEncoding] autorelease];
  77. ReleaseResource(res);
  78. if (descString) {
  79. [attributes setObject:descString forKey:(NSString*)kMDItemDescription];
  80. wasGood = YES;
  81. }
  82. }
  83. UseResFile(curResFile);
  84. CloseResFile(resFile);
  85. }
  86. NSDictionary *error;
  87. NSAppleScript *script = [[[NSAppleScript alloc] initWithContentsOfURL:fileURL
  88. error:&error]
  89. autorelease];
  90. NSString *scriptSource = [script source];
  91. if (scriptSource) {
  92. [attributes setObject:scriptSource forKey:(NSString*)kMDItemTextContent];
  93. wasGood = YES;
  94. }
  95. }
  96. return wasGood;
  97. }
  98. // Currently grabs the script description and puts it into kMDItemDescription.
  99. // Grabs the script code and puts it into kMDItemTextContent.
  100. Boolean GetMetadataForFile(void* interface,
  101. CFMutableDictionaryRef cfAttributes,
  102. CFStringRef cfContentTypeUTI,
  103. CFStringRef cfPathToFile) {
  104. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  105. NSMutableDictionary *attributes = (NSMutableDictionary*)cfAttributes;
  106. NSString *pathToFile = (NSString*)cfPathToFile;
  107. BOOL wasGood = NO;
  108. if (UTTypeConformsTo(cfContentTypeUTI, CFSTR("com.apple.applescript.scriptbundle"))) {
  109. wasGood = ImportScriptBundle(attributes, pathToFile);
  110. } else if (UTTypeConformsTo(cfContentTypeUTI, CFSTR("com.apple.applescript.script"))) {
  111. wasGood = ImportScript(attributes, pathToFile);
  112. }
  113. [pool release];
  114. return wasGood ? TRUE : FALSE;
  115. }