Skip to content

Instantly share code, notes, and snippets.

@Ma233
Created January 20, 2016 06:43
Show Gist options
  • Save Ma233/27cc4c6aa99be4a9e580 to your computer and use it in GitHub Desktop.
Save Ma233/27cc4c6aa99be4a9e580 to your computer and use it in GitHub Desktop.
pip install pyobjc-framework-DictionaryServices && ln -s ./dict.py /usr/bin/dict
#!/usr/bin/python
import re
import sys
from DictionaryServices import DCSCopyTextDefinition
split_re = re.compile(r"\s+(?=[A-Z]\.\s)")
_split_re = re.compile(r"(?=" + u"[\u2460-\u2473]" + r")")
def search(word):
wordrange = (0, len(word))
search_result = DCSCopyTextDefinition(None, word, wordrange)
if not search_result:
errmsg = "'%s' not found in system dict." % (word)
print errmsg.encode('utf-8')
sys.exit(2)
else:
return search_result.encode('utf-8')
def format(search_result):
_fresult = split_re.sub('\n\n', search_result)
fresult = _split_re.sub('\n ', _fresult.decode('utf-8'))
return fresult
def main():
if not len(sys.argv) == 2:
print 'Usage: dict <word>'
sys.exit(1)
word = sys.argv[1].decode('utf-8')
search_result = search(word)
print '---------------'
print format(search_result)
print '---------------'
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment