view_attachment.sh 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #!/bin/bash
  2. #
  3. # Original: Eric Gebhart, 2013.02.17
  4. # https://github.com/EricGebhart/view_attachment
  5. #
  6. # Modifications: Brandon Amos
  7. # https://github.com/bamos/dotfiles
  8. # [2014.04.03] Support Linux and OSX.
  9. #
  10. # Purpose: To be called by mutt as indicated by .mailcap to handle
  11. # mail attachments.
  12. tmpdir="/tmp/mutt_attach"
  13. type=$2
  14. open_with=$3
  15. mkdir -p $tmpdir
  16. # Mutt puts everything in /tmp by default.
  17. # This gets the basic filename from the full pathname.
  18. filename=`basename $1`
  19. # get rid of the extenson and save the name for later.
  20. file=`echo $filename | cut -d"." -f1`
  21. # if the type is empty then try to figure it out.
  22. if [ -z $type ]; then
  23. type=`file -bi $1 | cut -d"/" -f2`
  24. fi
  25. # if the type is '-' then we don't want to mess with type.
  26. # Otherwise we are rebuilding the name. Either from the
  27. # type that was passed in or from the type we discerned.
  28. if [ $type = "-" ]; then
  29. newfile=$filename
  30. else
  31. newfile=$file.$type
  32. fi
  33. newfile=$tmpdir/$newfile
  34. # Copy the file to our new spot so mutt can't delete it
  35. # before the app has a chance to view it.
  36. cp $1 $newfile
  37. case $(uname) in
  38. "Linux")
  39. if [ -z $open_with ]; then
  40. xdg-open $newfile
  41. else
  42. # TODO - handle this case
  43. $open_width $newfile
  44. fi
  45. ;;
  46. "Darwin")
  47. if [ -z $open_with ]; then
  48. open $newfile
  49. else
  50. open -a "$open_with" $newfile
  51. fi
  52. ;;
  53. esac