Some ASObjC instructions make part of the job.
--------------------------------------------------------------------------------
use AppleScript version "2.4" # Yosemite or later
use framework "Foundation"
use scripting additions
#=====#=====#=====#=====#=====#=====
on buildFullPath:proposedName inFolder:POSIXPath # appelé par une instruction
local |⌘|, theFolderURL, proposedName, theDestURL
set |⌘| to current application
set theFolderURL to |⌘|'s |NSURL|'s fileURLWithPath:POSIXPath
if class of proposedName is text then set proposedName to |⌘|'s NSString's stringWithString:proposedName
# If proposedName is an Hfs name, it's converted into a POSIX one
set proposedName to proposedName's stringByReplacingOccurrencesOfString:"/" withString:":"
set theDestURL to theFolderURL's URLByAppendingPathComponent:proposedName
return theDestURL's |path| as text
end buildFullPath:inFolder:
#=====#=====#=====#=====#=====#=====
-- Creates a new folder. There is no error if the folder already exists, and it will also create intermediate folders if required
on createFolderNamed:proposedName inFolder:POSIXPath # appelé par une instruction
local |⌘|, theFolderURL, theDestURL, theFileManager
set |⌘| to current application
set theFolderURL to |⌘|'s |NSURL|'s fileURLWithPath:POSIXPath
if class of proposedName is text then set proposedName to |⌘|'s NSString's stringWithString:proposedName
# If proposedName is an Hfs name, it's converted into a POSIX one
set proposedName to proposedName's stringByReplacingOccurrencesOfString:"/" withString:":"
set theDestURL to theFolderURL's URLByAppendingPathComponent:proposedName
set theFileManager to |⌘|'s NSFileManager's |defaultManager|()
-- set {theResult, theError} to theFileManager's createDirectoryAtURL:theDestURL withIntermediateDirectories:true attributes:(missing value) |error|:(reference)
theFileManager's createDirectoryAtURL:theDestURL withIntermediateDirectories:true attributes:(missing value) |error|:(missing value)
-- if not (theResult as boolean) then error (theError's |localizedDescription|() as text)
return theDestURL's |path| as text
end createFolderNamed:inFolder:
#=====#=====#=====#=====#=====#=====
on sortAList:theList
set theArray to current application's NSArray's arrayWithArray:theList
set theArray to theArray's sortedArrayUsingSelector:"localizedStandardCompare:"
return theArray as list
end sortAList:
#=====#=====#=====#=====#=====#=====
on replace:sourceString existingString:d1 newString:d2
set sourceString to current application's NSString's stringWithString:sourceString
return (sourceString's stringByReplacingOccurrencesOfString:d1 withString:d2) as text
end replace:existingString:newString:
#=====#=====#=====#=====#=====#=====
tell application "Mail"
set theProperties to properties of every mailbox
set boxNames to {}
# Concatenate if required the name of the container and the name of the box
# I'm not fond of this part repeat with aBox in theProperties
tell aBox
set theName to its name
try
set theContainer to name of its container
set end of boxNames to theContainer & "/" & theName
on error
set end of boxNames to theName
end try
end tell # aBox
end repeat
end tell # Mail
# Sort the list of boxes
set boxNames to my sortAList:boxNames
# Select the one whose attachment(s) must be saved
using terms from scripting additions
set aBox to choose from list boxNames # returns a list or a boolean
end using terms from
# It's a boolean if we cancel the actionif class of aBox is boolean then error number -128
# Extracts the unique item of the listset aBox to item 1 of aBox
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 targetFolder to POSIX path of (downloadFolder & "the attachments")
# This instruction create the two levels of subfolders if needed
set attachmentsFolder to my createFolderNamed:folderName inFolder:targetFolder
tell application "Mail"
set selectedMessages to every message of mailbox aBox
repeat with theMessage in selectedMessages
repeat with theAttachment in theMessage's mail attachments
set PosixName to name of theAttachment
# Builds the POSIX path to the saved file and convert it in a format accepted by Mail
set savePath to (my buildFullPath:PosixName inFolder:attachmentsFolder) as «class furl»
try
save theAttachment in savePath
end try
end repeat
end repeat
end tell
--------------------------------------------------------------------------------