Skip to content

Instantly share code, notes, and snippets.

@voidnologo
Created November 16, 2017 15:31
Show Gist options
  • Save voidnologo/e2d288a4b0e22656f3a759dc3ed7ea01 to your computer and use it in GitHub Desktop.
Save voidnologo/e2d288a4b0e22656f3a759dc3ed7ea01 to your computer and use it in GitHub Desktop.
Command line access to Dictionary in Mac OS X. Need to have pyobjc installed in environment.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import re
from DictionaryServices import DCSCopyTextDefinition
PURPLE = '\033[95m'
BLUE = '\033[94m'
GREEN = '\033[92m'
YELLOW = '\033[93m'
RED = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
def main():
try:
searchword = sys.argv[1]
except IndexError:
print('You did not enter any terms to look up in the Dictionary.')
sys.exit()
wordrange = (0, len(searchword))
result = DCSCopyTextDefinition(None, searchword, wordrange)
if not result:
print('{} not found in Dictionary.'.format(searchword))
else:
# result = re.sub(r'\|(.+?)\|', PURPLE + r'/\1/' + ENDC, result)
result = re.sub(r'(?<!\d)(\d)(?!\d)\s', '\n ' + BOLD + r'\1: ' + ENDC, result)
result = re.sub(r'▶', '\n\n ' + RED + '▶ ' + ENDC, result)
result = re.sub(r'• ', '\n ' + GREEN + '• ' + ENDC, result)
# result = re.sub(r'(‘|“)(.+?)(’|”)', YELLOW + r'“\2”' + ENDC, result)
result = re.sub(r'PHRASES', '\n\n' + YELLOW + 'PHRASES' + ENDC, result)
result = re.sub(r'DERIVATIVES', '\n\n' + YELLOW + 'DERIVATIVES' + ENDC, result)
result = re.sub(r'ORIGIN', '\n\n' + YELLOW + 'ORIGIN' + ENDC, result)
print(result)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment