Using dictionary from command line
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)
iMac 27' Quadcore, Mac OS X (10.6.4)
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'
Linc Davis wrote:
you could install the POSIX 'dict' command (e.g. from MacPorts) which is a client for online dictionaries.
The suggestion given above launches Dictionary.app, which is apparently not what you want.
// insert code here...
NSLog(@"Hello, World!");
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]);
Thanks for the great ObjC code.
(__bridgeCFStringRef)` and `(__bridgeNSString*)`.
Also, adding the CoreServices framework (in addition to Foundation) appears not to be necessary.
`
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.
This is going in my .bash_profile 🙂
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.
Works in Vi as well.
Great tip, thanks!
Phil,
It was another approach to the post theme. Personally, I keep the Apple Dictionary in my Dock, and use it often. As always, there are multiple solution forms, and individual workflow determination comes into play.
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.
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)
This may not be what you're looking for, but I find it handy sometimes: sp(){ egrep -hi "$*" /usr/share/dict/web* | less; }
Using dictionary from command line