How to sort email by which sender has sent the most emails

Using Mail.app, I want to find messages from the sender who has sent the most email.


Example: Let's say my inbox has,

200 messages from Yahoo News

150 from Seniors Club

350 from Food Network

etc.


I want to sort first by the number of messages, and secondly, by the sender. The list above would then be:


Messages Sender

350 Food Network

200 Yahoo News

150 Seniors Club


I'm running Mac OS X High Sierra. Is this possible?


Thanks.

iMac 27″, macOS 10.13

Posted on May 17, 2024 9:02 PM

Reply
7 replies

May 18, 2024 11:50 AM in response to kevin spake

Kevin,


Here is some AppleScript code that will gather your Mail account names and present you with a list so you can select a particular Mail account and get a descending message summary for that account in a dialog. I couldn't test it with Google Mail accounts on High Sierra due to OAuth 2.0 rejection of Apple's Mail client, but it does work with a non-Google Mail account that is established in Apple Mail. It also works fine with Apple Mail on macOS Sonoma 14.5.


Copy/paste the following code into Apple's Script Editor. Click the hammer icon and then run it.


(*
	mailmsg_summary.applescript
	
	Produce a dialog of descending message count and sender names
	Writes /tmp/CSet.txt and removes it when done
	Does not change any Mail content
	
	VikingOSX, 2024-05-18, Apple Support Communities, No warranties expressed or implied.
*)

use framework "Foundation"
use scripting additions

property ca : current application
property mailAcct : ""

set foo to {}

tell application "Mail"
	activate
	set account_names to (every account's name) as list
	set mailAcct to (choose from list account_names with title "Mail Accounts List" with prompt "Select Mail account" without multiple selections allowed and empty selection allowed) as rich text
	if mailAcct is false then return
	
	set msg_list to (every message of mailbox "INBOX" of account mailAcct) as list
	
	repeat with amsg in msg_list
		copy (extract name from amsg's sender) to the end of foo
	end repeat
	
end tell

if foo = {} then return

set rpt to my concordance(foo) as text
delay 2

set outrpt to (do shell script "awk '{printf \"%03d\\t%s\\n\",$1, substr($0, index($0, $2)) | \"sort -k1,1nr -b -k2,2\"}' /tmp/CSet.txt") as text
display dialog outrpt with title mailAcct & " Descending Summary"

tell application "Finder" to delete (POSIX file "/tmp/CSet.txt" as alias)
return

on concordance(alist)
	set report to ca's NSMutableString's |string|()
	set countedSet to ca's NSCountedSet's alloc()'s initWithArray:alist
	set descend to ca's NSSortDescriptor's alloc()'s initWithKey:(missing value) ascending:no
	set sorted to countedSet's allObjects()'s sortedArrayUsingDescriptors:[descend]
	repeat with anItem in sorted
		set n to (countedSet's countForObject:anItem)
		report's appendFormat_("%@  %@", n, anItem)
		(report's appendString:linefeed)
		(report's writeToFile:"/tmp/CSet.txt" atomically:true encoding:(ca's NSUTF8StringEncoding) |error|:(reference))
	end repeat
	return (report) as text
end concordance


May 18, 2024 2:35 PM in response to kevin spake

This solution places the header "Messages Sender" over the columns in the dialog. Otherwise, no difference to the first AppleScript post.


(*
	mailmsg_summary.applescript
	
	Produce a dialog of descending message count and sender names
	Writes /tmp/CSet.txt and removes it when done
	Does not change any Mail content
	
	VikingOSX, 2024-05-18, Apple Support Communities, No warranties expressed or implied.
*)

use framework "Foundation"
use scripting additions

property ca : current application
property mailAcct : ""

set foo to {}

tell application "Mail"
	activate
	set account_names to (every account's name) as list
	set mailAcct to (choose from list account_names with title "Mail Accounts List" with prompt "Select Mail account" without multiple selections allowed and empty selection allowed) as rich text
	if mailAcct is false then return
	
	set msg_list to (every message of mailbox "INBOX" of account mailAcct) as list
	
	repeat with amsg in msg_list
		copy (extract name from amsg's sender) to the end of foo
	end repeat
end tell

if foo = {} then return

set rpt to my concordance(foo) as text
delay 2
set rpthead to "Messages" & tab & "Sender" & linefeed
set outrpt to (do shell script "awk '{printf \"%03d\\t\\t\\t%s\\n\",$1, substr($0, index($0, $2)) | \"sort -k1,1nr -b -k2,2\"}' /tmp/CSet.txt") as text
set outContent to rpthead & outrpt
display dialog outContent with title mailAcct & " Descending Summary"

tell application "Finder" to delete (POSIX file "/tmp/CSet.txt" as alias)
return

on concordance(alist)
	set report to ca's NSMutableString's |string|()
	set countedSet to ca's NSCountedSet's alloc()'s initWithArray:alist
	set descend to ca's NSSortDescriptor's alloc()'s initWithKey:(missing value) ascending:no
	set sorted to countedSet's allObjects()'s sortedArrayUsingDescriptors:[descend]
	report's setString:("Messages" & tab & "Sender")
	repeat with anItem in sorted
		set n to (countedSet's countForObject:anItem)
		report's appendFormat_("%@  %@", n, anItem)
		(report's appendString:linefeed)
		(report's writeToFile:"/tmp/CSet.txt" atomically:true encoding:(ca's NSUTF8StringEncoding) |error|:(reference))
	end repeat
	return (report) as text
end concordance


May 19, 2024 5:29 AM in response to VikingOSX

Errata in the second post.


Remove

report's setString:("Messages" & tab & "Sender")

The report heading is already handled in the main code and introduces errors in the handler results.


Change

set outrpt to (do shell script "awk '{printf \"%03d\\t\\t\\t%s\\n\",$1, substr($0, index($0, $2)) | \"sort -k1,1nr -b -k2,2\"}' /tmp/CSet.txt") as text


To:

set outrpt to (do shell script "awk '{printf \"%4d\\t\\t\\t%s\\n\",$1, substr($0, index($0, $2)) | \"sort -k1,1nr -b -k2,2\"}' /tmp/CSet.txt") as text

That removes the leading zeroes in the report count.


It is also possible to add a mailfilter array to the main code that restricts the final report output to specific names of senders rather than all email sender names.

How to sort email by which sender has sent the most emails

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