Applescript to choose a message in mail & download its attachment

Hi,


I'd like to know if there's a way to prompt the user to choose a mail message like how we are able to choose file / folder?


Mainly, I'd like the process to go like:

Choose a mail, download its attachment, perform actions on the attachment.


I'm stuck at how I can reference a message as well for the downloading of the attachment. ATM, I'm only able to get the message ID by already selecting the mail message in Mail, and doing "set my_Sel to get selection".


Thank you.

OS X El Capitan (10.11.4)

Posted on Jun 16, 2016 11:39 PM

Reply
4 replies

Jun 17, 2016 1:12 AM in response to kluznic

It might be possible, but cumbersome, at best.


It would be far easier to have the user pre-select the message in question and then invoke the script, but if you want to go the other way you likely need to:


a) get a list of all the messages in the mailbox

b) present the user with a list of messages via choose from list

c) act on the result


What's your objection to acting on the selection?

Jun 17, 2016 1:23 AM in response to Camelot

Alright, I think the first method might be better. But how should I use the message ID? Could you give me an example snippets that uses message ID of messages?


tell application "Mail"

set the Inbox to mailbox "testing"


set theMessageID to get selection

set theMessage to message theMessageID

try

repeat with theAttachment in theMessage's mail attachments

set originalName to name of theAttachment

set savePath to (path to desktop) & originalName as text

try

save theAttachment in file savePath

end try


end repeat


end try

end tell


I get an error trying to set theMessage to message theMessageID, "Mail got an error: Can’t get message {message id 613 of mailbox "testing" number -1728".

Jun 17, 2016 1:55 PM in response to kluznic

This fails because of the line:


set theMessageID to get selection


'get selection' will aways return a list, even if there is only one message selected - heck, even if there are no messages selected, you'll still get a list back... it'll just be empty.


That's why the script fails when you try to:


set theMessage to messagetheMessageID


because you can't coerce a list to a message.


The solution is simple... extract the message from the list:


set theSelectedMessages to get selection

set theMessage to first item of theSelectedMessages


Now you can act on theMessage.


Alternatively, maybe there is a case where you want to support multiple selected messages, in which case you could just iterate through theSelectedMessages and process them in turn.


set theSelectedMessages to get selection

repeat with eachMessage in theSelectedMessages


...

end repeat

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

Applescript to choose a message in mail & download its attachment

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.