感謝@小心大熊貓和@007752277662友情提醒,目前Alfred是支持拼音搜索的,操作手法如下:
按空格,出現(xiàn)符號(hào)
';然后輸入需要拼音搜索的程序或者文件即可。
此文已經(jīng)沒有任何意義,@Deprecated。
Alfred是個(gè)好東西,不過檢索程序的時(shí)候不支持拼音搜索;我在論壇看到有人給作者反饋過,無(wú)奈作者說(shuō)支持中文,他不知道拼音是什么,于是就不了了之了。舉個(gè)例子:我想打開網(wǎng)易云音樂,可是當(dāng)我輸入wangyiyunyinyue的時(shí)候卻是這樣的結(jié)果:

要么我知道這個(gè)App的名字叫做NeteaseMusic,要么我就需要用中文輸入網(wǎng)易云音樂打開了;如果恰巧輸入法是英文輸入狀態(tài),那么就會(huì)遇到上圖的情況;這時(shí)候再把已經(jīng)輸入的刪除然后切換輸入法打開,效率無(wú)疑大大折扣。
就算這里搜索這個(gè)App可以使用英文名字解決,可是對(duì)于某些系統(tǒng)程序比如郵件可能還知道是Mail,那么備忘錄呢?便簽?zāi)兀窟€有一些別的中文程序沒有英文名的比如馬克飛象?如果Alfred能支持拼音搜索,這些問題全部都沒了!而且,Alfred可以強(qiáng)制使用英文輸入,直接使用字母檢索,不用切換輸入法了。
原理
經(jīng)過簡(jiǎn)單的觀察之后,發(fā)現(xiàn)Alfred檢索程序不僅僅是檢索名字,還收集了一些額外的信息;在Alfred作者的幫助下,知道它利用了Mac文件系統(tǒng)的一個(gè)拓展信息的字段;如果你發(fā)現(xiàn)某些目錄后面有@那么就是有拓展信息了:
drwxr-xr-x+ 3 root wheel 102 9 10 2014 Stickies.app/
drwxr-xr-x@ 3 weishu admin 102 3 26 2015 Sublime Text.app/
可以借助命令行工具xattr進(jìn)行操作;具體使用可以man xattr.
所以,我們可以通過把拼音信息添加到文件的拓展信息里面去,這樣Alfred就能借助這些信息幫助拼音檢索了。
實(shí)現(xiàn)
獲取程序名
程序名不僅僅是一個(gè)文件名這么簡(jiǎn)單,Mac軟件有一個(gè)叫做localization的概念,大致就是國(guó)際化吧;程序結(jié)構(gòu)把國(guó)際化的字段存放在不同的文件里面,在程序本地化之后自動(dòng)load.
我們要使用的那個(gè)字段是CFBundleName;存放在/<App>/Contents/Resources/<language>/InfoPlist.strings這個(gè)文件里面;我們把這個(gè)名字讀取出來(lái)即可。
嘗試過使用objc的接口NSBundle.localizedInfoDiction來(lái)獲取本地化的字段,無(wú)奈拿到的永遠(yuǎn)是英文字段;只好手工解析中文字段了(不會(huì)Objc );使用的命令行工具plutil:
def _get_localized_name(abs_path):
'''get the localized name of given app'''
bundle = NSBundle.new()
bundle.initWithPath_(abs_path)
localizations = bundle.localizations()
chinese = ('zh_CN', 'zh_Hans')
b = any(map(lambda x: x in localizations, chinese))
if not b: return
for ch in chinese:
path = bundle.pathForResource_ofType_inDirectory_forLanguage_('InfoPlist', 'strings', None, ch)
if not path: continue
# the path must surround with "", there may be space characters
json_str = subprocess.check_output(u'plutil -convert json -o - "%s"' % path, shell=True)
# print json_str
json_res = json.loads(json_str, encoding='utf8')
name = json_res.get('CFBundleName')
if name: return name
轉(zhuǎn)換為拼音
可以直接使用python的拼音轉(zhuǎn)換庫(kù)[pypinyin][1],借助這個(gè)工具,一行代碼搞定:
def _get_app_pinyin_name(app_name):
reduce(lambda x, y: x + y, lazy_pinyin(app_name, errors='ignore'))
添加拼音信息
拼音信息被添加到文件的拓展信息里面,直接使用xattr添加即可:
def _add_meta_data(app_pinyin_name, app_path):
''' add meta data(comments) to the app, which can help Alfred or SpotLight find it'''
subprocess.check_call('xattr -w com.apple.metadata:kMDItemFinderComment %s %s' % (app_pinyin_name, app_path), shell=True)
好了,把這些代碼整合起來(lái),就能得到最終的結(jié)果了,完整的代碼在[這里][2]。
def main():
pattern = re.compile(r'^[\\w\\s.]+$')
workspace = NSWorkspace.sharedWorkspace()
for app_dir in APP_DIRECTORYS:
if not os.path.exists(app_dir): continue
for root, dirs, files in os.walk(app_dir, topdown=True):
remove_list = []
for directory in dirs:
# print type(directory), root, directory
full_path = os.path.join(root, directory)
if not _is_application(workspace, full_path): continue
remove_list.append(directory)
localized_name = _get_localized_name(full_path)
app_name = localized_name if localized_name else directory.rsplit(r'.')[0]
if pattern.match(app_name):
continue
_add_meta_data(_get_app_pinyin_name(app_name), full_path)
# if this directory is already a Application
# do not traverse this; some app may be very large
# and there won't be any other app inside it
dirs[:] = [d for d in dirs if d not in remove_list]
最后,我們執(zhí)行這一段腳本即可sudo python main.py。之所以需要sudo是因?yàn)槟承┫到y(tǒng)程序(比如家計(jì)算器),直接使用是沒有權(quán)限的。
完整代碼見原文:http://www.tianweishu.com/2015/12/07/make-alfred-support-pinyinyin-search
最后看效果:

[1]: https://github.com/mozillazg/python-pinyin
[2]: https://gist.github.com/tiann/35fb758c18036d7f8640