Skip to content

Instantly share code, notes, and snippets.

@kuku
Forked from lambdamusic/define.py
Created August 31, 2018 01:09
Show Gist options
  • Select an option

  • Save kuku/d65bdfd779ee54b0ccb01612a623f4c5 to your computer and use it in GitHub Desktop.

Select an option

Save kuku/d65bdfd779ee54b0ccb01612a623f4c5 to your computer and use it in GitHub Desktop.

Revisions

  1. @lambdamusic lambdamusic revised this gist Feb 4, 2016. 1 changed file with 11 additions and 3 deletions.
    14 changes: 11 additions & 3 deletions define.py
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,4 @@
    #!/usr/bin/python
    #!/usr/bin/env python

    """
    Modified from
    @@ -21,15 +21,23 @@


    import sys
    from DictionaryServices import *

    try:
    from DictionaryServices import *
    except:
    print "WARNING: The pyobjc Python library was not found. You can install it by typing: 'pip install -U pyobjc'"
    print "..................\n"


    try:
    from colorama import Fore, Back, Style
    except:
    print "WARNING: The colorama Python library was not found. You can install it by typing: 'pip install colorama'"
    print "..................\n"






    def main():
    """
  2. @lambdamusic lambdamusic revised this gist Nov 28, 2015. No changes.
  3. @lambdamusic lambdamusic revised this gist Nov 28, 2015. No changes.
  4. @lambdamusic lambdamusic revised this gist Nov 28, 2015. 1 changed file with 11 additions and 0 deletions.
    11 changes: 11 additions & 0 deletions define.py
    Original file line number Diff line number Diff line change
    @@ -5,6 +5,17 @@
    http://macscripter.net/viewtopic.php?id=26675
    http://apple.stackexchange.com/questions/90040/look-up-a-word-in-dictionary-app-in-terminal
    HowTo
    chmod 755 define.py # make file executable
    alias define="'/Users/me/Script/define.py'" # => add to .emacs_profile
    # then (from command line)
    define recursive # or any other word
    # .. definition follows ..
    """


  5. @lambdamusic lambdamusic revised this gist Nov 28, 2015. 2 changed files with 91 additions and 37 deletions.
    91 changes: 91 additions & 0 deletions define.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,91 @@
    #!/usr/bin/python

    """
    Modified from
    http://macscripter.net/viewtopic.php?id=26675
    http://apple.stackexchange.com/questions/90040/look-up-a-word-in-dictionary-app-in-terminal
    """



    import sys
    from DictionaryServices import *

    try:
    from colorama import Fore, Back, Style
    except:
    print "WARNING: The colorama Python library was not found. You can install it by typing: 'pip install colorama'"
    print "..................\n"



    def main():
    """
    define.py
    Access the default OSX dictionary
    2015-11-27
    -added colors via colorama
    """
    try:
    searchword = sys.argv[1].decode('utf-8')
    except IndexError:
    errmsg = 'You did not enter any terms to look up in the Dictionary.'
    print errmsg
    sys.exit()
    wordrange = (0, len(searchword))
    dictresult = DCSCopyTextDefinition(None, searchword, wordrange)
    if not dictresult:
    errmsg = "'%s' not found in Dictionary." % (searchword)
    print errmsg.encode('utf-8')
    else:
    s = dictresult.encode("utf-8")
    arrow = doColor("\n\n\xe2\x96\xb6 ", "red")
    s = s.replace('\xe2\x96\xb6', arrow) # arrow

    bullet = doColor("\n\xe2\x80\xa2 ", "red")
    s = s.replace('\xe2\x80\xa2', bullet) # bullet

    phrases_header = doColor("\n\nPHRASES\n", "important")
    s = s.replace('PHRASES', phrases_header)

    derivatives_header = doColor("\n\nDERIVATIVES\n", "important")
    s = s.replace('DERIVATIVES', derivatives_header)

    origin_header = doColor("\n\nORIGIN\n", "important")
    s = s.replace('ORIGIN', origin_header)

    print doColor("Dictionary entry for:\n", "red")
    print s
    print "\n---------------------------------"




    def doColor(s, style=None):
    """
    util for returning a colored string
    if colorama is not installed, FAIL SILENTLY
    """
    try:
    if style == "comment":
    s = Style.DIM + s + Style.RESET_ALL
    elif style == "important":
    s = Style.BRIGHT + s + Style.RESET_ALL
    elif style == "normal":
    s = Style.RESET_ALL + s + Style.RESET_ALL
    elif style == "red":
    s = Fore.RED + s + Style.RESET_ALL
    elif style == "green":
    s = Fore.GREEN + s + Style.RESET_ALL
    except:
    pass
    return s



    if __name__ == '__main__':
    main()
    37 changes: 0 additions & 37 deletions dict.py
    Original file line number Diff line number Diff line change
    @@ -1,37 +0,0 @@
    #!/usr/bin/python


    """
    Modified from
    http://macscripter.net/viewtopic.php?id=26675
    http://apple.stackexchange.com/questions/90040/look-up-a-word-in-dictionary-app-in-terminal
    """


    import sys
    from DictionaryServices import *

    def main():
    try:
    searchword = sys.argv[1].decode('utf-8')
    except IndexError:
    errmsg = 'You did not enter any terms to look up in the Dictionary.'
    print errmsg
    sys.exit()
    wordrange = (0, len(searchword))
    dictresult = DCSCopyTextDefinition(None, searchword, wordrange)
    if not dictresult:
    errmsg = "'%s' not found in Dictionary." % (searchword)
    print errmsg.encode('utf-8')
    else:
    s = dictresult.encode("utf-8")
    s = s.replace('\xe2\x96\xb6', "\n\n\xe2\x96\xb6 ") # arrow
    s = s.replace('\xe2\x80\xa2', "\n\xe2\x80\xa2 ") # bullet
    s = s.replace('PHRASES', "\n\nPHRASES\n")
    s = s.replace('DERIVATIVES', "\n\nDERIVATIVES\n")
    s = s.replace('ORIGIN', "\n\nORIGIN\n")
    print s

    if __name__ == '__main__':
    main()
  6. @lambdamusic lambdamusic revised this gist Nov 12, 2015. 1 changed file with 23 additions and 14 deletions.
    37 changes: 23 additions & 14 deletions dict.py
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,9 @@
    #!/usr/bin/python


    """
    Modified from
    http://macscripter.net/viewtopic.php?id=26675
    http://apple.stackexchange.com/questions/90040/look-up-a-word-in-dictionary-app-in-terminal
    """
    @@ -10,19 +13,25 @@
    from DictionaryServices import *

    def main():
    try:
    searchword = sys.argv[1].decode('utf-8')
    except IndexError:
    errmsg = 'You did not enter any terms to look up in the Dictionary.'
    print errmsg
    sys.exit()
    wordrange = (0, len(searchword))
    dictresult = DCSCopyTextDefinition(None, searchword, wordrange)
    if not dictresult:
    errmsg = "'%s' not found in Dictionary." % (searchword)
    print errmsg.encode('utf-8')
    else:
    print dictresult.encode('utf-8')
    try:
    searchword = sys.argv[1].decode('utf-8')
    except IndexError:
    errmsg = 'You did not enter any terms to look up in the Dictionary.'
    print errmsg
    sys.exit()
    wordrange = (0, len(searchword))
    dictresult = DCSCopyTextDefinition(None, searchword, wordrange)
    if not dictresult:
    errmsg = "'%s' not found in Dictionary." % (searchword)
    print errmsg.encode('utf-8')
    else:
    s = dictresult.encode("utf-8")
    s = s.replace('\xe2\x96\xb6', "\n\n\xe2\x96\xb6 ") # arrow
    s = s.replace('\xe2\x80\xa2', "\n\xe2\x80\xa2 ") # bullet
    s = s.replace('PHRASES', "\n\nPHRASES\n")
    s = s.replace('DERIVATIVES', "\n\nDERIVATIVES\n")
    s = s.replace('ORIGIN', "\n\nORIGIN\n")
    print s

    if __name__ == '__main__':
    main()
    main()
  7. @lambdamusic lambdamusic created this gist Nov 12, 2015.
    28 changes: 28 additions & 0 deletions dict.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    #!/usr/bin/python

    """
    http://macscripter.net/viewtopic.php?id=26675
    http://apple.stackexchange.com/questions/90040/look-up-a-word-in-dictionary-app-in-terminal
    """


    import sys
    from DictionaryServices import *

    def main():
    try:
    searchword = sys.argv[1].decode('utf-8')
    except IndexError:
    errmsg = 'You did not enter any terms to look up in the Dictionary.'
    print errmsg
    sys.exit()
    wordrange = (0, len(searchword))
    dictresult = DCSCopyTextDefinition(None, searchword, wordrange)
    if not dictresult:
    errmsg = "'%s' not found in Dictionary." % (searchword)
    print errmsg.encode('utf-8')
    else:
    print dictresult.encode('utf-8')

    if __name__ == '__main__':
    main()