Apple Mail Automatic Archive by Domain

I cannot take all of the credit for this since I compiled many scripts together to get this working for me and now I use it daily.

INBOX MAIL ARCHIVE
Here is the version that will search for mail older than X amount of days from the mailbox you have selected and move them to an archived location into a folder for each root domain.


-- Automatically archive messages in the inbox for one or more accounts
-- based on their age.

-- Set this value to the age of messages, in days,
-- which should not be archived
property daysToSave : 45

-- Set ARCHIVE_FOLDER to the top level folder where
-- messages should be saved. For mine I created a folder called
-- Sorted which houses all of the sub folders created by the script
property archiveFolder : "Archived/Sorted"

-- This should remain empty and will be assigned the name 
-- of any mailbox you have have selected
property accountNames : {""}

--set scriptPath to ("Macintosh HD:Users:YOURUSER:Library:Application Scripts:com.apple.mail") Or another location of your choosing
set scriptPath to ("Macintosh HD:Users:YOURUSER:Library:Application Scripts:com.apple.mail:stringtemp.scpt")
set theScript to (load script alias scriptPath)

tell application "Mail"
	
	-- Figure out the earliest date for messages we want
	-- to leave in the mailbox
	set earliestDate to ((current date) - (daysToSave * days))
	
	set accountNames to (selected mailboxes of message viewer 1)
	
	repeat with anAccountName in accountNames
		-- Set the Mailbox name of selected mailbox
		try
			set theMailbox to anAccountName
		on error
			--log "Could not archive from " & (name of anAccountName as rich text)
			set theMailbox to null
		end try
		
		if theMailbox is not null then
			-- Find the messages we want to archive
			-- In addition to mail older than X number of days it will not
			-- archive any mail that is unread, flagged or marked as junk
			set theMessages to (every message of theMailbox where (read status is true and junk mail status is false and flagged status is false and date sent < earliestDate))
			
			repeat with currentMessage in theMessages
				-- first we find the sender of the message
				set theSender to sender of currentMessage as string
				-- now we strip off the sender's domain (everything after the "@")
				set oldDelimiters to AppleScript's text item delimiters
				set AppleScript's text item delimiters to {"@"}
				try
					set theDomain to the second text item of theSender
					-- always set the text item delimiters back as soon as possible!
					set AppleScript's text item delimiters to ""
					-- and strip the last character off the domain, as it will be a ">"
					if theDomain contains ">" then
						set theDomain to leftStringFromRight(theDomain, ">") of theScript
					end if
					repeat
						-- If the senders domain contains multiple periods
						-- I remove anything to right of any period to
						-- capture just the Root domain
						if theDomain contains "." then
							set theDomain to leftStringFromRight(theDomain, ".") of theScript
						else
							exit repeat
						end if
					end repeat
					-- Format the domain so your folders look uniform
					-- This is the OCD in me
					set theDomain to theScript's capitalizeString(theDomain)
				on error
					-- If the domain could not be found then it sets the 
					-- domain to Undetermined and archives those emails
					-- into one folder. But this could be used to exit
					-- current loop and leave the messsage alone
					set theDomain to "Undetermined"
					set AppleScript's text item delimiters to ""
				end try
				
				-- Set the archive location based on the
				set mailboxName to (archiveFolder & "/" & theDomain)
				-- Find the top level mailbox.  If this does not
				-- exist, we will get an error and bail out.
				set rootMailbox to mailbox archiveFolder
				-- Find the domain archive mailbox.  If this does not
				-- exist, we create it.
				try
					set domainMailbox to (mailbox (theDomain as string) of rootMailbox)
				on error
					make new mailbox with properties {name:(archiveFolder & "/" & theDomain)}
					set domainMailbox to (mailbox (theDomain as string) of rootMailbox)
				end try
				move currentMessage to domainMailbox
				
			end repeat
			
		end if
	end repeat
end tell


SENT MAIL ARCHIVE
Here is the same thing but instead of looking at the senders email address it looks at the recipients email address and archives it into a Sent Mail archive folder of your choosing. This one is not as useful since I have to choose what could be many email addresses but it has worked for me.


-- Automatically archive messages in the inbox for one or more accounts
-- based on their age.

-- Set this value to the age of messages, in days,
-- which should not be archived
property daysToSave : 45

-- Set ARCHIVE_FOLDER to the top level folder where
-- messages should be saved. For mine I created a folder called
-- Sent Items which houses all of the sub folders created by the script
property archiveFolder : "Archived/Sent Items"

-- This should remain empty and will be assigned the name 
-- of any mailbox you have have selected
property accountNames : {""}

--set scriptPath to ("Macintosh HD:Users:YOURUSER:Library:Application Scripts:com.apple.mail") Or another location of your choosing
set scriptPath to ("Macintosh HD:Users:YOURUSER:Library:Application Scripts:com.apple.mail:stringtemp.scpt")
set theScript to (load script alias scriptPath)

tell application "Mail"
	
	-- Figure out the earliest date for messages we want
	-- to leave in the mailbox
	set earliestDate to ((current date) - (daysToSave * days))
	
	set accountNames to (selected mailboxes of message viewer 1)
	
	repeat with anAccountName in accountNames
		-- Set the Mailbox name of selected mailbox
		try
			set theMailbox to anAccountName
		on error
			--log "Could not archive from " & (name of anAccountName as rich text)
			set theMailbox to null
		end try
		
		if theMailbox is not null then
			-- Find the messages we want to archive
			-- In addition to mail older than X number of days it will not
			-- archive any mail that is unread, flagged or marked as junk
			set theMessages to (every message of theMailbox where (read status is true and junk mail status is false and flagged status is false and date sent < earliestDate))
			
			repeat with currentMessage in theMessages
				-- first we find the first recipient of the message
				try
					tell (to recipient 1 of currentMessage) to set {msgRecipientAddr} to {its address}
				on error
					set msgRecipientAddr to "Undetermined"
				end try
				set theSender to msgRecipientAddr as string
				-- now we strip off the recipient's domain (everything after the "@")
				set oldDelimiters to AppleScript's text item delimiters
				set AppleScript's text item delimiters to {"@"}
				try
					set theDomain to the second text item of theSender
					-- always set the text item delimiters back as soon as possible!
					set AppleScript's text item delimiters to ""
					-- and strip the last character off the domain, as it will be a ">"
					if theDomain contains ">" then
						set theDomain to leftStringFromRight(theDomain, ">") of theScript
					end if
					repeat
						-- If the senders domain contains multiple periods
						-- I remove anything to right of any period to
						-- capture just the Root domain
						if theDomain contains "." then
							set theDomain to leftStringFromRight(theDomain, ".") of theScript
						else
							exit repeat
						end if
					end repeat
					-- Format the domain so your folders look uniform
					-- This is the OCD in me
					set theDomain to theScript's capitalizeString(theDomain)
				on error
					-- If the domain could not be found then it sets the 
					-- domain to Undetermined and archives those emails
					-- into one folder. But this could be used to exit
					-- current loop and leave the messsage alone
					set theDomain to "Undetermined"
					set AppleScript's text item delimiters to ""
				end try
				
				-- Set the archive location based on the
				set mailboxName to (archiveFolder & "/" & theDomain)
				-- Find the top level mailbox.  If this does not
				-- exist, we will get an error and bail out.
				set rootMailbox to mailbox archiveFolder
				-- Find the domain archive mailbox.  If this does not
				-- exist, we create it.
				try
					set domainMailbox to (mailbox (theDomain as string) of rootMailbox)
				on error
					make new mailbox with properties {name:(archiveFolder & "/" & theDomain)}
					set domainMailbox to (mailbox (theDomain as string) of rootMailbox)
				end try
				move currentMessage to domainMailbox
				
			end repeat
			
		end if
	end repeat
end tell