--------------------------------------------------------------------------------
set folderName to "attachments" # Change the name if you wish
set downloadFolder to (path to downloads folder as text) # Don't change this instruction
set attachmentsFolder to downloadFolder & folderName & ":" # Don't remove the ending colon
tell application "System Events"
if not (exists folder attachmentsFolder) then
make new folder at end of folder downloadFolder with properties {name:folderName}
end if
end tell
tell application "Mail"
set selectedMessages to every message of mailbox "aaa" # Of course, it's just an example
repeat with theMessage in selectedMessages
repeat with theAttachment in theMessage's mail attachments
set PosixName to name of theAttachment
# CAUTION, if the name of the file contain some slashes,
# it replace them by colons. So the next instruction reset the slashs.
set originalName to my remplace(PosixName, ":", "/")
set savePath to attachmentsFolder & originalName
try
save theAttachment in file (savePath)
end try
end repeat
end repeat
end tell
#=====
(*
replaces every occurences of d1 by d2 in the text t
*)
on remplace(t, d1, d2)
local oTIDs, l
set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d1}
set l to text items of t
set AppleScript's text item delimiters to d2
set t to l as text
set AppleScript's text item delimiters to oTIDs
return t
end remplace
--------------------------------------------------------------------------------