Introduzione:
Dopo lo script Bash che estrae tutte le parole dal corpo delle vostre email e genera una lista delle parole più usate presentato nella prima parte, vado ora ad illustrarvi un secondo script, che svolge un altro compito decisamente utile chi usa intensamente Gmail (leggere: Tutorial: come creare una newsletter e/o una mailing-list gratuitamente e velocemente usando GMail).
Lo script mail2subjectslist.sh:
Vi siete mai chiesti quali sono le e-mail dal titolo più ricorrente nella vostra casella di posta? Oppure le e-mail che generano più risposte (replies)? Se sì, questo script vi solleticherà il palato.
Una volta eseguito infatti, genera una lista “grezza” dei titoli (subjects) di tutte le e-mail trovate nella cartella data e successivamente ne crea un’altra in cui i titoli sono ordinati per frequenza.
La lista “grezza” (_subjects_tmp.txt) avrà la seguente forma:
[eldino's Shock News] Addio a Gary Coleman il piccolo Arnold della tv
Re: [eldino's Shock News] Addio a Gary Coleman il piccolo Arnold della tv
Re: [eldino's Local News] Japan in love (Ancona)
Re: [eldino's Local News] Japan in love (Ancona)
Re: [eldino's Fun Pics] Cat iPad stand
mentre la lista ordinata (_subjects-list.txt) sarà una roba del tipo:
2 Re: [eldino's Local News] Japan in love (Ancona)
2 Re: [eldino's Fun Pics] Cat iPad stand
1 [eldino's Shock News] Addio a Gary Coleman il piccolo Arnold della tv
1 Re: [eldino's Shock News] Addio a Gary Coleman il piccolo Arnold della tv
Questi due files vengono salvati nella sottocartella “output” presente nella cartella da cui viene lanciato lo script, e come vedete, contengono delle informazioni molto comode in numerose situazioni
Prima di eseguire lo script, cambiate il contenuto della variabile “mail_folder”, sostituendola col il path della vostra inbox. Infine, da Terminale entrate nella cartella dello script e date i comandi:
chmod +x mail2subjectslist.sh #per dargli i permessi di esecuzione sh mail2subjectslist.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 mail2subjectslist v1.0
# take as input a folder full of e-mails in .emlx format (Apple Mail)
# and create a sorted list with all the most frequent subjects.
# CUSTOMIZE THE VARIABLE(S) BELOW BEFORE EXECUTING THE SCRIPT:
mail_folder="/Users/ruben/Library/Mail/POP-numbworks@pop.gmail.com/INBOX.mbox/Messages"
# --------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"
}
# header
clear
echo "eldino's mail2subjectslist 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/_subjects-list.txt
rm -f $scriptFolder/output/_subjects_tmp.txt
touch $scriptFolder/output/_subjects-list.txt
touch $scriptFolder/output/_subjects_tmp.txt
echo "Ok: Done!"
# routine: browse all the files in the folder
echo
echo "Generating subjects list..."
actual_count=0
for file in $mail_folder/*
do
# if the file contains subject, it's an e-mail
if test `cat $file | grep -c "Subject:"` == 1
then
# create the subjects list
tmp_subject=`cat $file | grep "Subject:" | sed 's/Subject: //g'`
MailSubjectCleanUp $tmp_subject >> $scriptFolder/output/_subjects_tmp.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!"
# sort the list by number of occurences and create the final list
echo
echo "Sorting subjects list..."
sort $scriptFolder/output/_subjects_tmp.txt | uniq -c | sort -nr > $scriptFolder/output/_subjects-list.txt
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) (Mac OS X Leopard) – Parte 3/4 Parte 1/4, Parte 2/4 [...]
[...] come interfacciarsi con Apple Mail via script (Mac OS X Leopard) – Parte 4/4 Parte 1/4, Parte 2/4, Parte [...]
[...] Post & sourcecode here [...]