Skip to content

Instantly share code, notes, and snippets.

Last active January 27, 2023 07:59
What would you like to do?
Apple dictionaries
# Thanks to commenters for providing the base of this much nicer implementation!
# Save and run with $ python
# You may need to hunt down the dictionary files yourself and change the awful path string below.
# This works for me on MacOS 10.14 Mohave
from struct import unpack
from zlib import decompress
import re
filename = '/System/Library/Assets/com_apple_MobileAsset_DictionaryServices_dictionaryOSX/9f5862030e8f00af171924ebbc23ebfd6e91af78.asset/AssetData/Oxford Dictionary of English.dictionary/Contents/Resources/'
f = open(filename, 'rb')
def gen_entry():
limit = 0x40 + unpack('i',[0]
while f.tell()<limit:
sz, = unpack('i',
buf = decompress([8:])
pos = 0
while pos < len(buf):
chunksize, = unpack('i', buf[pos:pos+4])
pos += 4
entry = buf[pos:pos+chunksize]
title ='d:title="(.*?)"', entry).group(1)
yield title, entry
pos += chunksize
for word, definition in gen_entry():
// *** Old code - not needed given the python code above
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "zlib.h"
#define CHUNK 16384
40 Length of the zlib stream
4c 0020
54 0275 number of blocks
60 808c pointer to the next block
64 8088 length of the first block
68 047a4a length of the unpacked block
6c start of the zlib stream
80fc second block
int unpack(unsigned char *in, int len)
int ret,outed=0;
unsigned have;
z_stream strm;
unsigned char out[CHUNK];
strm.zalloc = Z_NULL;
strm.zfree = Z_NULL;
strm.opaque = Z_NULL;
strm.avail_in = 0;
strm.next_in = Z_NULL;
ret = inflateInit(&strm);
if (ret != Z_OK)
return ret;
strm.avail_in = len;
strm.next_in = in;
do {
strm.avail_out = CHUNK;
strm.next_out = out;
ret = inflate(&strm, Z_NO_FLUSH);
assert(ret != Z_STREAM_ERROR); /* state not clobbered */
switch (ret) {
ret = Z_DATA_ERROR; /* and fall through */
return ret;
// printf("%lx %x\n",strm.next_in-in,strm.avail_in);
have = CHUNK - strm.avail_out /* - (outed?0:4)*/;
int off = 0;
while (have - off > 3 && out[off] != '<' && out[1+off] != 'd' && out[2+off] != ':') {
if (have - off <= 3) {
fprintf(stderr, "could not find entry\n");
if (fwrite(out + off/*+(outed?0:4)*/, have - off, 1, stdout) != 1 || ferror(stdout)) {
return Z_ERRNO;
} while (strm.avail_out == 0);
return ret == Z_STREAM_END ? Z_OK : Z_DATA_ERROR;
char filename[256];
int main(int argc,char **argv) {
FILE *fin; int limit,blen=0,p,l,bcnt=0; unsigned char *buf=NULL;
assert(argc >= 2);
if((fin=fopen(filename,"rb"))) {
do {
// if(0==l) break;
if(blen<l) {
if(buf!=NULL) free(buf);
buf=(unsigned char *)malloc(blen);
//fprintf(stderr, "%x@%06x: %x>%06x\n",bcnt,p,l,((int *)buf)[1]);
} while(p<limit);
return 0;
// This program strips the first 4 characters from each line in the input
#include <stdio.h>
int main() {
while(!ferror(stdin) && !feof(stdin)) {
size_t len = 0;
char *line = fgetln(stdin, &len);
if (!line) break;
if (len > 4)
fwrite(line + 4, 1, len - 4, stdout);
return 0;
Copy link

vinniec commented Aug 18, 2019

After a lot of test I can say that maybe the dictionary that interests me is damaged, not well formed or the dictionaries can have different forms.
However to your script on line 25 I would postpend .decode('utf-8') and I would remove line 23

Copy link

ilius commented Aug 19, 2019

Any ideas how can we get the number of entries at the beginning, without reading the whole file?

Copy link

vinniec commented Aug 22, 2019

Based on the structure of the file that I deduced from this script this information is not there, but I think that this information could be in the other files (one of these, EntryID.index,, KeyText.index)

Copy link

I tried, but failed. I don't know much about pragraming, but this is very important to me, could you please help me?
Many thanks!
Here is the screenshot:

Copy link

vinniec commented Oct 30, 2019

Please, try with pyglossary: ilius/pyglossary#109
A lot of time has passed since my tests and all the tests I did are on the old pc.
So you could do the tests for this new feature instead of me.

Copy link

@josephg That's interesting, but how to extract both word and definition ?

Copy link

I'm getting this error here, does anyone by any chance know how to fix it?

Deluges-MacBook-Air:app Deluge$ python
Traceback (most recent call last):
  File "", line 31, in <module>
    for word, definition in gen_entry():
  File "", line 18, in gen_entry
    buf = decompress([8:])
zlib.error: Error -3 while decompressing data: incorrect header check
Deluges-MacBook-Air:app Deluge$ 

Copy link

vinniec commented Mar 4, 2020

Not me, I have the idea that they have changed the compression format of the dictionary entries and therefore I can't help you. At the time I tried with a Hex Editor to understand the pattern of a term, but I don't know if this tip it can help you.

Copy link

vinniec commented Mar 4, 2020

or maybe you have the same my old problem, and I can help you. In my dict the header of every compressed dict is wrongso i decompress every term only attempting a lot of time.

Copy link

It's this file here I have been trying to uncompress. It might be compressed a bit different than the regular dictionaries and that might be the problem.

"/System/Library/Input Methods/"

Copy link

For those receiving "cannot use a string pattern on a bytes-like object" error, just str(entry).

Copy link

asnahu commented Mar 13, 2021

In python3, for unicode strings, should change the following lines:

title ='d:title="(.*?)"', entry).group(1) to title ='d:title="(.*?)"', entry).group(1)


print(word) to print(word.decode("UTF-8"))

it will generate correct string, if not, will get an error: cannot use a string pattern on a bytes-like object.

Copy link

So just to be clear, we can use this script to spit out all the entries in an Apple dictionary, which we choose ourselves what format we want to save in?

Copy link


I am trying to read the dictionary inside a simple react arrow function, but am a little lost with making decompression work correctly. I am stuck at the output <Buffer 00 00 00 00 ... >

If anybody has managed to make the extraction work with JS, please let me know. Thanks!


Copy link

josephg commented Sep 13, 2021

Way more up to date info here:

Copy link

soshial commented Dec 25, 2022

I would like to extract morphological forms from binary AppleDict. I struggled with these files and didn't succeed:


If anyone succeeds deciphering those, I would be very grateful.

Copy link

soshial commented Jan 26, 2023

I was able to extract morphological forms from binary AppleDict (contained in and exact article addresses from Thank you @josephg and @fab-jul for inspiring me to do that.

On this pyglossary issue page you may find and format description in detail with code.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment