Last active
January 27, 2023 02:54
-
-
Save DQNEO/18fceb444097c447001d28eddf576226 to your computer and use it in GitHub Desktop.
Python script to lookup word definitions from MacOS's builtin dictionary app
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# | |
# LICSENCE: BSD | |
# | |
# Thanks to: https://www.lifewithpython.com/2016/07/python-use-mac-dictionary-app.html | |
# | |
import sys | |
from DictionaryServices import DCSGetTermRangeInString, DCSCopyTextDefinition | |
def lookup(word): | |
try: | |
word_range = DCSGetTermRangeInString(None, word, 0) | |
defn = DCSCopyTextDefinition(None, word, word_range) | |
return defn | |
except IndexError: | |
return 'NOT FOUND' | |
# Read each word from standard input, look up the definition, and print it | |
for word in sys.stdin: | |
w = word.strip() | |
if not w: | |
break | |
desc = lookup(w) | |
print([w,desc]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage
Prepare a words list as a text file in advance.
Pass the contents to my script.