Spotlight importer and document package question
Spotlight importer and document package question
- Subject: Spotlight importer and document package question
- From: Sean Todd <email@hidden>
- Date: Mon, 29 Aug 2005 15:03:43 -0500
The documentation states that one way to get spotlight to work with
documents that are saved as packages is to use file wrappers.
However, I can't get this to work. I have even tried converting the
sample app and importer with no luck.
To convert the sample app I replaced the write and read methods of
MyDocument.m:
- (BOOL)writeToFile:(NSString *)fileName ofType:(NSString *)docType
- (BOOL)readFromFile:(NSString *)fileName ofType:(NSString *)docType
with:
- (NSFileWrapper*)fileWrapperOfType:(NSString*)typeName error:
(NSError**)outError {
NSData* theData = [NSKeyedArchiver archivedDataWithRootObject:
[self myData]];
NSFileWrapper* theFileWrapper = [[NSFileWrapper alloc]
initRegularFileWithContents:theData];
NSMutableDictionary* thePackage = [NSMutableDictionary dictionary];
[thePackage setValue:theFileWrapper forKey:@"document file"];
[theFileWrapper release];
return [[[NSFileWrapper alloc]
initDirectoryWithFileWrappers:thePackage] autorelease];
}
- (BOOL)readFromFileWrapper:(NSFileWrapper*)fileWrapper ofType:
(NSString*)typeName error:(NSError**)outError {
NSFileWrapper* theFileWrapper = nil;
NSDictionary* theFiles = nil;
if ([fileWrapper isDirectory]) {
theFiles = [fileWrapper fileWrappers];
theFileWrapper = [theFiles valueForKey:@"document file"];
if (!theFileWrapper || ![theFileWrapper isRegularFile]) {
if (outError != NULL) {
// error stuff ...
*outError = theError;
}
return NO;
}
}
NSMutableDictionary* theDict = [NSKeyedUnarchiver
unarchiveObjectWithData:[theFileWrapper regularFileContents]];
if (!theDict) { return NO; }
[self setMyData:theDict];
return YES;
}
I also made the necessary changes so that the documents would be
saved as packages. This all seems to work nicely.
I modified the sample importer's GetMetadataForFile(...) function in
main.m by replacing the line:
tempDict=[[NSDictionary alloc] initWithContentsOfFile:(NSString *)
pathToFile];
with:
NSFileWrapper* fileWrapper = [[NSFileWrapper alloc] initWithPath:
(NSString*)pathToFile];
if (fileWrapper) {
if ([fileWrapper isDirectory]) {
NSDictionary* theFiles = [fileWrapper fileWrappers];
NSFileWrapper* theFileWrapper = [theFiles
valueForKey:@"document file"];
if (!theFileWrapper || ![theFileWrapper isRegularFile]) {
success = NO;
}
else {
NSDictionary* theDict = [NSKeyedUnarchiver
unarchiveObjectWithData:[theFileWrapper regularFileContents]];
tempDict = [[NSDictionary alloc]
initWithDictionary:theDict copyItems:NO];
if (!tempDict) {
success = NO;
}
}
}
// release the loaded document
[fileWrapper release];
}
Testing this using the Terminal gives the following:
Sean-Todds-Computer:~ stodd$ /usr/bin/mdimport -d2 /Volumes/Litter\
Box/Users/stodd/Desktop/packagetest.myCustomDocument/
2005-08-26 13:40:32.714 mdimport[298] Import '/Volumes/Litter Box/
Users/stodd/Desktop/packagetest.myCustomDocument' type
'dyn.ah62d4qmuhk2x45p3qr41g7dtrzwg825zrzw067a' no mdimporter
2005-08-26 13:40:32.715 mdimport[298] Sending attributes of '/Volumes/
Litter Box/Users/stodd/Desktop/packagetest.myCustomDocument' to
server. Attributes: '{
"_kMDItemImporterCrashed" = <null>;
"com_apple_metadata_modtime" = 146774390;
kMDItemContentCreationDate = 2005-08-26 13:39:50 -0500;
kMDItemContentModificationDate = 2005-08-26 13:39:50 -0500;
kMDItemContentType =
"dyn.ah62d4qmuhk2x45p3qr41g7dtrzwg825zrzw067a";
kMDItemContentTypeTree = ("com.apple.package",
"public.directory", "public.item");
kMDItemDisplayName = {"" = packagetest; };
kMDItemKind = {"" = DocumentType; };
}'
also:
Sean-Todds-Computer:~ stodd$ /usr/bin/mdimport -L
2005-08-29 14:51:50.027 mdimport[1320] Paths: id(501) (
"/System/Library/Spotlight/Image.mdimporter",
"/System/Library/Spotlight/Audio.mdimporter",
"/System/Library/Spotlight/Font.mdimporter",
"/System/Library/Spotlight/PS.mdimporter",
"/Library/Spotlight/Microsoft Office.mdimporter",
"/System/Library/Spotlight/iPhoto.mdimporter",
"/System/Library/Spotlight/RichText.mdimporter",
"/System/Library/Spotlight/QuickTime.mdimporter",
"/System/Library/Spotlight/Mail.mdimporter",
"/Library/Spotlight/AppleWorks.mdimporter",
"/Library/Spotlight/Keynote.mdimporter",
"/Library/Spotlight/SourceCode.mdimporter",
"/System/Library/Spotlight/QuartzComposer.mdimporter",
"/System/Library/Spotlight/vCard.mdimporter",
"/System/Library/Spotlight/Application.mdimporter",
"/Library/Spotlight/Pages.mdimporter",
"/System/Library/Spotlight/PDF.mdimporter",
"/System/Library/Spotlight/Bookmarks.mdimporter",
"/System/Library/Spotlight/Chat.mdimporter",
"/Library/Spotlight/MyCustomImporter.mdimporter",
"/System/Library/Spotlight/SystemPrefs.mdimporter",
"/System/Library/Spotlight/iCal.mdimporter"
)
(notice importer 3rd from the bottom)
Am I missing something or is the documentation wrong with regards to
using NSFileWrappers with spotlight?
Thanks,
Sean
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden