Create Mail messages with attachments from Terminal

Oct 12, '04 10:55:00AM

Contributed by: Anonymous

Here is a Unix script that I use to create an email in Mail containing a number of attachments that I specify via the command line. If you save the script as, say, attachments, then you create the email by entering something like attachments foo.jpg bar.txt. Remember to chmod +x the script and move it into someplace in your path, like /usr/local/bin.

#!/bin/sh

FILE_LIST=""
while [ "$*" != "" ]; do
  FILE_LIST="$FILE_LIST , POSIX file \"$1\""
  shift
done

osascript \
-e "      set file_list to { $FILE_LIST }        " \
-e "      tell application \"Mail\"              " \
-e "        open file_list                       " \
-e "        activate                             " \
-e "      end tell                               "
Note that the obvious way to do this with a droplet and on open fails to work because using the open(1) command causes the on open to receive the first file passed in, rather than the list of files. I imagine this is a bug.

Comments (8)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20041007213921761