1# 2# Spotlight.rb 3# Spotlight 4# 5# Created by Norberto Ortigoza on 9/12/05. 6# Copyright (c) 2005 CrossHorizons. All rights reserved. 7# 8 9require 'osx/cocoa' 10include OSX 11 12class Spotlight < NSObject 13 14 attr :query, true 15 attr :predicate, true 16 17 def init 18 @query = NSMetadataQuery.alloc.init 19 return self 20 end 21 22 def search 23 begin 24 predicateToRun = NSPredicate.predicateWithFormat(@predicate) 25 rescue OCException 26 p "FIX IT: I haven't find a way to check if the predicate's format is valid" 27 predicateToRun = NSPredicate.predicateWithFormat("kMDItemDisplayName == ''") 28 end 29 @query.setPredicate(predicateToRun) 30 @query.startQuery() 31 end 32 33 def stop 34 @query.stopQuery() 35 end 36 37 def register_for_notification(aDelegate, methodName) 38 @query.setDelegate(aDelegate) 39 NSNotificationCenter.defaultCenter.objc_send :addObserver, aDelegate, 40 :selector, methodName, 41 :name, :NSMetadataQueryDidFinishGatheringNotification, 42 :object, @query 43 end 44 45 def remove_delegate (aDelegate) 46 NSNotificationCenter.defaultCenter.objc_send :removeObserver, aDelegate, 47 :name, :NSMetadataQueryDidFinishGatheringNotification, 48 :object, nil 49 end 50 51 def dealloc 52 stop() 53 super_dealloc() 54 end 55 56end 57