Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
make alfred support pinyin search
#! /usr/bin/python
# -*- coding: utf-8 -*-
# @author weishu @2015/12/7
import subprocess
import os
import re
import json
from AppKit import NSWorkspace, NSBundle
from pypinyin import lazy_pinyin
# copy from Alfred 2 preferences, if you have applications installed at other place, add it here.
APP_DIRECTORYS = [
u'/Applications',
u'/Applications/Xcode.app/Contents/Applications',
u'/Developer/Applications',
u'/Library/PreferencePannes',
u'/opt/homebrew-cask/Caskroom',
u'/System/Library/PreferencePannes',
u'/usr/local/Cellar',
u'~/Library/Caches/Metadata',
u'~/Library/Mobile Documents',
u'~/Library/PreferencePannes'
]
def _is_application(workspace, abspath):
''' is a `abspath` stand for a mac app'''
return workspace.isFilePackageAtPath_(abspath)
def _get_app_pinyin_name(app_name):
return reduce(lambda x, y: x + y, lazy_pinyin(app_name, errors='ignore'))
def _add_meta_data(app_pinyin_name, app_path):
''' add meta data(comments) to the app, which can help Alfred or SpotLight find it'''
print "processing %s" % app_path
try:
subprocess.check_call('xattr -w com.apple.metadata:kMDItemFinderComment %s "%s"' % (app_pinyin_name, app_path), shell=True)
except:
print "process %s failed, ignore." % app_path
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', 'zh-Hans', 'zh-CN')
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
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:
full_path = os.path.join(root, directory)
# print full_path
if not _is_application(workspace, full_path): continue
remove_list.append(directory)
try:
localized_name = _get_localized_name(full_path)
except:
print "get localized name for %s failed. ignore" % full_path
# continue
# print "localized_name:", localized_name if localized_name else None
app_name = localized_name if localized_name else directory.rsplit(r'.')[0]
if pattern.match(app_name):
# print "app_name: %s not match" % app_name
continue
try:
_add_meta_data(_get_app_pinyin_name(app_name), full_path)
except:
# may be empty, just ignore
print "add meta for %s failed" % 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]
if __name__ == '__main__':
main()
@EI3ven

This comment has been minimized.

Copy link

@EI3ven EI3ven commented Mar 26, 2016

add meta for /Applications/有道词典.app failed
add meta for /Applications/蒲公英.app failed

添加失败

@tiann

This comment has been minimized.

Copy link
Owner Author

@tiann tiann commented Apr 4, 2016

暂不支持 10.11系统

@ghost

This comment has been minimized.

Copy link

@ghost ghost commented Apr 17, 2016

什么时候能支持10.11呢, 好期待!!!

@leonlee

This comment has been minimized.

Copy link

@leonlee leonlee commented May 13, 2016

修改一下呗,10.11

@tiann

This comment has been minimized.

Copy link
Owner Author

@tiann tiann commented Sep 19, 2016

已经支持mac 10.11.

@doge1024

This comment has been minimized.

Copy link

@doge1024 doge1024 commented Oct 18, 2016

Traceback (most recent call last):
File "alfred-pinyin.py", line 12, in
from pypinyin import lazy_pinyin
ImportError: No module named pypinyin
怎么解决?电脑是10.12

@tiann

This comment has been minimized.

Copy link
Owner Author

@tiann tiann commented Nov 2, 2016

@harddog 需要安装 pypinyin 这个库;或者下载下来解压放在这个脚本的同级目录。

@lxlzhenshuai

This comment has been minimized.

Copy link

@lxlzhenshuai lxlzhenshuai commented Aug 30, 2018

Traceback (most recent call last):
File "/Users/lxl/Applications/alfred-pinyin.py", line 11, in
from AppKit import NSWorkspace, NSBundle
ModuleNotFoundError: No module named 'AppKit'

@shenguanjiejie

This comment has been minimized.

Copy link

@shenguanjiejie shenguanjiejie commented Feb 27, 2019

我安装后后悔了 我想卸载掉怎么办啊? ...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment