I am new to applescript, and would appreciate some help with a project. I have a Mail rule set to move all messages containing a certain subject to a mailbox named “Test”. I receive a new message with the same subject everyday. I’m trying to write a script that opens Mail (got that part), searches the “Test” mailbox, opens the message with a given subject that was received “toda”. I have the mailbox set to sort by date received descending; so the message I am trying to access is always the first message listed. I then want to open the attachment that I receive in the email. Any help with this would be greatly appreciated. Thank you in advance.
Are you just trying to open the attachments, or do you also need the accompanying messages visible as well? What sort of attachments are involved? Documents, images, PowerPoints? Are there ever more than a single attachment to the messages?
Thank you for your reply. There are roughly 5 emails that I receive each day that I’m trying to work with. Each email contains only 1 attachment. It is either a .pdf or an Excel file, but there is one email that contains a zip file, that when unzipped contains an Excel file. I do not need to open the email. I simply need to download the attachment from each email, and save it to a folder on my desktop. The subject of the five emails is the same every day. The files are the same everyday except the filename contains the date it was created, so that changes everyday. I’m wanting to delete all files in my folder, then download the attachments from those 5 emails to that folder. For example; on Monday, I want the script to delete the files in the folder (Friday’s files), download each attachment from those 5 emails received today, and save them to that folder, then open all the files. I hope this helps to better describe what it is I’m trying to do. Please ask more questions if you need more clarifacation. Thank you again.
Try this script:
using terms from application "Mail" on perform mail action with messages The_Messages set to_day to (current date) set to_day's time to 0 --Sets the comparison time to the previous midnight set work_Folder to CleanFolder(to_day) --Folder stuff cannot be perfomed within the Mail rule handlers, so we send it out tell application "Mail" to repeat with This_Message in The_Messages set att_file to (work_Folder & (name of first mail attachment of This_Message)) save first mail attachment of This_Message in att_file end repeat end perform mail action with messages end using terms from ---------------------------------------------------------------------- to CleanFolder(td) --This handler will send any files that arrived before the previous midnight to the Trash and return the name of the folder as well set work_Folder to ((path to desktop as Unicode text) & "Claudia:") tell application "Finder" to delete ((every file in folder work_Folder) whose modification date < td) return work_Folder end CleanFolder
It should address the folder named Claudia on your desktop (you should change it to the folder’s real name) every time the script is called, as you set it up in your Mail rules. Any file in that folder that was put there before the previous midnight should be moved to the Trash, and all the attachments should be saved to that folder with their original names.
Let me know if this works for you, then we can tackle the opening of the newly saved files.
Thank you very much for the help. I have not had a chance to try the code yet. I’ll be trying it later today. From just looking at it, I do have one question. Will this look inside a specific mailbox in Mail and open the 5 emails based off subject (in addition to date)? In other words…I’m wanting the script to open Mail, look in mailbox “test” and download the attachments from “Email 1”, “Email 2”, “Email 3”, “Email 4”, “Email 5” that were received today? IF the code you sent does all this I apologize for the question. I just wanted to double check. Thank you again, and i’ll let you know how the code works when I’m able to try it out.
This does NOT go to Mail and examine any mailboxes, rather, it is designed to be activated by Mail when the specific messages arrive. You need to go to Mail Preferences, and set up a Rule that calls this script when any of the messages arrives that you want processed. For instance; I assume that you have already set up a rule to put the messages into the proper mailbox in Mail, so you would simply select that rule, click Edit, and add an action for Run Applescript, then select the saved script. (You can save the script anywhere. When you select Run AppleScript for a Mail rule, you can use the choose button to find the script where every you have saved it.) Your messages would then be both saved to the proper mailbox in Mail and the attachments would be saved to the folder that you want (after you replace the term Claudia (in the script I sent you) with the name of the folder you are going to use.
This should be a non-destructive test; that is, if it does not work correctly the first time, the attachment files will not be lost. They will be in the Trash, and they should also still be attached to the original messages so that you can go back and get them again if necessary.