Posted By


tgunr on 03/14/07

Tagged


Statistics


Viewed 913 times
Favorited by 0 user(s)

NSMetadataQuery


/ Published in: Objective C
Save to your folder(s)



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

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.