Mutt and GNOME MIME types

A little while ago I wanted to launch an OGG file attached to an email. Since I read email in Mutt and it does such an excellent job of delegating unknown MIME types to external applications I simply pressed v (to see the attachments), selected the OGG attachment and pressed enter. Realplayer launched! Imagine my surprise.

After purging Realplayer from my system I had an idea. Mutt uses mailcap to determine how to deal with MIME types it can’t display, but since I’m using GNOME and it has a different system to deal with MIME types I frequently am surprised when viewing attachments in Mutt. Isn’t there some way of combining the two? Well, I frequently use gnome-open to launch viewers from the command line. But it forks off a new process for the viewer so it can’t be used in mailcapi. I spent some time looking into the inner workings of gnome-open and it might be possible to hack it to add a command line option to make it wait for the viewer to exit. However my quick test using Python suggests that it’d require changes in the underlying librariesii.

From what I learned looking through the implementation of gnome-open I hacked together the following short Python script:

#! /usr/bin/env python

import gnomevfs
import os
import sys

mime_type = gnomevfs.get_mime_type(sys.argv[1])
mime_app = gnomevfs.mime_get_default_application(mime_type)
application = os.popen('whereis %s' % mime_app[2]).readline().split(' ')[1]

os.execl(application, application, sys.argv[1])

Then I added a line to the top of ~/.mailcap:

application/*; ${HOME}/bin/gnopen '%s'; test=test -n "$DISPLAY"

Works like a charm, but of course suggestions/improvement are welcome!

  1. It seems Mutt removes the temporary file as soon as the viewer returns, so if gnome-open forks and exits Mutt will delete the file before the actual viewer gets to load it. Bugger.[back]
  2. I traced it down to a call to g_spawn_async that should be changed to g_spawn_sync, but that’s deep down in the underbelly of GNOME-VFS.[back]

3 Comments

  1. brinal says:

    Thanks a ton. This is nice ! I simply added some mime-types i didn’t want handled that way before that line and added image/*-types etc and off i go.

    one thing i need to figure out now, is how to open multiple attachements when there tagged as in 2 pdfs at once…

    thanks! brinal

  2. andykirk says:

    I hate the way mutt waits for you to finish viewing an attachment. A third option would be for your python script copy the temp file it is given, then spawn the real viewer on the copy and exit.

    Thanks for the code BTW.

  3. It may be a stupid answer, but check if you have xdg-open installed. It uses the same mailcap files, but it does not fork.

Leave a Reply

Please use markdown to make your comment beautiful.