Return to Snippet

Revision: 2615
at March 14, 2007 13:35 by tgunr


Initial Code
NSMetadataQuery
(null)/(null)/(null) (null) | Permalink
NSMetadataQuery seems to have bugs and unable to get NSMetadataQueryResultContentRelevanceAttribute value.

Use MDQueryRef instead.



#import

@interface MyDocument : NSDocument
{
IBOutlet id field;

MDQueryRef _query;


}
- (IBAction)search:(id)sender;
@end

---------

#import "MyDocument.h"


@implementation MyDocument

- (id)init
{
self = [super init];
if (self) {
}
return self;
}

- (NSString *)windowNibName
{
return @"MyDocument";
}

- (void)windowControllerDidLoadNib:(NSWindowController *) aController
{
[super windowControllerDidLoadNib:aController];

}


- (IBAction)search:(id)sender
{

if( [[field stringValue] isEqualToString:@"" ] ) return;


NSString *predicateFormat = @"(kMDItemTextContent == \"?%@*\"c) && (kMDItemContentType != 'com.apple.mail.emlx') && (kMDItemContentType != 'public.vcard')";

_query = MDQueryCreate (
NULL,
[NSString stringWithFormat:predicateFormat,[field stringValue]],
[NSArray arrayWithObjects:kMDQueryResultContentRelevance,
kMDItemPath, kMDItemDisplayName, kMDItemFSName,nil ],
[NSArray arrayWithObject:kMDQueryResultContentRelevance]
);


MDQuerySetSearchScope (
_query,
[NSArray arrayWithObject:kMDQueryScopeComputer ],
0
);


[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(finish) name:kMDQueryDidFinishNotification object:_query];



MDQueryExecute (
_query,
kMDQuerySynchronous
);



}


-(void)finish
{

[[NSNotificationCenter defaultCenter] removeObserver:self
name:nil
object:_query];

MDItemRef miref;

CFIndex idx;


idx = MDQueryGetResultCount(_query);


CFIndex hoge;
for( hoge = 0; hoge < idx; hoge++ )
{

miref = MDQueryGetResultAtIndex( _query, hoge);


NSString* name = (NSString*)MDItemCopyAttribute (
miref,
kMDItemFSName
);
NSString* path = (NSString*)MDItemCopyAttribute (
miref,
kMDItemPath
);
NSString* displayname = (NSString*)MDItemCopyAttribute (
miref,
kMDItemDisplayName
);
NSNumber* score = (NSNumber*)MDQueryGetAttributeValueOfResultAtIndex (
_query,
kMDQueryResultContentRelevance,
hoge
);

if( score != nil )
NSLog(@"score %f",[score floatValue]);


}


}

@end

Initial URL
http://homepage.mac.com/mnishikata/page2/page2.html

Initial Description

                                

Initial Title
NSMetadataQuery

Initial Tags
osx

Initial Language
Objective C