kt.kthapar

Q: 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

Close

Q: Using dictionary from command line

  • All replies
  • Helpful answers

Page 1 of 3 last Next
  • by etresoft,

    etresoft etresoft Dec 11, 2010 9:46 AM in response to kt.kthapar
    Level 7 (29,178 points)
    Mac OS X
    Dec 11, 2010 9:46 AM in response to kt.kthapar
    open dict://hello
  • by Linc Davis,

    Linc Davis Linc Davis Dec 12, 2010 2:00 PM in response to kt.kthapar
    Level 10 (207,963 points)
    Applications
    Dec 12, 2010 2:00 PM in response to kt.kthapar
    Not possible with the built-in dictionaries as far as I know, but 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.
  • by etresoft,

    etresoft etresoft Dec 12, 2010 2:56 PM in response to Linc Davis
    Level 7 (29,178 points)
    Mac OS X
    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
  • by matthewcornell,

    matthewcornell matthewcornell Feb 25, 2011 7:39 AM in response to kt.kthapar
    Level 1 (0 points)
    Feb 25, 2011 7:39 AM in response to kt.kthapar
    I can understand wanting to do this in Terminal, but really, as a power user, Spotlight for looking up words is very fast: Command-space to open spotlight, type the word, command-down or control-n to the dictionary icon, return. When done, command-tab to get back to Terminal. Easy!
  • by Seph,

    Seph Seph Feb 26, 2011 11:39 AM in response to kt.kthapar
    Level 1 (120 points)
    Feb 26, 2011 11:39 AM in response to kt.kthapar
    If you don't mind using the mouse, you can mouse over the word, then press 'cmd-ctrl-d' to get a drop-down of the definition.
  • by mklement0,

    mklement0 mklement0 Mar 22, 2014 8:39 PM in response to etresoft
    Level 1 (0 points)
    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: `

    (__bridge CFStringRef)` and `(__bridge NSString*)`.

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

    `

  • by VikingOSX,

    VikingOSX VikingOSX Mar 23, 2014 5:26 PM in response to kt.kthapar
    Level 7 (20,819 points)
    Mac OS X
    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.

  • by Tony T1,

    Tony T1 Tony T1 Mar 23, 2014 5:54 PM in response to VikingOSX
    Level 6 (9,249 points)
    Mac OS X
    Mar 23, 2014 5:54 PM in response to VikingOSX

    This is going in my .bash_profile

  • by Phil Stokes,

    Phil Stokes Phil Stokes Mar 23, 2014 7:26 PM in response to VikingOSX
    Level 2 (360 points)
    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.

  • by Phil Stokes,

    Phil Stokes Phil Stokes Mar 23, 2014 7:46 PM in response to Seph
    Level 2 (360 points)
    Mar 23, 2014 7:46 PM in response to Seph

    Works in Vi as well.

     

    Great tip, thanks!

  • by VikingOSX,

    VikingOSX VikingOSX Mar 23, 2014 8:16 PM in response to Phil Stokes
    Level 7 (20,819 points)
    Mac OS X
    Mar 23, 2014 8:16 PM in response to Phil Stokes

    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.

  • by Phil Stokes,

    Phil Stokes Phil Stokes Mar 24, 2014 3:44 AM in response to VikingOSX
    Level 2 (360 points)
    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.

  • by Tony T1,

    Tony T1 Tony T1 Mar 24, 2014 5:33 AM in response to Phil Stokes
    Level 6 (9,249 points)
    Mac OS X
    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)

  • by Cole Tierney,

    Cole Tierney Cole Tierney Mar 24, 2014 6:10 AM in response to kt.kthapar
    Level 4 (1,375 points)
    Mar 24, 2014 6:10 AM in response to kt.kthapar

    This may not be what you're looking for, but I find it handy sometimes: sp(){ egrep -hi "$*" /usr/share/dict/web* | less; }

Page 1 of 3 last Next