Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Using dictionary from command line

Is it possible I could see the definition of a word for eg. 'Apple' in terminal itself without opening the dictionary app?

I work in the terminal most of the times and I am just wondering if I could get all the information in terminal itself.

Any ideas? Thanks!

Message was edited by: kt.kthapar

iMac 27' Quadcore, Mac OS X (10.6.4)

Posted on Dec 11, 2010 6:04 AM

Reply
40 replies

Mar 24, 2014 7:29 AM in response to Tony T1

Tony T1 wrote:

So, if you don't have an internet connection, you wouldn't be reading this 🙂


If you only own a desktop workstation I can appreciate that you'd miss the point. However, by far the majority of units that Apple sell are portables. Unlike air, the internet is not everywhere, and that's an important consideration for notebook users (I'll forget that you totally overlooked the response time issue...).


Anyway, a better note is not to use any scripts that you don't understand


Why are scripts a special case from any other software? If people were required to understand how every utility they use on their computer actualy works, then they'd be able to use very little, and software developers wouldn't have much of a market.


There's no reason why every user has to understand how every script on their computer works. What matters is at best that the user understands what is needed to make it work and what its major limitations are. In this case, as I pointed out, that's the internet and response time, respectively.

Mar 24, 2014 8:19 AM in response to Phil Stokes

Phil Stokes wrote:

If you only own a desktop workstation I can appreciate that you'd miss the point.

There's no reason why every user has to understand how every script on their computer works.


The point was that before using a script posted in any forum, it's best understand what it does, Capisce? (that an Internet connection is needed for curl is sort of useless information if your not using a script from a trusted source)

Mar 24, 2014 8:55 AM in response to Tony T1

Great. All you need to do now is tell the OP how to do that in the command line (you might want to review the thread title there)...


Here, I got you started:


http://stackoverflow.com/questions/1617152/using-google-as-a-dictionary-lookup-v ia-bash-how-can-one-grab-the-first-definit


Still no word on comparative response times...maybe you have Google fiber and no data cap. 😁

Oct 21, 2015 10:53 PM in response to kt.kthapar

Here is the way I like the most. It uses Princeton University's Wordnet(https://wordnet.princeton.edu/) database:


1) If you don't have it already, get MacPorts(https://www.macports.org/)

2) afterwards, get the port wn and its dependencies - sudo port install wn

3) then, just type 'wn <word you want to look up> -over' in the terminal and then press enter

4) you will then have something like this in the terminal:

Overview of adj ambiguous


The adj ambiguous has 3 senses (first 3 from tagged texts)

1. (9) equivocal, ambiguous -- (open to two or more interpretations; or of uncertain nature or significance; or (often) intended to mislead; "an equivocal statement"; "the polling had a complex and equivocal (or ambiguous) message for potential female candidates"; "the officer's equivocal behavior increased the victim's uneasiness"; "popularity is an equivocal crown"; "an equivocal response to an embarrassing question")

2. (4) ambiguous -- (having more than one possible meaning; "ambiguous words"; "frustrated by ambiguous instructions, the parents were unable to assemble the toy")

3. (1) ambiguous -- (having no intrinsic or objective meaning; not organized in conventional patterns; "an ambiguous situation with no frame of reference"; "ambiguous inkblots")

Oct 23, 2015 12:38 PM in response to KcidKcus

Oh, did I forget to mention that wn can be used as a thesaurus? Well, it most certainly can! In the terminal, type 'wn <word you want to look up> -antsa'


*type '-ants(n)oun | (v)erb | (a)djective | (r)adverb' to look up antonyms of a word

*type '-syns(n)oun | (v)erb | (a)djective | (r)adverb' to look up synonyms of a word


For more information about what you can do with wn, type 'man wn' in the terminal. Have fun! 😎

Oct 31, 2015 11:53 AM in response to kt.kthapar

Copy and paste the following Python script as plain text into a new TextEdit document. Save it as dict.py to your Scripts folder located at /Users/<username>/Library/Scripts/. Adjust the textwrap column width (width=76), and the initial_indent and subsequent_indent spacings to your preference:

#!/usr/bin/env python2.7 import sys import DictionaryServices import textwrap def main(): try: search_term = sys.argv[1] except IndexError: print 'No search term was entered.' sys.exit() search_result = DictionaryServices.DCSCopyTextDefinition( None, search_term, (0, len(search_term))) if not search_result: print '"%s" was not found in Dictionary.' % (search_term) else: term_definition = search_result.replace( u'\u25B6', '').replace('ORIGIN', ' ORIGIN') single_spacing = ' '.join(term_definition.split()) wrapper = textwrap.TextWrapper(width=76, initial_indent=' ', subsequent_indent=' ') print wrapper.fill(single_spacing) if __name__ == '__main__': main()


Make the script executable with the following command in Terminal, substituting your username for <username>:

chmod u+x /Users/<username>/Library/Scripts/dict.py


If using the default bash shell in Terminal, place the following line in your ~/.bash_profile, substituting your username for <username>. If using a different shell, such a zsh, place it in that shell's profile. For zsh that would be ~/.zprofile:

alias dict='/Users/<username>/Library/Scripts/dict.py'


Then simply use the "dict" command with the search term you wish defined.

User uploaded file

If you prefer, use the following Python script to display the definition in a border. Adjust the textwrap column width (width=76) to your preference and the border will resize automatically:

#!/usr/bin/env python2.7 import sys import DictionaryServices import textwrap def main(): try: search_term = sys.argv[1] except IndexError: print 'No search term was entered.' sys.exit() search_result = DictionaryServices.DCSCopyTextDefinition( None, search_term, (0, len(search_term))) if not search_result: print '"%s" was not found in Dictionary.' % (search_term) else: term_definition = search_result.replace( u'\u25B6', '').replace('ORIGIN', ' ORIGIN') single_spacing = ' '.join(term_definition.split()) border_text = textwrap.fill(single_spacing, width=76).splitlines() max_length = max(len(s) for s in border_text) column_width = max_length + 2 print '+' + '-' * column_width + '+' for s in border_text: print '| %-*.*s |' % (max_length, max_length, s) print '+' + '-' * column_width + '+' if __name__ == '__main__': main()

User uploaded file

The elapsed time to return a definition is acceptable, generally less than a third of a second (on my 2008 MacBook Pro).

User uploaded file

Oct 31, 2015 12:33 PM in response to Roote

You should understand that even if you have syntax checked your Python code, and there are absolutely no PEP errors indicated, the Jive hosting software in the Support Community will jack your indentation around and copy/pasting it will result in syntax errors while running it in the Terminal after its visit to TextEdit. The latter is not a programmer's editor and it may actually introduce additional code indentation and encoding issues.


The same code, when copied and pasted from your post into SublimeText 3 using its Paste and Indent menu item, fixed all but two indentation errors caused by Jive. Since I use the Anaconda syntax checking package for Sublime Text 3, I can see all of the Python PEP syntax errors that are flagged in the code with English language reason, and watch them disappear as I fix them. After running your code through the Sublime Text 3 editor, it now works without any errors on El Capitan 10.11.1.


You do not need to explicitly refer to Python 2.7. Just use Python and the most recently installed version will be used. As of El Capitan, Apple does not provide Python 3. It would be useful to include those versions of OS X where you have tested your code.

Using dictionary from command line

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.