Created
November 16, 2012 12:03
-
-
Save youpy/4086771 to your computer and use it in GitHub Desktop.
mdfind3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# -*- coding: utf-8 -*- | |
# -*- ruby -*- | |
require 'spotlight' | |
require 'optparse' | |
predicates = ['(true)'] | |
usedate = false | |
usesize = false | |
reverse = false | |
scopes = [] | |
saved_search_filename = nil | |
print_plist = false | |
def escape(str) | |
str.gsub(/"/, "\\\"") | |
end | |
# # デフォルトでは新しいものから順に表示 | |
# @query.setSortDescriptors([NSSortDescriptor.alloc.initWithKey('kMDItemContentModificationDate',ascending:false)]) | |
opt = OptionParser.new | |
opt.banner =<<BANNER | |
mdfind3 - better mdfind | |
original: https://github.com/masui/mdfind2 | |
usage: | |
% mdfind3 --contenttype com.omnigroup.omnigraffle.graffle --from 2011/11/1 | |
% mdfind3 --query '検索' --contenttype 'com.adobe.pdf' | |
options: | |
BANNER | |
opt.on('-f from', '--from from', 'e.g. --from 2010/1/1') { |from| | |
ymd = from.split(/\//) | |
predicates << "kMDItemFSContentChangeDate >= %i" % Time.local(*ymd) | |
usedate = true | |
} | |
opt.on('-t to', '--to to') { |to| | |
ymd = to.split(/\//) | |
predicates << "kMDItemFSContentChangeDate <= %i" % Time.local(*ymd) | |
usedate = true | |
} | |
opt.on('-q query', '--query query', 'e.g. --query "キーワード"'){ |query| | |
predicates << "kMDItemTextContent == \"%s\"cdw" % escape(query) | |
} | |
opt.on('-c contenttype', '--contenttype contenttype', 'e.g. --type com.omnigroup.omnigraffle.graffle'){ |contenttype| | |
predicates << "kMDItemContentType == \"%s\"cdw" % escape(contenttype) | |
} | |
opt.on('-k kind', '--kind kind', 'e.g. --kind "HTML Document"'){ |kind| | |
predicates << "kMDItemKind == \"*%s*\"cdw" % escape(kind) | |
} | |
opt.on('-b size', '--biggerthan size', 'e.g. --biggerthan 100000'){ |size| | |
predicates << "kMDItemFSSize >= %s" % size | |
usesize = true | |
} | |
opt.on('-s sizes', '--smallerthan size'){ |size| | |
predicates << "kMDItemFSSize <= %s" % size | |
usesize = true | |
} | |
opt.on('-d url', '--downloadfrom url'){ |url| | |
predicates << "kMDItemWhereFroms == \"%s\"cdw" % "*#{escape(url)}*" # 曖昧指定 | |
} | |
opt.on('-n name', '--name name'){ |name| | |
# predicates << NSPredicate.predicateWithFormat("kMDItemPath like[c] %@", "#{path}") kMDItemPathは検索に利用できないぽい | |
predicates << "kMDItemFSName == \"%s\"cdw" % escape(name) | |
} | |
opt.on('-a filename', '--savedsearch filename') { |filename| | |
saved_search_filename = filename | |
} | |
opt.on('-r', '--reverse', 'not implemented yet'){ | |
reverse = true | |
} | |
opt.on('-o dir', '--onlyin dir'){|dir| | |
scopes << dir | |
} | |
opt.on('-p', '--plist', 'print plist xml to stdout'){ | |
print_plist = true | |
} | |
if ARGV.length == 0 then # 条件ゼロでは検索しない | |
puts opt | |
exit | |
end | |
opt.parse!(ARGV) | |
# if usesize then | |
# @query.setSortDescriptors([NSSortDescriptor.alloc.initWithKey('kMDItemFSSize',ascending:(reverse ? true : false))]) | |
# end | |
# if usedate then | |
# @query.setSortDescriptors([NSSortDescriptor.alloc.initWithKey('kMDItemContentModificationDate',ascending:(reverse ? true : false))]) | |
# end | |
query_string = predicates.join(' && ') | |
if saved_search_filename | |
query_from_saved_search = Spotlight::Query.from_saved_search(saved_search_filename) | |
query_string = [query_from_saved_search.query_string, query_string].join(' && ') | |
scopes = scopes.empty? ? query_from_saved_search.scopes : scopes | |
end | |
# puts query_string | |
query = Spotlight::Query.new(query_string) | |
query.scopes = scopes | |
if print_plist | |
query.to_saved_search('/dev/stdout') | |
else | |
query.execute.each do |item| | |
puts item.get(:kMDItemPath) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is it supposed to be html escaping in the .plist ?