look up a word in Dictionary.app in Terminal

Solution 1:

You can use...

open dict://my_word

...which will open the Dictionary application and lookup the string my_word. If you want to use multiple words use something like open dict://"Big Bang Theory".

There's no output in the Terminal though.

Solution 2:

Using the Python Objective-C bindings, you could create just a small python script to get it from the built in OS X Dictionary. Here's a post that details this script"

#!/usr/bin/python3

import sys
from CoreServices import DictionaryServices


def main():
    try:
        searchword = sys.argv[1]
    except IndexError:
        errmsg = 'You did not enter any terms to look up in the Dictionary.'
        print(errmsg)
        sys.exit()
    wordrange = (0, len(searchword))
    dictresult = DictionaryServices.DCSCopyTextDefinition(None, searchword, wordrange)
    if not dictresult:
        errmsg = "'%s' not found in Dictionary." % (searchword)
        print(errmsg)
    else:
        print(dictresult)


if __name__ == '__main__':
    main()

Save that to dict.py, and then just run python dict.py dictation

enter image description here

Check out the post for more instructions on making it accessable all across your terminal.

Solution 3:

I found a solution using Swift 4.

#!/usr/bin/swift
import Foundation

if (CommandLine.argc < 2) {
    print("Usage: dictionary word")
}else{
    let argument = CommandLine.arguments[1]
    let result = DCSCopyTextDefinition(nil, argument as CFString, CFRangeMake(0, argument.count))?.takeRetainedValue() as String?
    print(result ?? "")
}
  1. save this as dict.swift
  2. add permission by chmod +x dict.swift
  3. lookup dictionary
    • run with interpreter ./dict.swift word
    • build by compiler swiftc dict.swift and run ./dict word