Skip to content

Instantly share code, notes, and snippets.

@m1a0yu3
Forked from tiann/alfred-pinyin.py
Created March 23, 2016 01:38
Show Gist options
  • Select an option

  • Save m1a0yu3/b73150c3d5f8bc6eac1c to your computer and use it in GitHub Desktop.

Select an option

Save m1a0yu3/b73150c3d5f8bc6eac1c to your computer and use it in GitHub Desktop.

Revisions

  1. weishu revised this gist Jan 5, 2016. 1 changed file with 5 additions and 1 deletion.
    6 changes: 5 additions & 1 deletion alfred-pinyin.py
    Original file line number Diff line number Diff line change
    @@ -76,8 +76,12 @@ def main():
    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

    localized_name = _get_localized_name(full_path)
    # print "localized_name:", localized_name if localized_name else None
    app_name = localized_name if localized_name else directory.rsplit(r'.')[0]

  2. tianws revised this gist Jan 5, 2016. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions alfred-pinyin.py
    Original file line number Diff line number Diff line change
    @@ -37,8 +37,9 @@ def _add_meta_data(app_pinyin_name, app_path):
    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 Exception, e:
    print e
    except:
    print "process %s failed, ignore." % app_path

    def _get_localized_name(abs_path):
    '''get the localized name of given app'''
    bundle = NSBundle.new()
  3. tianws revised this gist Dec 18, 2015. 1 changed file with 5 additions and 2 deletions.
    7 changes: 5 additions & 2 deletions alfred-pinyin.py
    Original file line number Diff line number Diff line change
    @@ -83,8 +83,11 @@ def main():
    if pattern.match(app_name):
    # print "app_name: %s not match" % app_name
    continue

    _add_meta_data(_get_app_pinyin_name(app_name), full_path)
    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
  4. tianws revised this gist Dec 13, 2015. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions alfred-pinyin.py
    Original file line number Diff line number Diff line change
    @@ -8,8 +8,6 @@
    import re
    import json

    import ipdb

    from AppKit import NSWorkspace, NSBundle
    from pypinyin import lazy_pinyin

  5. tianws revised this gist Dec 8, 2015. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions alfred-pinyin.py
    Original file line number Diff line number Diff line change
    @@ -73,17 +73,17 @@ def main():
    remove_list = []
    for directory in dirs:
    full_path = os.path.join(root, directory)
    print full_path
    # print full_path
    if not _is_application(workspace, full_path): continue

    remove_list.append(directory)

    localized_name = _get_localized_name(full_path)
    print "localized_name:", localized_name if localized_name else None
    # 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
    # print "app_name: %s not match" % app_name
    continue

    _add_meta_data(_get_app_pinyin_name(app_name), full_path)
  6. tianws revised this gist Dec 8, 2015. 1 changed file with 7 additions and 5 deletions.
    12 changes: 7 additions & 5 deletions alfred-pinyin.py
    Original file line number Diff line number Diff line change
    @@ -32,7 +32,7 @@ def _is_application(workspace, abspath):
    return workspace.isFilePackageAtPath_(abspath)

    def _get_app_pinyin_name(app_name):
    reduce(lambda x, y: x + y, lazy_pinyin(app_name, errors='ignore'))
    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'''
    @@ -46,7 +46,7 @@ def _get_localized_name(abs_path):
    bundle = NSBundle.new()
    bundle.initWithPath_(abs_path)
    localizations = bundle.localizations()
    chinese = ('zh_CN', 'zh_Hans')
    chinese = ('zh_CN', 'zh_Hans', 'zh-Hans', 'zh-CN')

    b = any(map(lambda x: x in localizations, chinese))
    if not b: return
    @@ -72,16 +72,18 @@ def main():
    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)
    print full_path
    if not _is_application(workspace, full_path): continue

    remove_list.append(directory)

    localized_name = _get_localized_name(full_path)
    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):
    if pattern.match(app_name):
    print "app_name: %s not match" % app_name
    continue

    _add_meta_data(_get_app_pinyin_name(app_name), full_path)
    @@ -92,4 +94,4 @@ def main():
    dirs[:] = [d for d in dirs if d not in remove_list]

    if __name__ == '__main__':
    main()
    main()
  7. tianws revised this gist Dec 7, 2015. 1 changed file with 60 additions and 57 deletions.
    117 changes: 60 additions & 57 deletions alfred-pinyin.py
    Original file line number Diff line number Diff line change
    @@ -15,78 +15,81 @@

    # 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'
    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)
    ''' is a `abspath` stand for a mac app'''
    return workspace.isFilePackageAtPath_(abspath)

    def _get_app_pinyin_name(app_name):
    reduce(lambda x, y: x + y, lazy_pinyin(app_name, errors='ignore'))
    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'''
    subprocess.check_call('xattr -w com.apple.metadata:kMDItemFinderComment %s %s' % (app_pinyin_name, app_path), shell=True)

    ''' 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 Exception, e:
    print e
    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
    '''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

    def main():
    pattern = re.compile(r'^[\w\s.]+$')
    pattern = re.compile(r'^[\w\s.]+$')

    workspace = NSWorkspace.sharedWorkspace()
    workspace = NSWorkspace.sharedWorkspace()

    for app_dir in APP_DIRECTORYS:
    if not os.path.exists(app_dir): continue
    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
    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]
    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 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]
    # 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()
    main()
  8. tianws created this gist Dec 6, 2015.
    92 changes: 92 additions & 0 deletions alfred-pinyin.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,92 @@
    #! /usr/bin/env python
    # -*- coding: utf-8 -*-

    # @author weishu @2015/12/7

    import subprocess
    import os
    import re
    import json

    import ipdb

    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):
    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'''
    subprocess.check_call('xattr -w com.apple.metadata:kMDItemFinderComment %s %s' % (app_pinyin_name, app_path), shell=True)

    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

    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]

    if __name__ == '__main__':
    main()