Advertisement
Guest User

SpotLight Api

a guest
Nov 5th, 2014
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.                     ////////////AppController.h////////////
  2.  
  3. #import <Cocoa/Cocoa.h>
  4.  
  5. @interface AppController : NSObject <NSMetadataQueryDelegate> {
  6.     NSMetadataQuery *query;
  7.     NSString *searchKey;
  8.     BOOL searchContent;
  9. }
  10.  
  11. - (void)queryNote:(NSNotification *)note;
  12. - (void)setSearchText:(NSString *)text;
  13.  
  14. @property(retain) NSMetadataQuery *query;
  15. @property(copy) NSString *searchKey;
  16. @property BOOL searchContent;
  17.  
  18. @end
  19.  
  20.                     ////////////AppController.m////////////
  21.  
  22. #import "AppController.h"
  23.  
  24. @implementation AppController
  25.  
  26. + (void)initialize {
  27.     //Doing nothing for now
  28. }
  29.  
  30. - (id)init {
  31.     if (self = [super init]) {
  32.         self.searchKey = @"";
  33.        
  34.         self.query = [[[NSMetadataQuery alloc] init] autorelease];
  35.         NSNotificationCenter *nf = [NSNotificationCenter defaultCenter];
  36.         [nf addObserver:self selector:@selector(queryNote:) name:nil object:self.query];
  37.  
  38.         [self.query setSortDescriptors:[NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:(id)kMDItemFSName ascending:YES] autorelease]]];
  39.  
  40.         [self.query setGroupingAttributes:[NSArray arrayWithObjects:(id)kMDItemKind, (id)kMDItemFSSize, nil]];
  41.         [self.query setDelegate:self];
  42.     }
  43.     return self;
  44. }
  45.  
  46. - (void)dealloc {
  47.     [[NSNotificationCenter defaultCenter] removeObserver:self];
  48.     [query release];
  49.     [searchKey release];
  50.     [super dealloc];
  51. }
  52.  
  53. @synthesize query;
  54.  
  55. - (void)queryNote:(NSNotification *)note {
  56.     NSLog(@"%@",note.name);
  57. }
  58.  
  59. - (void)createSearchPredicate {
  60.    
  61.     NSPredicate *predicateToRun = nil;
  62.     if (self.searchContent) {
  63.            NSString *predicateFormat = @"kMDItemTextContent like[c] %@";
  64.         predicateToRun = [NSPredicate predicateWithFormat:predicateFormat, self.searchKey];
  65.     }
  66.    
  67.     NSUInteger options = (NSCaseInsensitivePredicateOption|NSDiacriticInsensitivePredicateOption);
  68.     NSPredicate *compPred = [NSComparisonPredicate
  69.                 predicateWithLeftExpression:[NSExpression expressionForKeyPath:@"*"]
  70.                             rightExpression:[NSExpression expressionForConstantValue:self.searchKey]
  71.                                    modifier:NSDirectPredicateModifier
  72.                                        type:NSLikePredicateOperatorType
  73.                                     options:options];
  74.    
  75.     if (self.searchContent) {
  76.         predicateToRun = [NSCompoundPredicate orPredicateWithSubpredicates:[NSArray arrayWithObjects:compPred, predicateToRun, nil]];
  77.     } else {
  78.    
  79.         predicateToRun = compPred;
  80.     }
  81.    
  82.     NSPredicate *emailExclusionPredicate = [NSPredicate predicateWithFormat:@"(kMDItemContentType != 'com.apple.mail.emlx') && (kMDItemContentType != 'public.vcard')"];
  83.     predicateToRun = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:predicateToRun, emailExclusionPredicate, nil]];
  84.    
  85.     [self.query setPredicate:predicateToRun];          
  86.     [self.query startQuery];
  87. }
  88.  
  89. - (void)setSearchContent:(BOOL)value {
  90.     if (searchContent != value) {
  91.         searchContent = value;
  92.         [self createSearchPredicate];
  93.     }
  94. }
  95.  
  96. - (BOOL)searchContent {
  97.     return searchContent;
  98. }
  99.  
  100. - (void)setSearchText:(NSString *)text
  101. {
  102.     self.searchKey = text;
  103. }
  104. - (void)setSearchKey:(NSString *) value {
  105.     if (searchKey != value) {
  106.         NSLog(value);
  107.         [searchKey release];
  108.         searchKey = [value copy];
  109.         [self createSearchPredicate];
  110.     }
  111. }
  112.  
  113. - (NSString *)searchKey {
  114.     return [[searchKey retain] autorelease];
  115. }
  116.  
  117. - (void)tableViewDoubleClick:(id)path {
  118.     [[NSWorkspace sharedWorkspace] openFile:path];
  119. }
  120.  
  121. @end
  122.  
  123.                     ////////////main.cpp////////////
  124. AppController* testObj = [[AppController alloc] init];
  125. [testObj setSearchText:@"*.txt"];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement