Hello
Here is a short script which I use from time to time. The last time I ran it upon a mailbox with 712 messages.
It saved 462 files with no problem.
They are : scripts (.scpt), zip archives, Numbers documents, Jpg, png, tiff, Numbers templates, PDF text files
As you see I didn’t saved folders or packages which weren’t well treated in the past but I had none of them recently.
It’s a subset from a long script written for a French user so, it is mostly commented in French and some variable names resemble to french words
# Étape 26 - Enregistrement des pièces jointes
#
# Défini le dossier de stockage des pièces jointes
set attachmentsFolder to (path to documents folder as text) & "Attachments" # ATTENTION, on ne met pas de caractère ":" final
do shell script "/bin/mkdir -p " & quoted form of (POSIX path of attachmentsFolder)
tell application "Mail"
set theMessages to selection
# Définit la variable mailSubFolder par défaut afin qu'elle existe même s'il n'y a pas de pièce jointe
set mailSubFolder to false
repeat with eachMessage in theMessages
-- set the sub folder for the attachments to the first part of senders email before a period
-- All future attachments from this sender will the be put here.
set {senderName, sender_Mail, theTopicSubject_, mailDate_} to my getMailInfos() # instruction appelant un handler
log {senderName, sender_Mail, theTopicSubject_, mailDate_}
tell mailDate_ to set dateStamp to space & (((its year) * 10000 + (its month) * 100 + (its day)) as rich text) & "_" & rich text 2 thru -1 of ((1000000 + (its hours) * 10000 + (its minutes) * 100 + (its seconds)) as rich text)
# ATTENTION, on n'avait pas mis de caractère ":" final
set mailSubFolder to attachmentsFolder & ":" & senderName # ATTENTION, on ne met pas de caractère ":" final
do shell script "/bin/mkdir -p " & quoted form of (POSIX path of mailSubFolder)
# Enregistrement des pièces jointes
# S'il existe déjà un fichier du même nom, ajoute la date-heure du mail pour différencier
try
repeat with theAttachment in eachMessage's mail attachments
--log (count theAttachment)
try
# ATTENTION, on n'avait pas mis de caractère ":" final
set attachmentName to name of theAttachment
tell application "System Events"
if exists disk item (mailSubFolder & ":" & attachmentName) then
if attachmentName ends with ":" then set attachmentName to text 1 thru -2 of attachmentName
if attachmentName contains "." then
set nameAsList to my decoupe(attachmentName, ".")
set attachmentName to my recolle(items 1 thru -2 of nameAsList, ".") & dateStamp & "." & item -1 of nameAsList
else
set attachmentName to attachmentName & dateStamp
end if
end if
end tell
save theAttachment in file (mailSubFolder & ":" & attachmentName)
on error errMsg number errNbr
log "error #" & errNbr & ", " & errMsg
end try
end repeat
on error err_MSG number err_NBR
log "error #" & err_NBR & ", " & err_MSG
end try
end repeat # with eachMessage.
end tell # Mail
#===== Called from treatAmail
#
on getMailInfos()
# define the local variables whose value will be send to the caller
local sender_Name, sender_Mail, mail_Subject, mail_Date
log return & "Entering the handler : getMailInfos"
tell application "Mail"
tell item 1 of (get selection) # extracts datas from the selected message
set sender_Name to extract name from its sender
set sender_Mail to extract address from its sender
set mail_Subject to subject
set mail_Date to date received # ATTENTION, on conserve la composante time
end tell # the message
end tell # Mail
# send the extracted values to the caller code
return {sender_Name, sender_Mail, mail_Subject, mail_Date}
end getMailInfos
#=====
on recolle(l, d)
local oTids, t
set {oTids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
set t to l as text
set AppleScript's text item delimiters to oTids
return t
end recolle
#=====
on decoupe(t, d)
local oTids, l
set {oTids, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
set l to text items of t
set AppleScript's text item delimiters to oTids
return l
end decoupe
#====
Yvan KOENIG (VALLAURIS, France) mardi 14 avril 2015 21:19:30