Advertisement
Shnatsel

Thunderbird multi-attachment script

Feb 15th, 2014
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.09 KB | None | 0 0
  1. #!/bin/bash
  2.  
  3. # This is a wrapper script to attach several files to an email in Thunderbird.
  4. # Written by Sergey "Shnatsel" Davidoff <[email protected]>
  5. # License: 3-clause BSD
  6.  
  7. # Thunderbird is very picky about the file paths and will just hang in case it
  8. # doesn't like something.
  9. # If it does, I'm probably doing something slightly wrong here.
  10.  
  11. #TODO: define a Gettext domain
  12.  
  13. # Disable history substitution on "!" symbols so we can have them in strings
  14. set +H
  15.  
  16. if [ -z "$1" ] || [ "$1" = '-h' ] || [ "$1" = '--help' ]; then
  17.     echo $"Usage: $0 /path/to/file [/path/to/another/file...]
  18. Relative paths are also supported, but files with comma in the name are NOT."
  19.     exit 1 # so that calling without parameters is counted as a failure
  20. fi
  21.  
  22. while [ -n "$1" ]; do
  23.     # Check if the file exists; if not, complain and exclude the file.
  24.     # Thunderbird will hang if given a nonexistent file.
  25.     if [ -e "$1" ]; then
  26.         if ! ( echo "$1" | grep -q ',' ); then
  27.             FILES="$FILES,$1"
  28.         else
  29.             comma_files="$comma_files
  30. $1"
  31.         fi
  32.     else
  33.         nonexistent_files="$nonexistent_files
  34. $1"
  35.     fi
  36.     shift
  37. done
  38.  
  39. # Remove the commas in the beginning and end of file list,
  40. # otherwise Thunderbird will hang.
  41. FILES=${FILES%,}
  42. FILES=${FILES#,}
  43.  
  44. # Error reporting
  45. if [ -n "$comma_files" ]; then
  46.     ERRORS="$ERRORS
  47. "$"Thunderbird cannot handle files with commas in the name.
  48. These files were not attached: $comma_files"
  49. fi
  50. if [ -n "$nonexistent_files" ]; then
  51.     ERRORS="$ERRORS
  52. "$"These files do not seem to exist and were ignored: $nonexistent_files"
  53. fi
  54. ERRORS=${ERRORS#'
  55. '} # Remove the \n in the beginning of the error list, it's prettier that way
  56.  
  57. if [ -n "$FILES" ]; then
  58.     if [ -n "$ERRORS" ]; then
  59.         notify-send --icon=thunderbird $"Some files were not attached" "$ERRORS"
  60.     fi
  61.     # Finally execute Thunderbird and exit with its exit code
  62.     thunderbird -compose "attachment='${FILES}'"
  63. else
  64.     if [ -n "$ERRORS" ]; then
  65.         notify-send --icon=thunderbird $"No files were attached!" "$ERRORS"
  66.     fi
  67.     exit 1
  68. fi
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement