Skip to content

Instantly share code, notes, and snippets.

@segphault
Created February 4, 2012 23:50
Show Gist options
  • Save segphault/1741191 to your computer and use it in GitHub Desktop.
Save segphault/1741191 to your computer and use it in GitHub Desktop.
Querying spotlight with MacRuby
def performSearch(pred, &cb)
search = NSMetadataQuery.alloc.init
search.predicate = pred
search.searchScopes = [File.expand_path("~")]
NSNotificationCenter.defaultCenter.addObserverForName(
"NSMetadataQueryDidFinishGatheringNotification", object:nil, queue:nil,
usingBlock: Proc.new {|n| n.object.results.each {|r| cb.call(r) } })
search.startQuery
end
pred = NSPredicate.predicateWithFormat("kMDItemDisplayName like[c] '*whatever*'")
performSearch(pred) {|r| puts r.valueForAttribute("kMDItemDisplayName") }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment