Skip navigation

Using dictionary from command line

4895 Views 21 Replies Latest reply: Mar 24, 2014 9:36 AM by Phil Stokes RSS
1 2 Previous Next
kt.kthapar Calculating status...
Currently Being Moderated
Dec 11, 2010 6:04 AM
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)
  • etresoft Level 7 Level 7 (23,895 points)
    Currently Being Moderated
    Dec 11, 2010 9:46 AM (in response to kt.kthapar)
    open dict://hello
    MacBook 2007 (white), Mac OS X (10.6.5), + iMac 27" + iPad + MacBook Pro
  • Linc Davis Level 10 Level 10 (107,660 points)
    Currently Being Moderated
    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.
    Mac OS X (10.6.5)
  • etresoft Level 7 Level 7 (23,895 points)
    Currently Being Moderated
    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
    MacBook 2007 (white), Mac OS X (10.6.5), + iMac 27" + iPad + MacBook Pro
  • matthewcornell Calculating status...
    Currently Being Moderated
    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!
    MacBook Pro, Mac OS X (10.5)
  • Seph Level 1 Level 1 (120 points)
    Currently Being Moderated
    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.
    MacBook Pro (Early 2008) 17" 2.5 GHz Core 2 Duo, Mac OS X (10.6.6), Mac Pros, Xserve, Xsan, iMac, Mac Mini
  • mklement0 Calculating status...
    Currently Being Moderated
    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.

    `

  • VikingOSX Level 5 Level 5 (4,695 points)
    Currently Being Moderated
    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.

  • Tony T1 Level 6 Level 6 (8,125 points)
    Currently Being Moderated
    Mar 23, 2014 5:54 PM (in response to VikingOSX)

    This is going in my .bash_profile

  • Phil Stokes Level 2 Level 2 (325 points)
    Currently Being Moderated
    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.

  • Phil Stokes Level 2 Level 2 (325 points)
    Currently Being Moderated
    Mar 23, 2014 7:46 PM (in response to Seph)

    Works in Vi as well.

     

    Great tip, thanks!

  • VikingOSX Level 5 Level 5 (4,695 points)
    Currently Being Moderated
    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.

  • Phil Stokes Level 2 Level 2 (325 points)
    Currently Being Moderated
    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.

  • Tony T1 Level 6 Level 6 (8,125 points)
    Currently Being Moderated
    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)

  • Cole Tierney Level 4 Level 4 (1,375 points)
    Currently Being Moderated
    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; }

1 2 Previous Next

Actions

More Like This

  • Retrieving data ...

Bookmarked By (1)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.