Copy mail content and attachments from e-mails in Mac Mail

I was looking for a way to easily get information from emails sent to me via a web form into an Excel file on my Mac. I've used applescript to do that and I want to show some basic examples on this page that may be useful to others as well. You can also find a script to copy Excel files from emails and put them in a local folder on your Mac. Note: I'm not an expert, but I like to use AppleScript because sometimes it's only possible with applescript or it's much easier than having to do the same thing with VBA code. Many thanks to the visitors on MacScripter.net for sharing their knowledge.

If you not know how to run AppleScript follow the steps below :

How do I run AppleScript on my Mac

  • Click on the Spotlight icon in the top right corner of your Mac.
  • Enter "Script Editor" to find and open this program.
  • Click the New Document button, and we are ready to start.

Tip
: Right click on the Script Editor icon in the dock and choose Options>Keep in Dock so it is easy the next time to open it when you need it.

  • Click on one of the examples scripts below and you will notice that the whole script is selected
  • press Command c to copy it to the clipboard
  • click in the Script Editor
  • Use Command v to paste the script inside the editor
  • Compile the script by pressing the hammer icon next to the run button
  • Change the mailbox and account name
  • Press the Run button

Copy content from e-mails that are in specific mailbox into Excel workbook


You can run the script below in the Script Editor to get the info and mail content from the e-mails from the account and mailbox that you can choose when you run the script. You can run the script without changing anything to create a new Excel file with the info and content from the e-mails in it. You can save the file if you want and do with the data what you want. I use VBA to clean up my data and split it into columns for example but you can also use worksheet functions to do the job. All depending how the data looks like. This example will loop through the first 5 e-mails in the mailbox, Read the information in the script to change this.

Note: In the second example on this page, you can fill in the account name and mailbox name so you if you always use the same account and mailbox you do not have to choose both in the script each time.


The only thing that you have to change after you paste the second script below in the Script Editor is the account name and the name of the mailbox in the script line below. In the first example on this page you can choose both when you run the script. This example will loop through the first 5 e-mails in the mailbox, Read the information in the script to change this.

set MyMailBox to mailbox "Inbox" of account "Exchange"

The account names are easy to find when you use Mail>Accounts in the menu bar with Mail active. Or you can run this script line one time to get the names

tell application "Mail" to get the name of every account

And you can try the name of the mailbox that you see in Mail first, but the correct name of the mailbox can be sometimes difficult to find and if you got problems the easiest way to find the exact name is to run this script line one time in the Script Editor :

tell application "Mail" to get the name of every mailbox of account "Exchange"

For example I must use the Dutch word "Postvak IN" for "Inbox" that I see in Mail on my English Macbook, also mailbox names are case-sensitive so it is maybe possible that you must use "INBOX"

When you run the script below it will open a new Excel file and copy all info and mail content inside that file, you can save the file if you want and do with the data what you want. I use VBA to clean up my data and split it into columns for example but you can also use worksheet functions to do the job. All depending how the data looks like.


In the mails that I got from my webform there where U+2028 line separators, In Excel it is not so easy to replace the U+2028 line separator so I looked for a solution inside the script to replace them with "_" so I can split the text on that into columns, you can see the solution that I add to the script above in the script below. The other cleanup and splitting the data into columns I do in Excel with VBA.

Copy attachments from e-mails that are in specific mailbox into local folder


This script makes a folder on your Desktop named "RDB-Mail-Attachments" and copy the Excel attachments that are in the e-mails to this local folder, if the folder already exists it will first delete all the files in it before it will paste the new files in it. It is possible that you must give permission when you run the script because it create a folder on your Desktop. I choose in this example to get Excel files, but you can change that in the script if you want. I often use a mail rule that move e-mails with files in it to a certain mailbox, the script is handy to get the files out of the e-mails so I can get information out of them with VBA for example. This example will loop through the first 20 e-mails, see the script for how to change this. It also add a number before each file so you not have a problem with duplicate file names.

11/11/2024
Web design by Will Woodgate