Introduzione:
Quello che vi propongo oggi è lo script più “raw” della collezione: funziona ma necessiterebbe di alcune rifiniture ulteriori, che non sono esattamente semplici da mettere in atto per caratteristiche intrinseche della shell Bash e del formato .emlx. Ad ogni modo, ho deciso di condividerlo comunque, perchè girando sulla Rete mi sono reso conto che nessuno ha sviluppato una soluzione del genere finora.
Lo script mail2book.sh:
Mettiamo caso che riceviate/inviate una serie di e-mail il cui titolo inizi con la stessa stringa, a mò di etichetta, per esempio: “[Pink News]“:
[Pink News] Puntata #1
[Pink News] Puntata #2
[Pink News] Puntata #3
[Pink News] Puntata #4
[Pink News] Bonus estate
…
Tutte queste e-mail (titolo + corpo) volete raggrupparle in un unico file di testo, proprio a mò di libro.
Ecco il mio script fa proprio questo.
I problemi principali di cui soffre sono però due:
1. la shell Bash non ha un supporto degno allo standard Unicode, per cui tutte le accentate verrano sostituite con codici strani, tipo “=E8″, il che è decisamente fastidioso (una soluzione parziale potrebbe essere la funzione “MailSubjectCleanUp” inclusa nello script ma non adoperata);
2. il corpo delle e-mail potrebbe contenere “immondizia” di varia natura, come signatures etc, che andranno ripulite a mano.
Prima di eseguire lo script, cambiate il contenuto delle variabili “mail_folder” e “mail_subject”, sostituendola col il path della vostra inbox e con la parte di titolo che accomuna le e-mail che volete raggruppare nel libro. Infine, da Terminale entrate nella cartella dello script e date i comandi:
chmod +x mail2book.sh #per dargli i permessi di esecuzione sh mail2book.sh #per lanciarlo
Il codice sorgente
Ecco il codice dello script. Commenti, variabili e messaggi a schermo sono in inglese per renderlo comprensibile anche all’estero qualora servisse a qualche straniero.
#!/bin/sh
# eldino's mail2book v1.0
# take as input a folder full of e-mails in .emlx format (Apple Mail)
# and create a sort of book with all the bodies of the e-mails with a given
# subject
#
# for example: if you have a bunch of e-mails with a subject starting with "tech news",
# you can create a book out of them
# CUSTOMIZE THE VARIABLE(S) BELOW BEFORE EXECUTING THE SCRIPT:
mail_folder="/Users/ruben/Library/Mail/POP-numbworks@pop.gmail.com/INBOX.mbox/Messages"
mail_subject="Subject: \[Pink News\]"
# --------You don't need to modify anything below!--------
# get the folder from which the script is running
# source: http://www.mydatabasesupport.com/forums/shell/176524-currently-executing-script-path.html
prog_path=$(cd -P -- "$(dirname -- "$0")" && pwd -P) && prog_path=$prog_path/$(basename -- "$0")
scriptFolder=$(echo $(dirname $prog_path))
# function: clear some garbage that you could find in the e-mail
# instead of accented letters etc.. you can improve
# this function yourself, basing on your language needs
MailSubjectCleanUp() {
echo $* | sed 's/=?windows-1252?Q?//g' | sed 's/=?ISO-8859-1?Q?//g' | sed 's/=5B/[/g' | sed 's/=5D/]/g' | sed 's/=3A/:/g' | sed 's/=E8/è/g' | sed 's/_/ /g' | sed 's/=E0/ì/g' | sed "s/=27/'/g"
}
# function: extract the body of the e-mail
MailBodyExtract() {
nTotalLines=`cat -n $* | tail -1 | cut -f1`
BlockStartLineNumber=`cat -n $* | grep "\-\-00" | cut -f1 | head -1`
BlockEndLineNumber=`cat -n $* | grep "\-\-00" | cut -f1 | head -2 | tail -1`
shift_up=`expr $nTotalLines - $BlockStartLineNumber - 3`
shift_down=`expr $BlockEndLineNumber - $BlockStartLineNumber - 4`
cat $* | tail -$shift_up | head -$shift_down
}
# header
clear
echo "eldino's mail2book v1.0"
echo "---------------------------------------------------"
echo
# show intro advice
echo "The chosen mail_folder is:\n " $mail_folder "\nDo you wish to continue? [y/n]"
echo "WARNING: THE SCRIPT WILL DELETE THE CONTENT OF 'OUTPUT' SUBFOLDER, SO IF YOU NEED IT BACKUP IT NOW BEFORE PROCEED!"
read answer
if test "$answer" != "y"
then
echo "You typed 'n' or something else, so I assume it's No."
echo "Customize the mail_folder variable in the script and try again."
exit
fi
# test: if folder doesn't exist..
echo
echo "Checking if the mail_folder exists..."
if test -d $mail_folder
then
echo "Ok: The mail_folder exists!"
else
echo "Error: The given mail_folder doesn't exists."
echo "Fix the mail_folder variable in the script and try again."
exit
fi
# test: if folder is empty, display error and stop script
echo
echo "Counting emails in mail_folder..."
mail_count=`ls $mail_folder | grep -c .emlx`
if test $mail_count == 0
then
echo "Error: Folder is empty!"
exit
else
echo "Ok: You have" $mail_count "e-mails in the given folder."
fi
# test: if the output subfolder doesnt' exist, create it
echo
echo "Checking output subfolder..."
if test -d $scriptFolder/output/
then
echo "Ok: The output subfolder exists!"
else
echo "Error: The output subfolder doesn't exists. Creating it.."
mkdir $scriptFolder/output/
echo "Ok: Done!"
fi
# routine: remove and create fresh support files
# from 'output' subfolder inside 'mail_folder'
echo
echo "Removing support files from 'output' subfolder..."
echo $scriptFolder/output/
rm -f $scriptFolder/output/_mail2book.txt
touch $scriptFolder/output/_mail2book.txt
echo "Ok: Done!"
# routine: browse all the files in the folder
echo
echo "Adding bodies to the book..."
actual_count=0
for file in $mail_folder/*
do
# if the file contains the give subject, it contains a body to extract
if test `cat $file | grep -c "$mail_subject"` == 1
then
# retrieve final subject
final_subject=`cat $file | grep "$mail_subject" | sed 's/Subject: //g'`
# save subject to file
echo $final_subject $'\n' >> $scriptFolder/output/_mail2book.txt
# save body to file
MailBodyExtract $file >> $scriptFolder/output/_mail2book.txt
echo $'\n' $'\n' >> $scriptFolder/output/_mail2book.txt
fi
# display the % of progress
actual_count=`expr $actual_count + 1`
echo "["`date "+20%y-%m-%d %H:%M"`"]" "Processed: " $actual_count "/" $mail_count "(" `echo "scale=4; ($actual_count / $mail_count) * 100" | bc` "%)"
done
echo "Ok: Done!"
# footer
echo
echo "Finish! Check the result files inside the following folder:"
echo $scriptFolder/output/
echo
echo "Thank you for using a script by eldino!"
Screenshots:
Di seguito posto qualche screenshot dello script in funzione, tanto per darvene un’idea


[...] [IT] Bash: come interfacciarsi con Apple Mail via script (Mac OS X Leopard) – Parte 4/4 Parte 1/4, Parte 2/4, Parte 3/4 [...]
[...] Post & sourcecode here [...]