I’m using an old version of Mail but this works for me. You may have to check Mail’s dictionary to confirm that the items used below have not changed. I would suggest closing all of Mail’s windows except for the message viewer, in which you have selected a single message with an attachment. You may have to do something to allow you to save files and perhaps someone can assist with that as it’s not required with my OS version.
Before getting started, to keep things simple, specify a destination folder on the desktop, which is where any attachments will be saved. Create a folder with the name ‘repository’ on the desktop and put this at the top of the script.
-- this will refer to a folder named 'repository' that is on the desktop
set savePath to ((path to desktop) as text) & "repository:"
--> "MacHD:Users:username:Desktop:repository:"
To clear up something important… Mail has ‘selected messages’, which is a property of ‘message viewer’, so begin by getting the message viewer. We don’t have to specify an account here because the selection inherently determines the account and mailbox. Run a script containing only the command below and it should return a result that looks like the last line and provides a way to specify the viewer, which we will then use to get the selected messages.
tell application "Mail"
message viewer 1
end tell
--> message viewer id 1 of application "Mail"
We could probably continue to work with the above but now that we know the message viewer ID, I’d rather be more explicit and use the following:
tell application "Mail"
message viewer id 1
end tell
--> message viewer id 1 of application "Mail"
Next, we get the ‘selected messages’ of our viewer, and this returns a list of the selected messages — which at this time is a single message. We assign this list to a variable (sm).
set sm to selected messages of message viewer id 1
--> {message id 4901 of mailbox "INBOX" of account id "EF608919-4803-5043-FCAF-34021926ABBA" of application "Mail"}
Then, we cycle through the list of selected messages and save any attachments to the specified folder. Since a message may have multiple attachments, we are working with a list even when there is but a single attachment, so we will cycle through that list as well.
set sm to selected messages of message viewer id 1
repeat with eachMessage in sm
mail attachment of eachMessage
end repeat
--> {mail attachment id "2" of message id 4901 of mailbox "INBOX" of account id "EF608919-4803-5043-FCAF-34021926ABBA" of application "Mail"}
By the way, if there was a second message selected that did not have an attachment, the results might look something like this below… a nested list where one of the contained lists was empty.
set allAttach to {}
repeat with eachMessage in sm
set end of allAttach to mail attachment of eachMessage
end repeat
allAttach
--> {{}, {mail attachment id "2" of message id 4901 of mailbox "INBOX" of account id "EF608919-4803-5043-FCAF-34021926ABBA" of application "Mail"}}
And finally, to put it all together:
set savePath to ((path to desktop) as text) & "repository:"
tell application "Mail"
set sm to selected messages of message viewer id 1
repeat with eachMsg in sm
repeat with eachAttach in (mail attachments of eachMsg)
--> item 1 of every mail attachment of item 1 of {message id 4901 of mailbox "INBOX" of account id "EF608919-4803-5043-FCAF-34021926ABBA" of application "Mail"}
set nom to name of eachAttach
--> "Winter 2024.pdf"
save contents of eachAttach in file (savePath & nom as string)
--> save item 1 of every mail attachment of message id 4901 of mailbox "INBOX" of account id "EF608919-4803-5043-FCAF-34021926ABBA" in file "MacHD:Users:username:Desktop:repository: Winter 2024.pdf"
end repeat
end repeat
end tell
NB If multiple attachments have the same filename, there will be a conflict when saving them
Assuming this works for you, select a couple of messages and try again.