Complicated mail forwarding logic - thoughts on where I'm wrong here?

Hi gang -

I’ve been trying to digest what I’ve found here as quickly as possible, as we’ve (my company) had an AppleScript expert and no longer have access to him (suddenly) - leaving us to try to solve a large forwarding issue via Mac Mail and mail rules.

While we can forward fine with Mac Mail, and may just go back to that, we cannot seem to change the subject line of the incoming message via AppleScript. I’ve included the code we’ve put together and I’m hoping anyone might point us in the correct direction. If necessary, we could simply go to AppleScript that changes the outgoing mail so it has a distinct subject (the original software that sends these notifications does not allow this).

  1. I’d love any input that you can share on this.
  2. Is there a particular type of AppleScript (compiled, compiled bundle, straight .AppleScript) file that can be used to process a mail rule or can any format work?

THANKS IN ADVANCE for ANY suggestions you might provide!
Christopher



use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

using terms from application "Mail"
	on perform mail action with messages messageList for rule aRule
		set PrimaryRecipient to ""
		set SecondaryRecipient to ""
		set TertiaryRecipient to ""
		set LevelAdminReceipient to "allison@boguscompany.com"
		set ArcaleaAdminRecipient to "chris@arcalea.com"
		set ArcaleaAdminBackupRecipient to "mike@arcalea.com"
		
		tell application "Mail"
			repeat with eachMessage in messageList -- your code goes here...
				set newSubject to "Booking "
				set nameForSubject to ""
				set newForSubject to ""
				set newMsgText to "FORWARDED FROM TOUR BOOKING SYSTEM" & return
				set msgText to content of eachMessage
				set incomingSubject to subject of eachMessage
				set subjectLine to subject of eachMessage
				if subjectLine contains "Monroe" then
					set PrimaryRecipient to "kcarswell@boguscompany.com"
					set SecondaryRecipient to "dalila@boguscompany.com"
					set TertiaryRecipient to "corinne@boguscompany.com"
				else if subjectLine contains "Adams" then
					set PrimaryRecipient to "kcarswell@boguscompany.com"
					set SecondaryRecipient to "ann@boguscompany.com"
					set TertiaryRecipient to "annie@boguscompany.com"
				else if subjectLine contains "Wacker" then
					set PrimaryRecipient to "kcarswell@boguscompany.com"
					set SecondaryRecipient to "ellen@boguscompany.com"
					set TertiaryRecipient to ""
				else if subjectLine contains "Rusk" then
					set PrimaryRecipient to "paige@boguscompany.com"
					set SecondaryRecipient to "diane@boguscompany.com"
					set TertiaryRecipient to ""
				else if subjectLine contains "Main" then
					set PrimaryRecipient to "diane@boguscompany.com"
					set SecondaryRecipient to "ralitsa@boguscompany.com"
					set TertiaryRecipient to ""
				else if subjectLine contains "Dallas" then
					set PrimaryRecipient to "danielle@boguscompany.com"
					set SecondaryRecipient to "Margaret@boguscompany.com"
					set TertiaryRecipient to ""
				else if subjectLine contains "Charlotte" then
					set PrimaryRecipient to "mollie@boguscompany.com"
					set SecondaryRecipient to "kendall@boguscompany.com"
					set TertiaryRecipient to ""
				else if subjectLine contains "Jacksonville" then
					set PrimaryRecipient to "susan@boguscompany.com"
					set SecondaryRecipient to "roxann@boguscompany.com"
					set TertiaryRecipient to ""
				else if subjectLine contains "Seattle" then
					set PrimaryRecipient to "ingrid@boguscompany.com"
					set SecondaryRecipient to "machaela@boguscompany.com"
					set TertiaryRecipient to ""
				else if subjectLine contains "Minden" then
					set PrimaryRecipient to "chrislarkin@20zen.com"
					set SecondaryRecipient to "dylanxditto@gmail.com"
					set TertiaryRecipient to ""
				else
					set PrimaryRecipient to "allison@boguscompany.com"
					set SecondaryRecipient to ""
					set newMsgText to "Some ERROR has occurred. This message was flagged to be forwarded but does not match the rules as defined." & return
					set subjectLine to "ERROR" & subjectLine
				end if
				
				repeat with eachline in (paragraphs of msgText)
					if ((eachline as rich text) starts with "Customer name:") then
						set newForSubject to "Tour Booking " & eachline
						display dialog nameForSubject
					end if
					--if ((eachline as rich text) starts with "Date") then
					-- set nameForSubject to nameForSubject & " " & eachline
					--end if
				end repeat
				-- display dialog msgText with title nameForSubject
			end repeat
			if msgText ≠ "" then
				if siteSelection(incomingSubject) contains "ERROR" then
					log "ERROR" & return & incomingSubject & return & msgText
				end if
				set newMsg to make new outgoing message with properties {subject:newForSubject, content:newMsgText & msgText} -- EDITED
				tell newMsg
					-- make new to recipient at end of to recipients with properties {address:"notreal@20zen.com"}
					-- make new to recipient at end of to recipients with properties {address:"fakename@gmail.com"}
					make new to recipient at end of to recipients with properties {address:PrimaryRecipient}
					display dialog PrimaryRecipient
					if SecondaryRecipient ≠ "" then
						make new to recipient at end of to recipients with properties {address:SecondaryRecipient}
						display dialog SecondaryRecipient
					end if
					if TertiaryRecipient ≠ "" then
						make new to recipient at the end of to recipients with properties {address:TertiaryRecipient}
						display dialog TertiaryRecipient
					end if
					make new to recipient at the end of to recipients with properties {address:ArcaleaAdminRecipient}
					make new to recipient at the end of to recipients with properties {address:ArcaleaAdminBackupRecipient}
					if PrimaryRecipient ≠ "allison@boguscompany.com" then
						make new to recipient at the end of to recipients with properties {address:LevelAdminReceipient}
					end if
				end tell
				set theResult to send newMsg
				log newForSubject
			end if
		end tell
	end perform mail action with messages
end using terms from