Results 1 to 8 of 8

Thread: HOWTO: Add thumbnail support for chm files to gnome

  1. #1
    Join Date
    Mar 2008
    Beans
    227

    HOWTO: Add thumbnail support for chm files to gnome

    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.

  2. #2
    Join Date
    Dec 2009
    Beans
    1

    Smile Re: HOWTO: Add thumbnail support for chm files to gnome

    it works great
    u shine my ubuntu

  3. #3
    Join Date
    Apr 2006
    Location
    Alberta,Canada
    Beans
    1,135
    Distro
    Ubuntu Development Release

    Re: HOWTO: Add thumbnail support for chm files to gnome

    Great tip - works great

    Not sure if it was a glitch with my system but I had to restart nautilus before it kicked in.
    What color do Smurfs turn when you choke em?
    ____________________________

  4. #4
    Join Date
    Dec 2008
    Location
    Croatia
    Beans
    34
    Distro
    Ubuntu

    Re: HOWTO: Add thumbnail support for chm files to gnome

    Perfect.. Thanks!
    Donny you're out of your element!

  5. #5
    Join Date
    May 2010
    Beans
    8

    Re: HOWTO: Add thumbnail support for chm files to gnome

    Awesome script sir!as what they say works like a charm. Just applied it on my newly installed Ubuntu 10.04 and works in an instant. No tweaking needed to get it working right unlike other scrips out there that works depending on the type of machine that you have. I will install this on my cousin's machine and I hope I am right in saying works without the need to edit the script.

  6. #6
    Join Date
    Aug 2011
    Beans
    2

    Re: HOWTO: Add thumbnail support for chm files to gnome

    Doesn't work in 11.04. Is there any way to fix it?

  7. #7
    Join Date
    Jul 2006
    Location
    Golden, CO, USA
    Beans
    95
    Distro
    Ubuntu Gnome 13.10 Saucy Salamander

    Re: HOWTO: Add thumbnail support for chm files to gnome

    It does not work in gnome3 --- see my post here with some info http://ubuntuforums.org/showthread.p...3#post11459233

    maybe together we can find a solution...
    Have a nice day,
    Romano --- Linux user since 1989: first kernel 0.99pl8, Slackware distro (on 40 floppies! )

  8. #8
    Join Date
    Jul 2006
    Location
    Golden, CO, USA
    Beans
    95
    Distro
    Ubuntu Gnome 13.10 Saucy Salamander

    Re: HOWTO: Add thumbnail support for chm files to gnome

    I solved for my XFig thumbnailer: http://ubuntuforums.org/showthread.php?t=1881360

    I am available to give hints for this one. Just contact me.
    Last edited by r_mano; November 25th, 2011 at 04:23 PM.
    Have a nice day,
    Romano --- Linux user since 1989: first kernel 0.99pl8, Slackware distro (on 40 floppies! )

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •