For anyone out there with chm files, I wrote a script that will add thumbnail support for chm files to gnome. With this script you'll get thumbnails of your chm files in the nautilus file manager. The script uses the largest image from the homepage of the chm file to generate the thumbnail, usually this will be an image of the front cover.
First you'll need to install the dependencies of this script:
Code:
sudo apt-get install python-beautifulsoup python-chm imagemagick
The script itself:
Code:
#!/usr/bin/env python
import sys, os
from chm import chm
from BeautifulSoup import BeautifulSoup
class ChmThumbNailer(object):
def __init__(self):
self.chm = chm.CHMFile()
def thumbnail(self, ifile, ofile, sz):
if self.chm.LoadCHM(ifile) == 0:
return 1
bestname = None
bestsize = 0
base = self.chm.home.rpartition('/')[0] + '/'
size, data = self.getfile(self.chm.home)
if size > 0:
if self.chm.home.endswith(('jpg','gif','bmp')):
self.write(ofile, sz, data)
else:
soup = BeautifulSoup(data)
imgs = soup.findAll('img')
for img in imgs:
name = base + img.get("src","")
size, data = self.getfile(name)
if size > bestsize:
bestsize = size
bestname = name
if bestname != None:
size, data = self.getfile(bestname)
if size > 0:
self.write(ofile, sz, data)
self.chm.CloseCHM()
def write(self, ofile, sz, data):
fd = os.popen('convert - -resize %sx%s "%s"' % (sz, sz, ofile), "w")
fd.write(data)
fd.close()
def getfile(self,name):
(ret, ui) = self.chm.ResolveObject(name)
if ret == 1:
return (0, '')
return self.chm.RetrieveObject(ui)
if len(sys.argv) > 3:
chm = ChmThumbNailer()
chm.thumbnail(sys.argv[1], sys.argv[2], sys.argv[3])
Name the script something like chm-thumbnailer.py, save it somewhere (e.g. in a bin directory in your home directory) and make it executable:
Code:
chmod a+x chm-thumbnailer.py
Next you'll have to let gnome know about this script:
(Note: the code below assumes you have saved the script as: /home/myname/bin/chm-thumbnailer.py you must replace it with the actual location of the script)
Code:
gconftool --type=string --set "/desktop/gnome/thumbnailers/application@x-chm/command" "/home/myname/bin/chm-thumbnailer.py %i %o %s"
gconftool --type=bool --set "/desktop/gnome/thumbnailers/application@x-chm/enable" "true"
That's all. Now just use nautilus to navigate to a folder with chm files in it and the thumbnails will be generated.
Bookmarks