Titus Stahl

Selecting attachments in Mutt via a GUI dialog

As part of my ongoing project to make Mutt more useable for me, I looked for a way to add attachments to Mutt with a GUI dialog. While usually doing things on the keyboard is faster (and this is the whole premise of using Mutt), for me, this is not true for selecting files. I find the “Recent files” section in the GNOME file dialogs especially helpful and can more quickly locate files this way than any other.

How to achieve this? First, you need the zenity package installed which lets you use GUI dialogs from the command line.

Then you have to add the following to your .muttrc:

set wait_key=no 
macro compose a "<shell-escape>bash /home/yourusername/.mutt/attach-file.sh<enter><enter-command>source /tmp/muttpick<enter>" "Attach with GUI"

This calls first the attach-file.sh script in your Mutt’s config directory and then executes the commands in /tmp/muttpick.

What does the script look like?

printf "%s" "push <attach-file>" > /tmp/muttpick
A="`zenity --file-selection`"
B="`basename "$A"`"
C=${B// /_}
cp "$A" "/tmp/$C"
printf "%s" "/tmp/$C"  >> /tmp/muttpick
printf "%s\n" "<enter>" >> /tmp/muttpick
echo "" >> /tmp/muttpick

This script selects a file with the Zenity dialog, it copies that file to the tmp directory and removes any spaces from the file name in that process (for whatever reason this is needed to work). Finally, it inserts the relevant Mutt commands to attach this file into /tmp/muttpick (which gets executed by Mutt after the scripts end per the macro above).