dotfiles

configuration files for shell, text editor, graphical environment, etc.
git clone git://src.adamsgaard.dk/dotfiles
Log | Files | Refs | README | LICENSE Back to index

view_attachment.sh (4135B)


      1 #!/bin/sh
      2 #
      3 # Author:  Eric Gebhart
      4 #
      5 # Purpose:  To be called by mutt as indicated by .mailcap to handle mail attachments.
      6 #
      7 # Function: Copy the given file to a temporary directory so mutt
      8 #           Won't delete it before it is read by the application.
      9 #
     10 #           Along the way, discern the file type or use the type
     11 #           That is given.
     12 #
     13 #           Finally use 'open' or 'open -a' if the third argument is
     14 #           given.
     15 #
     16 #
     17 # Arguments:
     18 #
     19 #     $1 is the file
     20 #     $2 is the type - for those times when file magic isn't enough.
     21 #                      I frequently get html mail that has no extension
     22 #                      and file can't figure out what it is.
     23 #
     24 #                      Set to '-' if you don't want the type to be discerned.
     25 #                      Many applications can sniff out the type on their own.
     26 #                      And they do a better job of it too.
     27 #
     28 #                      Open Office and MS Office for example.
     29 #
     30 #     $3 is open with.  as in open -a 'open with this .app' foo.xls
     31 #
     32 # Examples:  These are typical .mailcap entries which use this program.
     33 #
     34 #     Image/JPEG; /Users/vdanen/.mutt/view_attachment %s
     35 #     Image/PNG; /Users/vdanen/.mutt/view_attachment %s
     36 #     Image/GIF; /Users/vdanen/.mutt/view_attachment %s
     37 #
     38 #     Application/PDF; /Users/vdanen/.mutt/view_attachment %s
     39 #
     40 #         #This HTML example passes the type because file doesn't always work and
     41 #         #there aren't always extensions.
     42 #
     43 #     text/html; /Users/vdanen/.mutt/view_attachment %s html
     44 #
     45 #         # If your Start OpenOffice.org.app is spelled with a space like this one, <--
     46 #         # then you'll need to precede the space with a \ .  I found that too painful
     47 #         # and renamed it with an _.
     48 #
     49 #     Application/vnd.ms-excel; /Users/vdanen/.mutt/view_attachment %s "-" '/Applications/OpenOffice.org1.1.2/Start_OpenOffice.org.app'
     50 #     Application/msword; /Users/vdanen/.mutt/view_attachment %s "-" '/Applications/OpenOffice.org1.1.2/Start_OpenOffice.org.app'
     51 #
     52 #
     53 # Debugging:  If you have problems set debug to 'yes'.  That will cause a debug file
     54 #             be written to /tmp/mutt_attach/debug so you can see what is going on.
     55 #
     56 # See Also:  The man pages for open, file, basename
     57 #
     58 
     59 # the tmp directory to use.
     60 tmpdir="$HOME/tmp/mutt_attach"
     61 
     62 # the name of the debug file if debugging is turned on.
     63 debug_file=$tmpdir/debug
     64 
     65 # debug.  yes or no.
     66 debug="no"
     67 #debug="yes"
     68 
     69 type=$2
     70 open_with=$3
     71 
     72 # make sure the tmpdir exists.
     73 mkdir -p $tmpdir
     74 
     75 # clean it out.  Remove this if you want the directory
     76 # to accumulate attachment files.
     77 rm -f $tmpdir/*
     78 
     79 # Mutt puts everything in /tmp by default.
     80 # This gets the basic filename from the full pathname.
     81 filename=`basename $1`
     82 
     83 # get rid of the extenson and save the name for later.
     84 file=`echo $filename | cut -d"." -f1`
     85 
     86 if [ $debug = "yes" ]; then
     87     echo "1:" $1 " 2:" $2 " 3:" $3 > $debug_file
     88     echo "Filename:"$filename >> $debug_file
     89     echo "File:"$file >> $debug_file
     90     echo "===========================" >> $debug_file
     91 fi
     92 
     93 # if the type is empty then try to figure it out.
     94 if [ -z $type ]; then
     95     file  $1
     96     type=`file -bi $1 | cut -d"/" -f2 | sed 's/;.*$//'`
     97 fi
     98 
     99 # if the type is '-' then we don't want to mess with type.
    100 # Otherwise we are rebuilding the name.  Either from the
    101 # type that was passed in or from the type we discerned.
    102 if [ $type = "-" ]; then
    103     newfile=$filename
    104 else
    105     newfile=$file.$type
    106 fi
    107 
    108 newfile=$tmpdir/$newfile
    109 
    110 # Copy the file to our new spot so mutt can't delete it
    111 # before the app has a chance to view it.
    112 cp $1 $newfile
    113 
    114 if [ $debug = "yes" ]; then
    115     echo "File:" $file "TYPE:" $type >> $debug_file
    116     echo "Newfile:" $newfile >> $debug_file
    117     echo "Open With:" $open_with >> $debug_file
    118 fi
    119 
    120 # If there's no 'open with' then we can let preview do it's thing.
    121 # Otherwise we've been told what to use.  So do an open -a.
    122 if [ `uname | grep -i Darwin` ]; then
    123     opencmd=open
    124 else
    125     opencmd=xdg-open
    126 fi
    127 
    128 if [ -z $open_with ]; then
    129     echo $opencmd $newfile
    130     $opencmd $newfile
    131 else
    132     echo $opencmd -a "$open_with" $newfile
    133     $opencmd -a "$open_with" $newfile
    134 fi