You may find interesting code in this script writen to help an user which wanted to save attachments in a rtfd file whose text part is the message itself.
In this case, the mails to treat were stored in a dedicated mailbox.
I leave the change of this feature as an exercise 
####################################################
#
property useMessageDate : true
# true = extracts the date from the body of the message and stamp the file with it
# false = use the current date-time to stamp the file with it
#
property substractTimeToGMT : true
# true = substract time to GMT from dates
# false = doesn't substract time to GMT
#
property stampWithShell : true
# true = use Shell script to build date-time stamp
# false = use horodateur to build date-time stamp
#
property stampTheMessages : true
# true = insert date-time stamp in fileMessages names
# false = leave fileMessages names without stamp
#
property moveToFinalStorage : true
# true = move the fileMessages to the folder "final_storage"
# (where they may conflict if you stamp them according to "date received")
# false = leave the fileMessages on the Desktop
# (where they may conflict if you run the script twice with no stamp)
#
####################################################
property ptd : missing value
#
# For safe, activate GUIScripting if it's not already active.
#
my activateGUIscripting()
beep 1
tell application "SystemUIServer"
activate
display dialog "CAUTION !" & return & "Don't move the cursor while the script is running." giving up after 30
end tell
set ptd to path to desktop as text
my SubRoutine1()
set path2FinalFolder to missing value
set ptd to missing value
#=====
on SubRoutine1()
local Cyclethroughmessages, wName
tell application "Mail"
activate
{}
try
messages of mailbox "* items to shift" as list
end try
end tell # Mail
repeat with Cyclethroughmessages in reverse of result
#
# This loop isn't entered if grabbing the messages failed or returned an empty list.
#
tell application "Mail"
activate
set selected mailboxes of message viewers to mailbox "* items to shift"
set the selected messages of message viewers to Cyclethroughmessages
set wName to name of window 1
#
# Extracts date received from the message.
# May be replaced by date which is the same than date sent.
#
{date received, count (every mail attachment)} of Cyclethroughmessages
my SubRoutine2(wName, result)
end tell # Mail
end repeat
end SubRoutine1
#=====
on SubRoutine2(windowName, {date_received, nb_Attachments})
local nameOfFileMessage, theExt
# step 1, save the message on the Desktop.
# The next steps are executed if the message
# embed attachments which aren't passed in the rtfd file.
# step 2, save the attachments in a folder on the Desktop.
# step 3, move the attachment from the folder to the rtfd document
# step 4, move the message file in the dedicated folder.
set nameOfFileMessage to my saveTheMessage(windowName)
if nameOfFileMessage ends with ".rtf" then
set theExt to ".rtf"
# here if there was no attachment !
else
set theExt to ".rtfd"
#
# step 2 The file message is a rtfd one. Check if it contain the attachment files.
#
# Grab the count of attachment files existing in the rtfd package.
# Substract 1 because the count is :
# 1 (for the TXT.rtf file) + count attachment files
#
tell application "System Events"
(count every disk item of disk item (ptd & nameOfFileMessage)) - 1
end tell
if result ≠nb_Attachments then
#
# Attachment files weren't embedded !
# Save the full set in a folder on the Desktop.
#
my saveAndPassAttachments(windowName, nameOfFileMessage)
end if # result ≠nb_Attachments
end if # nameOfFileMessage ends with .
#
# Step 4, move the file path2FileMessage to its final location.
#
if moveToFinalStorage then
if stampTheMessages then
set stampedName to my stampMessageFile(nameOfFileMessage, date_received, theExt)
my moveMessageToFinalStorage(stampedName, date_received, theExt)
# the stamped message is in final_storage
else
my moveMessageToFinalStorage(nameOfFileMessage, date_received, theExt)
# the bare message is in final_storage
end if # stampTheMessages
else
if stampTheMessages then
set stampedName to my stampMessageFile(nameOfFileMessage, date_received, theExt)
# the stamped message is on the Desktop
else
# the bare message is on the Desktop
end if # stampTheMessages
end if # moveToFinalStorage
end SubRoutine2
#=====
on saveTheMessage(window_Name)
local theNameOfFileMessage
activate application "Mail"
tell application "System Events" to tell process "Mail" to tell window window_Name
#
# Step 1, save the message on the Desktop
#
# Issue the shortcut for "Save As."
keystroke "s" using {command down, shift down}
repeat until exists sheet 1
delay 0.1
end repeat
tell sheet 1
keystroke "d" using {command down, shift down} # to save on the Desktop
try
if value of checkbox 1 of group 1 = 0 then click checkbox 1 of group 1
end try
tell group 1 to tell pop up button 1
click
repeat
try
name of menu item 1 of menu 1 # try to grab the name of the menu item appearing in the "button"
exit repeat # exit when the pop up button is activated, the grabbed name is the "displayed" one
on error
delay 0.1
end try
end repeat
--name of menu items of menu 1
--> {"Format texte enrichi", "Format texte", "Source du message brut"}
#
# The list is always displaying the names of menu items in this order
# so there is no need to know the localized name of the menu item.
#
click menu item 1 of menu 1
end tell # group 1 (of sheet 1).
set theNameOfFileMessage to value of text field 1
--name of buttons
--> {"Enregistrer", "Nouveau dossier", "Annuler"}
click button 1 # Click the button "Save"
end tell # sheet 1
end tell # System Events.Mail.window
#
# Loop to be sure that the TextEdit file is written on disk
#
tell application "System Events"
repeat until exists disk item (ptd & theNameOfFileMessage)
delay 0.1
end repeat
end tell # System Events
return theNameOfFileMessage
end saveTheMessage
#=====
on saveAndPassAttachments(window_Name, name_FileMessage)
local PrintDateTimeName, newFolderPath, path2FileMessage, wCreate, dName, errmsgg
#
# Attachment files weren't embedded !
# Save the full set in a folder on the Desktop.
#
# Create a unique name for the temporary folder
#
if stampWithShell then
set PrintDateTimeName to my dateTimeStamp()
else
if substractTimeToGMT then
set PrintDateTimeName to my horoDateur((current date) - (time to GMT))
else
set PrintDateTimeName to my horoDateur(current date)
end if # substractTimeToGMT
end if # stampWithShell
set newFolderPath to ptd & PrintDateTimeName # folder with attachments files.
set path2FileMessage to ptd & name_FileMessage
activate application "Mail"
tell application "System Events"
tell process "Mail"
tell menu bar 1 to tell menu bar item 3 to tell menu 1
--name of menu items
(*{
01 - "Nouveau message",
02 - "Nouvelle fenêtre de visualisation",
03 - "Ouvrir Message",
04 - missing value,
05 - "Fermer",
06 - "Fermer toutes les fenêtres",
07 - "Enregistrer",
08 - "Enregistrer sous.",
09 - "Enregistrer comme modèle.",
10 - missing value,
11 - "Joindre des fichiers.",
12 - "Enregistrer les pièces jointes.", <<<<==================
13 - "Coup d'œil sur les pièces jointes.",
14 - missing value,
15 - "Ajouter un compte.",
16 - "Importer des boîtes aux lettres.",
17 - missing value,
18 - "Imprimer."
}*)
click menu item 12 # Click menu item "Save attachments."
end tell # menu bar 1 .
tell window window_Name
repeat
delay 0.2
if exists sheet 1 then exit repeat
end repeat
tell sheet 1
keystroke "d" using {command down, shift down} # to save on the Desktop
--name of buttons
--> {"Enregistrer", "Nouveau dossier", "Annuler"}
click button 2 # Click the button "New Folder"
end tell # sheet 1
end tell # window window_Name
repeat
try
delay 0.1
set wCreate to (name of first window whose subrole is "AXSystemDialog")
exit repeat
end try
end repeat
tell window wCreate
keystroke PrintDateTimeName # Type the name of the folder to create
--name of buttons
--> {"Créer", "Annuler"}
click button 1 # Click the button "Create" (the new folder)
end tell # window wCreate
delay 0.2
tell window window_Name to tell sheet 1
--name of buttons
--> {"Enregistrer", "Nouveau dossier", "Annuler"}
click button 1 # Click the button "Save"
end tell # window window_Name
end tell # process
repeat until (exists folder newFolderPath)
delay 0.1
end repeat
end tell # System Events
#
# Step 3
# Move the attachments from the temp folder to the rtfd document.
# I dislike the Finder but to open a folder and copy its contents, there is no alternative
#
tell application "Finder"
activate # REQUIRED to issue the keystrokes
open folder newFolderPath
delay 0.2 # The Finder is lazy so this delay may be useful
tell application "System Events" to tell application process "Finder"
keystroke "a" using {command down} # "Select All" (the files which were attached)
delay 0.2
keystroke "c" using {command down} # "Copy" (to clipboard)
end tell # System Events.
delay 0.2
close window PrintDateTimeName
end tell # Finder
try
set dName to ""
tell application "TextEdit"
activate
#
# the dictionary states that the descriptor must be an alias but file is a valid one
#
open file path2FileMessage
repeat
try
delay 0.25
set dName to name of front document
if dName = name_FileMessage then exit repeat
end try
end repeat
#
# The loop inserted a delay linked to the document size
# Now, we are sure that the document is really open ;-)
#
tell front document
make new paragraph at after last paragraph with data return & return & return
end tell # Front document
activate application "TextEdit" # REQUIRED to issue key code and keystroke
tell application "System Events" to tell application process "TextEdit"
#
# Move the cursor to then end of the document !
#
key code 125 using {command down}
delay 0.4 # A delay is required here !
#
# Paste the file(s) at the end of the document.
#
keystroke "v" using {command down}
end tell # System Events.
close document dName with saving
end tell # TextEdit
on error errmsg
try
tell application "TextEdit" to close document dName with saving
end try
tell application "SystemUIServer" to display dialog errmsg
end try
#
# Use a shell script because from time to time, System Events fails to delete the folder.
#
do shell script "rm -R " & quoted form of POSIX path of newFolderPath
end saveAndPassAttachments
--=====
on stampMessageFile(nameOf_FileMessage, the_Date, the_Ext)
if useMessageDate then
if substractTimeToGMT then
my horoDateur(the_Date - (time to GMT))
else
my horoDateur(the_Date)
end if # substractTimeToGMT
else
if stampWithShell then
my dateTimeStamp()
else if substractTimeToGMT then
my horoDateur((current date) - (time to GMT))
else
my horoDateur(current date)
end if #stampWithShell
end if # useMessageDate
set finalName to (text 1 thru -(1 + (count the_Ext)) of nameOf_FileMessage) & space & result & the_Ext
tell application "System Events"
set name of disk item (ptd & nameOf_FileMessage) to finalName
end tell # System Events
return finalName
end stampMessageFile
--=====
on moveMessageToFinalStorage(final_Name)
local p2docs, nameFinalFolder, path2FinalFolder, finalName
set p2docs to path to documents folder as text
set nameFinalFolder to "final_storage Æ’"
set path2FinalFolder to p2docs & nameFinalFolder
tell application "System Events"
if not (exists folder path2FinalFolder) then make new folder at end of folder p2docs with properties {name:nameFinalFolder}
end tell # System Events
quoted form of POSIX path of (ptd & final_Name)
do shell script "mv " & result & space & quoted form of POSIX path of path2FinalFolder
end moveMessageToFinalStorage
#=====
on dateTimeStamp()
if substractTimeToGMT then
return (do shell script "date -u +%Y-%m-%d" & character id 160 & "%H%M%SZ")
else
return (do shell script "date +%Y-%m-%d" & character id 160 & "%H%M%SZ")
end if # substractTimeToGMT
end dateTimeStamp
#=====
on horoDateur(une_date)
tell une_date to return (((its year) * 10000 + (its month) * 100 + (its day)) as text) & "_" & text 2 thru -1 of ((1000000 + (its hours) * 10000 + (its minutes) * 100 + (its seconds)) as text)
end horoDateur
#=====
on activateGUIscripting()
(* to be sure than GUI scripting will be active *)
tell application "System Events"
if not (UI elements enabled) then set (UI elements enabled) to true
end tell
end activateGUIscripting
#=====
Yvan KOENIG (VALLAURIS, France) mardi 16 octobre 2012 18:28:18