Using applications to view attachments from Mutt
Dec 02, '04 09:42:00AM
Contributed by: Anonymous
Mutt uses a .mailcap file to decide how to view attachments. I tried many different ways of specifying the handlers but nothing seemed to work. Even an example that worked with pine did not work. The first problem was that the temporary file would be deleted before the application spawned by open could read it. Another problem is that some file types could not be discerned from the file alone. Some files even have extensions missing. This solution solves all of those problems. It allows for any appropriate application to handle any type of file. The key is to have a program that does several things:
- Move the file to a slightly more permanent location.
- Discern the file type and extension if not given.
- Don't do anything with the type if told to ignore it.
- Allow for an application to be specified.
- Open the appropriate application for the file
Luckily the open command does a good job if given enough hints about what type of file it's dealing with. That usually means a good file extension. Do man open for more details. There was still a fair amount of file handling needed to accomplish all of these goals. I wrote this bash script and called it view_attachment and placed it in /usr/local/bin. Make sure it's got execute permissions with chmod +x view_attachment. The program is more comment than code -- but that's a good thing.
The next step is to edit your ~/.mailcap file so mutt can use the new program. The easiest entries are for images and pdfs, because their types are easily discernable. The mailcap entry for jpeg looks like this:
Image/JPEG;view_attachment %s
All this entry does is call the view_attachment program with the name of the temporary file that mutt wrote. view_attachment will discern the type and add or change the file extension so the Preview application will know what to do with it.
The next problem I had was with HTML files. Frequently, HTML mail does not have an extension. It is also not always discernable with the file command that does so well with images and lots of other file types. The mailcap file helps with this. It knows that the attachment is HTML. The solution is to tell view_attachment what the type is. That keeps it from guessing wrong. So the mailcap entry for HTML looks like this.
text/html; view_attachment %s html
The html after the %s tells view_attachment that the file type is html. view_attachment uses the type for the file extension. That ensures the proper application will handle it. Most likely that will be Safari.
The last problem was using a specific application for a specific file type. I use Open Office for a lot of different file types including Excel, Word, and PowerPoint files. Open Office is really good at figuring out what the file is, and doing the right thing. view_attachment doesn't need to do anything with the type. It could mess things up if it did. It still needs to move the file, though. The only other thing it should do is an open -a command with the application we asked for. That means two things.
- Type discerning needs to be turned off completely.
- There needs to be a third argument to specify the application
The file typing can be turned off by giving a - as the type. Here's what the majority of my Mailcap entries look like. The only difference is mime-type at the beginning of each line. Note this is shown on two lines, but it's one long line with a space instead of a line break:
Application/vnd.ms-excel; /usr/local/bin/view_attachment %s "-"
'/Applications/OpenOffice.org1.1.2/Start_OpenOffice.org.app'
I changed the name of Start OpenOffice.org.app to Start_OpenOffice.org.app in my Applications -> OpenOffice.org1.1.2 directory. The space could have stayed, but it has to be escaped with a \, and that was more pain than I wanted to deal with. Here's my entire .mailcap file as an example to work with.
Comments (8)
Mac OS X Hints
http://hints.macworld.com/article.php?story=20041201032409975