--------------------------------------------------------------------------------
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:
#=====#=====#=====#=====#=====#=====
on splitWithTwo:sourceString usingStrings:{d1, d2}
set sourceString to current application's NSString's stringWithString:sourceString
set anNSString to item 2 of (sourceString's componentsSeparatedByString:d1)
return (item 1 of (anNSString's componentsSeparatedByString:d2)) as text
end splitWithTwo:usingStrings:
#=====#=====#=====#=====#=====#=====
tell application "Mail"
set theMailBoxes to every mailbox
set boxNames to {"Inbox", "Drafts"}
# Concatenate if required the name of the container and the name of the box
repeat with aBox in theMailBoxes
try
aBox as rich text # Generates an error message
on error errMsg number errNbr
--> (*Impossible de convertir «class mbxp» "fournisseurs/logiciels achetés/Carbon Copy Cloner" of application "Mail" en type text.*)
set aBox to (my splitWithTwo:errMsg usingStrings:{"«class mbxp» " & quote, quote & " of application"})
set end of boxNames to aBox
end try
end repeat
end tell # Mail
# Sort the list of boxes
set boxNames to my sortAList:boxNames
# Select the one whose attachment 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 action
if class of aBox is boolean then error number -128 # Extracts the unique item of the list
set 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"
if aBox is in {"Inbox", "Outbox", "Deleted Messages", "Drafts"} then
set theAccounts to name of accounts
tell me to set theAccount to choose from list theAccounts # returns a list or a boolean
# It's a boolean if we cancel the action
if class of theAccount is boolean then error number -128
# Extracts the unique item of the list
set theAccount to item 1 of theAccount
set theAccount to first account whose name is theAccount
if aBox is "Inbox" then
set selectedMessages to every message of mailbox "INBOX" of theAccount
else if aBox is "Outbox" then
set selectedMessages to every message of mailbox "Sent Messages" of theAccount
else if aBox is "Deleted Messages" then
set selectedMessages to every message of mailbox "Deleted Messages" of theAccount
else
set selectedMessages to every message of mailbox "Drafts" of theAccount
end if
else
set selectedMessages to every message of mailbox aBox
end if
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
--------------------------------------------------------------------------------
Yvan KOENIG running Sierra 10.12.2 in French (VALLAURIS, France) jeudi 5 janvier 2017 16:48:50