Are you looking to extract email addresses from your Apple Mail inboxes without manually sifting through each message? This comprehensive tutorial will guide you through the process using AppleScript, a scripting language for macOS that can automate various tasks. This guide is perfect for beginners and ensures you can efficiently gather email addresses from your inboxes and save them to a CSV file.
Table of Contents
Why Use AppleScript?
AppleScript is a powerful scripting language for macOS that allows you to automate repetitive tasks. It’s especially useful for tasks like extracting email addresses from your Apple Mail inbox, saving you countless hours of manual work.
Prerequisites
Before we dive into the tutorial, ensure you have the following:
- A Mac running macOS.
- Basic familiarity with Apple Mail.
- The Script Editor application (included with macOS).
Step-by-Step Tutorial
Opening Script Editor
To get started, you need to open the Script Editor on your Mac:
- Open
Finder. - Go to
Applications. - Navigate to
Utilities. - Double-click on
Script Editorto open it.
Writing the AppleScript
In the Script Editor, you’ll write a script to extract email addresses from your inboxes. Here’s the complete script you’ll use:
applescriptCopy code-- Function to write the email list to a CSV file
on write_to_file(this_data, target_file)
try
set the target_file to the target_file as text
set the open_target_file to open for access file target_file with write permission
write this_data to open_target_file starting at eof
close access open_target_file
return true
on error
try
close access file target_file
end try
return false
end try
end write_to_file
-- Main script
tell application "Mail"
set allAccounts to (get accounts)
set resultList to {}
set totalEmailsProcessed to 0
repeat with anAccount in allAccounts
set inboxMailbox to mailbox "INBOX" of anAccount
set theMessages to messages of inboxMailbox
set totalEmails to count of theMessages
repeat with eachMessage in theMessages
try
set senderEmail to extract address from sender of eachMessage
set senderName to extract name from sender of eachMessage
set firstName to ""
set lastName to ""
if senderName contains " " then
set firstName to word 1 of senderName
set lastName to word -1 of senderName
else
set firstName to senderName
end if
if {firstName, lastName, senderEmail} is not in resultList then
set end of resultList to {firstName, lastName, senderEmail}
end if
set totalEmailsProcessed to totalEmailsProcessed + 1
if totalEmailsProcessed mod 100 = 0 then
display dialog "Processed " & totalEmailsProcessed & " emails so far" buttons {"OK"} giving up after 1
end if
end try
end repeat
end repeat
end tell
-- Convert list to CSV format
set csvText to "First Name, Last Name, Email Address" & return
repeat with anItem in resultList
set {firstName, lastName, senderEmail} to anItem
set csvText to csvText & firstName & ", " & lastName & ", " & senderEmail & return
end repeat
-- Save CSV to desktop
set filePath to (path to desktop as text) & "email_list.csv"
set fileExported to write_to_file(csvText, filePath)
-- Notify user of completion
if fileExported then
display dialog "Email addresses have been exported to a CSV file on your desktop."
else
display dialog "Failed to export email addresses to a CSV file."
end if
Understanding the Script
Function to Write Data to a File
This function writes the email addresses to a CSV file on your desktop.
applescriptCopy codeon write_to_file(this_data, target_file)
try
set the target_file to the target_file as text
set the open_target_file to open for access file target_file with write permission
write this_data to open_target_file starting at eof
close access open_target_file
return true
on error
try
close access file target_file
end try
return false
end try
end write_to_file
Main Script
This part of the script iterates through all accounts and their inboxes to extract email addresses.
applescriptCopy codetell application "Mail"
set allAccounts to (get accounts)
set resultList to {}
set totalEmailsProcessed to 0
repeat with anAccount in allAccounts
set inboxMailbox to mailbox "INBOX" of anAccount
set theMessages to messages of inboxMailbox
set totalEmails to count of theMessages
repeat with eachMessage in theMessages
try
set senderEmail to extract address from sender of eachMessage
set senderName to extract name from sender of eachMessage
set firstName to ""
set lastName to ""
if senderName contains " " then
set firstName to word 1 of senderName
set lastName to word -1 of senderName
else
set firstName to senderName
end if
if {firstName, lastName, senderEmail} is not in resultList then
set end of resultList to {firstName, lastName, senderEmail}
end if
set totalEmailsProcessed to totalEmailsProcessed + 1
if totalEmailsProcessed mod 100 = 0 then
display dialog "Processed " & totalEmailsProcessed & " emails so far" buttons {"OK"} giving up after 1
end if
end try
end repeat
end repeat
end tell
Convert List to CSV Format
This section converts the extracted email addresses to a CSV format.
applescriptCopy code-- Convert list to CSV format
set csvText to "First Name, Last Name, Email Address" & return
repeat with anItem in resultList
set {firstName, lastName, senderEmail} to anItem
set csvText to csvText & firstName & ", " & lastName & ", " & senderEmail & return
end repeat
Save CSV to Desktop and Notify User
Finally, the script saves the CSV file to your desktop and notifies you when the process is complete.
applescriptCopy code-- Save CSV to desktop
set filePath to (path to desktop as text) & "email_list.csv"
set fileExported to write_to_file(csvText, filePath)
-- Notify user of completion
if fileExported then
display dialog "Email addresses have been exported to a CSV file on your desktop."
else
display dialog "Failed to export email addresses to a CSV file."
end if
Running the Script
- Open Script Editor:
- Navigate to
Applications>Utilitiesand openScript Editor.
- Navigate to
- Paste the Script:
- Copy the complete script provided above and paste it into the Script Editor.
- Run the Script:
- Click the
Runbutton in the Script Editor to execute the script.
- Click the
Once the script is complete, it will save a file named email_list.csv to your desktop containing all senders’ first names, last names, and email addresses in your inbox.
Conclusion
Using AppleScript to automate the extraction of email addresses from your Apple Mail inbox can save you significant time and effort. This tutorial has walked you through the entire process, from opening the Script Editor to running the script and saving the results in a CSV file. Happy scripting!
By following this guide, you should be able to efficiently gather email addresses from your inbox and streamline your workflow. If you have any questions or have issues, please comment below.



