title: Looking for a terminal command to parse MacOSX dictionary data file
body: <p><strong>Problem</strong></p>
  
  <p>MacOSX comes with dictionaries stored in <code>/Library/Dictionaries</code>. I would like to parse them to obtain dictionary results programmatically (via Terminal, AppleScript, or Automator). The dictionaries are MacOSX packages and all have a <code>Contents</code> folder that contains a file called <code>Body.data</code>. I would like to parse that file for a UTF-8 string (maybe Chinese character double bytes) and return the lines where the string is found.</p>
  
  <p>I've tried the following, which is not returning any results:</p>
  
  <pre><code>find . -name 'Body.data' -exec grep -li '我' {} \;
  </code></pre>
  
  <p>When I search through the dictionary using the app interface I can find the appropriate text. My objective is to create a workflow service to translate selected Chinese text into the pinyin equivalents which are stored in the system/user dictionaries.</p>
  
  <p><strong>Update</strong></p>
  
  <p>The following worked for me based on the accepted answer:</p>
  
  <p>Created and Archived a command line utility called <code>rdef</code> using Xcode with this code:</p>
  
  <pre><code>#import &lt;Foundation/Foundation.h&gt;
  
  int main(int argc, const char * argv[])
  {
  
      @autoreleasepool {
  
          if(argc &lt; 2)
          {
              printf("Usage: rdef &lt;word to define&gt;");
  
              return -1;
          }
  
          NSString * search =
          [NSString stringWithCString: argv[1] encoding: NSUTF8StringEncoding];
  
          CFStringRef def =
          DCSCopyTextDefinition(NULL,
                                (__bridge CFStringRef)search,
                                CFRangeMake(0, [search length]));
  
          NSString * output =
          [NSString stringWithFormat: @"Definition of &lt;%@&gt;: %@", search, (__bridge NSString *)def];
  
          printf("%s", [output UTF8String]);
  
  
      }
      return 0;
  }
  </code></pre>
  
  <p>Added the following to my project frameworks:</p>
  
  <p><img src="https://i.stack.imgur.com/3yZcT.png" alt="frameworks-added"> </p>
  
  <p>Performed a Build and then deployed manually using the steps below.</p>
  
  <p><strong>To deploy:</strong></p>
  
  <p>Right-clicked the Archived package and chose Show in Finder. Then Show Package Contents and drilled down product folder and copied the executable to <code>/local/usr/bin</code>. Now from a command prompt I can run the utility like so:</p>
  
  <pre><code>rdef 我|awk -F '\|' '{ gsub(/^ +| +$/, "", $2); print $2 }'
  </code></pre>
  
  <p>Please see the accepted answer below for extended references.</p>
  
  <p><strong>NB:</strong> 
  The github for the utility can be found at <a href="https://github.com/mingsai/rdef.git" rel="nofollow noreferrer">https://github.com/mingsai/rdef.git</a></p>
  
  <p><del>Next I will just create a Service to call the utility from Automator against selected text.</del></p>
  
  <p><strong>Service Solution</strong></p>
  
  <p>To pay it forward for the folks who've helped, especially @mklement0: here is the Solution for taking the command utility and converting it to a MacOSX service that can be used to translate Chinese characters to pinyin.</p>
  
  <p>Create a new Automator Service file and make sure to select output replaces selected text.</p>
  
  <p><img src="https://i.stack.imgur.com/E5VYl.png" alt="MacOSX Service - Convert Chinese to Pinyin"></p>
  
  <p><strong>Automator Script details</strong> </p>
  
  <pre><code>PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin/: 
  export PATH
  LC_CTYPE=UTF-8
  x=$1
  
  for ((i=0;i&lt;${#x};i++)); do rdef "${x:i:1}" | awk -F  '\|' 'BEGIN {ORS=" "}{ gsub(/^ | +?/, "", $2); if (length($2) &gt; 0) print $2 ; exit}'; done
  </code></pre>
  
  <p>To make the Service "live" just delete the "Ask for Text" and save the service with name of your choice (e.g. Convert to Pinyin).</p>
  
  <p>To use the revised service highlight any Chinese characters and right click the context menu then on the bottom under the Services menu select "Convert to Pinyin" ... (as indicated below)</p>
  
  <p><strong>Usage</strong></p>
  
  <p><img src="https://i.stack.imgur.com/j5Apl.png" alt="highlighted text"></p>
  
  <p><img src="https://i.stack.imgur.com/1YjBH.png" alt="Service-Selection"></p>
  
  <p>Produces this output</p>
  
  <p><img src="https://i.stack.imgur.com/x36Om.png" alt="output"></p>
  
  <p>Hope that helps anyone with this problem.</p>