Skip to content

Instantly share code, notes, and snippets.

@masui
Created November 3, 2011 02:30
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save masui/1335617 to your computer and use it in GitHub Desktop.
Save masui/1335617 to your computer and use it in GitHub Desktop.
Find large Microsoft Word documents on Mac with Spotlight+MacRuby
#
# List large Microsoft Word files upto 10
#
framework 'Cocoa'
def finish(notification)
max = @query.resultCount
max = 10 if max > 10
(0...max).each { |i|
path = @query.resultAtIndex(i).valueForAttribute('kMDItemPath')
puts path
puts File.size(path)
}
exit
end
@query = NSMetadataQuery.alloc.init
@query.searchScopes = [ "#{ENV['HOME']}" ]
@query.sortDescriptors = [NSSortDescriptor.alloc.initWithKey('kMDItemFSSize',ascending:false)]
NSNotificationCenter.defaultCenter.addObserver(self,
selector:'finish:',
name:'NSMetadataQueryDidFinishGatheringNotification',
object:@query)
pred1 = NSPredicate.predicateWithFormat("kMDItemContentType == %@", "com.microsoft.word.doc")
pred2 = NSPredicate.predicateWithFormat("kMDItemContentType == %@", "org.openxmlformats.wordprocessingml.document")
preds = [pred1, pred2]
@query.predicate = NSCompoundPredicate.orPredicateWithSubpredicates(preds)
@query.startQuery
NSRunLoop.currentRunLoop.run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment