forked from youknowone/VisualJSON
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetMetadataForFile.m
More file actions
69 lines (56 loc) · 2.98 KB
/
Copy pathGetMetadataForFile.m
File metadata and controls
69 lines (56 loc) · 2.98 KB
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
//
// GetMetadataForFile.m
// VisualJSONImporter
//
// Created by 2012 youknowone.org on 12. 6. 4..
// Copyright (c) 2012 youknowone.org. All rights reserved.
//
#include <CoreFoundation/CoreFoundation.h>
#import <CoreData/CoreData.h>
#import "MySpotlightImporter.h"
Boolean GetMetadataForFile(void *thisInterface, CFMutableDictionaryRef attributes, CFStringRef contentTypeUTI, CFStringRef pathToFile);
//==============================================================================
//
// Get metadata attributes from document files
//
// The purpose of this function is to extract useful information from the
// file formats for your document, and set the values into the attribute
// dictionary for Spotlight to include.
//
//==============================================================================
Boolean GetMetadataForFile(void *thisInterface, CFMutableDictionaryRef attributes, CFStringRef contentTypeUTI, CFStringRef pathToFile)
{
// Pull any available metadata from the file at the specified path
// Return the attribute keys and attribute values in the dict
// Return TRUE if successful, FALSE if there was no data provided
// The path could point to either a Core Data store file in which
// case we import the store's metadata, or it could point to a Core
// Data external record file for a specific record instances
Boolean ok = FALSE;
@autoreleasepool {
NSError *error = nil;
if ([(__bridge NSString *)contentTypeUTI isEqualToString:@"YOUR_STORE_FILE_UTI"]) {
// import from store file metadata
// Create the URL, then attempt to get the meta-data from the store
NSURL *url = [NSURL fileURLWithPath:(__bridge NSString *)pathToFile];
NSDictionary *metadata = [NSPersistentStoreCoordinator metadataForPersistentStoreOfType:nil URL:url error:&error];
// If there is no error, add the info
if (error == NULL) {
// Get the information you are interested in from the dictionary
// "YOUR_INFO" should be replaced by key(s) you are interested in
NSObject *contentToIndex = [metadata objectForKey:@"YOUR_INFO"];
if (contentToIndex != nil) {
// Add the metadata to the text content for indexing
[(__bridge NSMutableDictionary *)attributes setObject:contentToIndex forKey:(NSString *)kMDItemTextContent];
ok = TRUE;
}
}
} else if ([(__bridge NSString *)contentTypeUTI isEqualToString:@"YOUR_EXTERNAL_RECORD_UTI"]) {
// import from an external record file
MySpotlightImporter *importer = [[[MySpotlightImporter alloc] init] autorelease];
ok = [importer importFileAtPath:(__bridge NSString *)pathToFile attributes:(__bridge NSMutableDictionary *)attributes error:&error];
}
}
// Return the status
return ok;
}