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

May 22, 2017 8:45 PM in response to Roote

Tangentially, if you like to discover new words, you can look up words randomly in Dictionary on the command-line using dict.py, described in a previous post above, and command substitution. You'll first need to extract a word list from whichever dictionary you use. You can then use a language and function of your choice. I prefer shuf, non-native to OS X, but which you can download as part of GNU Coreutils. You can also use Ruby, Perl, or Python which are all included in OS X.


Shuf:


dict "$(shuf -n1 ~/WordLists/NOAD3rdED.txt)"


Ruby:


dict "$(ruby -e 'puts $<.readlines.shuffle' < ~/WordLists/NOAD3rdED.txt | sed -n 1,1p)"


Perl:


dict "$(perl -MList::Util=shuffle -e 'print shuffle <>' ~/WordLists/NOAD3rdED.txt | head -1)"


Python:


dict "$(python -c 'import random, sys; print random.choice(open(sys.argv[1]).readlines()),' ~/WordLists/NOAD3rdED.txt)"


If you create a shell script and name it something like dictrand.sh, and place an alias in your ~/.bash_profile as described in a previous post above for dict, you can just use the command name. A script example:


#!/bin/bash


/Users/pd/Library/Scripts/dict.py "$(shuf -n1 ~/WordLists/NOAD3rdED.txt)"


Line example in ~/.bash_profile:


alias dictrand='/Users/pd/Library/Scripts/dictrand.sh'

User uploaded file

Dec 12, 2010 2:56 PM in response to Linc Davis

Linc Davis wrote:
you could install the POSIX 'dict' command (e.g. from MacPorts) which is a client for online dictionaries.


Ugh! How crude!

The suggestion given above launches Dictionary.app, which is apparently not what you want.


Yes indeed. Sorry for not paying more attention.

Since you work in terminal most of the time, a little Xcode can't hurt.
1) Create a new Foundation Command Line Tool in Xcode.
2) Replace the lines:

// insert code here...
NSLog(@"Hello, World!");

with

if(argc < 2)
{
printf("Usage: dict <word to define> ");

return -1;
}

NSString * search =
[NSString stringWithCString: argv[1] encoding: NSUTF8StringEncoding];

CFStringRef def =
DCSCopyTextDefinition(NULL,
(CFStringRef)search,
CFRangeMake(0, [search length]));

NSString * output =
[NSString
stringWithFormat:
@"Definition of <%@>: %@", search, (NSString *)def];

printf("%s", [output UTF8String]);

3) Add the CoreServices framework to the project. Right/command click on "External Frameworks and Libraries", choose "Add..", choose "Existing Frameworks..", choose "CoreServices.framework", and click "Add".
4) Build
5) Enjoy

Mar 22, 2014 8:39 PM in response to etresoft

Thanks for the great ObjC code.


  • `CFRelease(def);` should be added at the end to release the string returned by `DCSCopyTextDefinition()`.
  • As of Xcode 5.1 on OS X 10.9.2 with ARC you also need `__bridge` cast qualifiers: `

(__bridgeCFStringRef)` and `(__bridgeNSString*)`.

Also, adding the CoreServices framework (in addition to Foundation) appears not to be necessary.

`

Mar 23, 2014 5:26 PM in response to kt.kthapar

One could put the following code in their ~/.bashrc file as a shell function.


dict () {

curl dict://dict.org/d:"${1}"

}


Then:


source .bashrc

dict Apple | more


Alternative using SpotLight


Use command+Space will open Spotlight. Put in the word you want to look up in the Apple Dictionary App. The results appear far down on the Spotlight results. Press command+L to jump to the dictionary entry and open the result in a Window, or command+D to open the Apple Dictionary App referencing the Spotlight word.

Mar 23, 2014 7:26 PM in response to VikingOSX

VikingOSX wrote:


One could put the following code in their ~/.bashrc file as a shell function.


dict () {

curl dict://dict.org/d:"${1}"

}


Then:


source .bashrc

dict Apple | more




For those considering this, note that


i. it requires an internet connection to work


ii. it'll be as fast (or slow) as your connection + the server respond time.


While it's an interesting way to do it, it seems wasteful to go online to query a dictionary when there's one instantly accessible sitting on your hard drive.

Mar 24, 2014 3:44 AM in response to VikingOSX

Sure, as I said I thought the solution was quite interesting from a technical point of view. Always good to know of different ways to skin a cat. I was just pointing out some practical drawbacks is all. 🙂


In fact my own workflow is your second suggestion. Accessing Spotlight from the keyboard is a very fast way to accomplish a lot of things in OS X, not just the dictionary.

Mar 24, 2014 5:33 AM in response to Phil Stokes

Phil Stokes wrote:


For those considering this, note that


i. it requires an internet connection to work



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

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

(i.e., if you don't know what curl is, then man curl before using)

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.