We Recommend

Programming in Objective-C Programming in Objective-C
Programming in Objective-C is a concise, carefully written tutorial on the basics of Objective-C and object-oriented programming. The book makes no assumption about prior experience with object-oriented programming languages or with the C language (upon which Objective-C is based). And because of this, both novice and experienced programmers alike can use this book to quickly and effectively learn the fundamentals of Objective-C.


Posted By

tgunr on 03/14/07


Tagged

osx objc


Versions (?)


NSMetadataQuery


Published in: Objective C 


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

  1. NSMetadataQuery
  2. (null)/(null)/(null) (null) | Permalink
  3. NSMetadataQuery seems to have bugs and unable to get NSMetadataQueryResultContentRelevanceAttribute value.
  4.  
  5. Use MDQueryRef instead.
  6.  
  7.  
  8.  
  9. #import
  10.  
  11. @interface MyDocument : NSDocument
  12. {
  13. IBOutlet id field;
  14.  
  15. MDQueryRef _query;
  16.  
  17.  
  18. }
  19. - (IBAction)search:(id)sender;
  20. @end
  21.  
  22. ---------
  23.  
  24. #import "MyDocument.h"
  25.  
  26.  
  27. @implementation MyDocument
  28.  
  29. - (id)init
  30. {
  31. self = [super init];
  32. if (self) {
  33. }
  34. return self;
  35. }
  36.  
  37. - (NSString *)windowNibName
  38. {
  39. return @"MyDocument";
  40. }
  41.  
  42. - (void)windowControllerDidLoadNib:(NSWindowController *) aController
  43. {
  44. [super windowControllerDidLoadNib:aController];
  45.  
  46. }
  47.  
  48.  
  49. - (IBAction)search:(id)sender
  50. {
  51.  
  52. if( [[field stringValue] isEqualToString:@"" ] ) return;
  53.  
  54.  
  55. NSString *predicateFormat = @"(kMDItemTextContent == \"?%@*\"c) && (kMDItemContentType != 'com.apple.mail.emlx') && (kMDItemContentType != 'public.vcard')";
  56.  
  57. _query = MDQueryCreate (
  58. NULL,
  59. [NSString stringWithFormat:predicateFormat,[field stringValue]],
  60. [NSArray arrayWithObjects:kMDQueryResultContentRelevance,
  61. kMDItemPath, kMDItemDisplayName, kMDItemFSName,nil ],
  62. [NSArray arrayWithObject:kMDQueryResultContentRelevance]
  63. );
  64.  
  65.  
  66. MDQuerySetSearchScope (
  67. _query,
  68. [NSArray arrayWithObject:kMDQueryScopeComputer ],
  69. 0
  70. );
  71.  
  72.  
  73. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(finish) name:kMDQueryDidFinishNotification object:_query];
  74.  
  75.  
  76.  
  77. MDQueryExecute (
  78. _query,
  79. kMDQuerySynchronous
  80. );
  81.  
  82.  
  83.  
  84. }
  85.  
  86.  
  87. -(void)finish
  88. {
  89.  
  90. [[NSNotificationCenter defaultCenter] removeObserver:self
  91. name:nil
  92. object:_query];
  93.  
  94. MDItemRef miref;
  95.  
  96. CFIndex idx;
  97.  
  98.  
  99. idx = MDQueryGetResultCount(_query);
  100.  
  101.  
  102. CFIndex hoge;
  103. for( hoge = 0; hoge < idx; hoge++ )
  104. {
  105.  
  106. miref = MDQueryGetResultAtIndex( _query, hoge);
  107.  
  108.  
  109. NSString* name = (NSString*)MDItemCopyAttribute (
  110. miref,
  111. kMDItemFSName
  112. );
  113. NSString* path = (NSString*)MDItemCopyAttribute (
  114. miref,
  115. kMDItemPath
  116. );
  117. NSString* displayname = (NSString*)MDItemCopyAttribute (
  118. miref,
  119. kMDItemDisplayName
  120. );
  121. NSNumber* score = (NSNumber*)MDQueryGetAttributeValueOfResultAtIndex (
  122. _query,
  123. kMDQueryResultContentRelevance,
  124. hoge
  125. );
  126.  
  127. if( score != nil )
  128. NSLog(@"score %f",[score floatValue]);
  129.  
  130.  
  131. }
  132.  
  133.  
  134. }
  135.  
  136. @end

Report this snippet 

You need to login to post a comment.