3 Star 2 Fork 1

咖啡碼農 / Alfred4OSC

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
alfred.py 3.33 KB
一键复制 编辑 原始数据 按行查看 历史
咖啡碼農 提交于 2014-10-16 15:27 . first commit
# -*- coding: utf-8 -*-
import itertools
import os
import plistlib
import unicodedata
import sys
import commands
import subprocess
from xml.etree.ElementTree import Element, SubElement, tostring
"""
You should run your script via /bin/bash with all escape options ticked.
The command line should be
python yourscript.py "{query}" arg2 arg3 ...
"""
UNESCAPE_CHARACTERS = u""" ;()"""
_MAX_RESULTS_DEFAULT = 9
preferences = plistlib.readPlist('info.plist')
bundleid = preferences['bundleid']
class Item(object):
@classmethod
def unicode(cls, value):
try:
items = value.iteritems()
except AttributeError:
return unicode(value)
else:
return dict(map(unicode, item) for item in items)
def __init__(self, attributes, title, subtitle, icon=None):
self.attributes = attributes
self.title = title
self.subtitle = subtitle
self.icon = icon
def __str__(self):
return tostring(self.xml(), encoding='utf-8')
def xml(self):
item = Element(u'item', self.unicode(self.attributes))
for attribute in (u'title', u'subtitle', u'icon'):
value = getattr(self, attribute)
if value is None:
continue
if len(value) == 2 and isinstance(value[1], dict):
(value, attributes) = value
else:
attributes = {}
SubElement(item, attribute, self.unicode(attributes)).text = unicode(value)
return item
def args(characters=None):
return tuple(unescape(decode(arg), characters) for arg in sys.argv[1:])
def config():
return _create('config')
def decode(s):
return unicodedata.normalize('NFD', s.decode('utf-8'))
def uid(uid):
return u'-'.join(map(unicode, (bundleid, uid)))
def unescape(query, characters=None):
for character in (UNESCAPE_CHARACTERS if (characters is None) else characters):
query = query.replace('\\%s' % character, character)
return query
def work(volatile):
path = {
True: '~/Library/Caches/com.runningwithcrayons.Alfred-2/Workflow Data',
False: '~/Library/Application Support/Alfred 2/Workflow Data'
}[bool(volatile)]
return _create(os.path.join(os.path.expanduser(path), bundleid))
def write(text):
sys.stdout.write(text)
def xml(items, maxresults=_MAX_RESULTS_DEFAULT):
root = Element('items')
for item in itertools.islice(items, maxresults):
root.append(item.xml())
return tostring(root, encoding='utf-8')
def _create(path):
if not os.path.isdir(path):
os.mkdir(path)
if not os.access(path, os.W_OK):
raise IOError('No write access: %s' % path)
return path
def create_pwd_window(lable, title):
return run_applescript("""tell application "Alfred 2"
activate
set alfredPath to (path to application "Alfred 2")
set alfredIcon to path to resource "appicon.icns" in bundle (alfredPath as alias)
display dialog "%s" with title "%s" buttons {"OK"} default button "OK" default answer "" with icon alfredIcon with hidden answer
set answer to text returned of result
end tell""" % (lable, title))
def run_applescript(scpt_str):
process = subprocess.Popen(['osascript', '-e', scpt_str],
stdout=subprocess.PIPE)
out = process.communicate()[0]
return out.strip()
Python
1
https://gitee.com/coder-leeyohn/Alfred4OSC.git
git@gitee.com:coder-leeyohn/Alfred4OSC.git
coder-leeyohn
Alfred4OSC
Alfred4OSC
master

搜索帮助