Pulling recipient out of a received mail message

So I have found a script that lets me send an autoreply to a Mail message using a rule. The script is written to send the reply to the sender of the message, but I would actually like to a send a reply to the recipient of the message (I am BCC’d on these messages). But for some reason when I change “sender” to recipient in the script, it doesn’t work.

Here’s the original version of the script:

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		tell application "Mail"
			repeat with eachMessage in theMessages
				set theName to extract name from sender of eachMessage
				if exists reply to of eachMessage then
					set theAddress to reply to of eachMessage
				else
					set theAddress to extract address from sender of eachMessage
				end if
				set newMessage to make new outgoing message
				tell newMessage
					make new to recipient at end of to recipients with properties {name:theName, address:theAddress}
					set visible to false
					set subject to "Re: " & subject of eachMessage
					set content to "###enter message content here###"
				end tell
				send newMessage
				tell eachMessage
					set was replied to to true
				end tell
			end repeat
		end tell
	end perform mail action with messages
end using terms from

Here’s my edited version:

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		tell application "Mail"
			repeat with eachMessage in theMessages
				set theName to extract name from [b]recipient[/b] of eachMessage
				set theAddress to extract address from [b]recipient[/b] of eachMessage
				set newMessage to make new outgoing message
				tell newMessage
					make new to recipient at end of to recipients with properties {name:theName, address:theAddress}
					set visible to false
					set subject to "Re: " & subject of eachMessage
					set content to "###enter message content here###"
				end tell
				send newMessage
				tell eachMessage
					set was replied to to true
				end tell
			end repeat
		end tell
	end perform mail action with messages
end using terms from

Where am I going wrong?

Hi,

it doesn’t work because sender is just a text property while recipient is an element
with address, name and class properties.
Consider also that an email can contain multiple recipients.

So you must specify the recipient, get its address and then you can use the extract address from command

I see – how do I do that? I’ve modified the script a little bit since the last post:

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		tell application "Mail"
			repeat with eachMessage in theMessages
				set theName to name of first recipient of eachMessage
				set theAddress to address of first recipient of eachMessage
				set newMessage to make new outgoing message
				tell newMessage
					make new to recipient at end of to recipients with properties {name:theName, address:theAddress}
					set visible to false
					set subject to "Re: " & subject of eachMessage
					set content to "###enter message content here###"
				end tell
				send newMessage
				tell eachMessage
					set was replied to to true
				end tell
			end repeat
		end tell
	end perform mail action with messages
end using terms from

Ok, got it to work! But this seems awkward – must be a more elegant way:

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		tell application "Mail"
			repeat with eachMessage in theMessages
				set theNewName to name of first recipient of eachMessage
				set theNewAddress to address of first recipient of eachMessage
				set theName to extract name from theNewName
				set theAddress to extract name from theNewAddress
				set newMessage to make new outgoing message
				tell newMessage
					make new to recipient at end of to recipients with properties {name:theName, address:theAddress}
					set visible to false
					set subject to "Re: " & subject of eachMessage
					set content to "###enter message content here###"
				end tell
				send newMessage
				tell eachMessage
					set was replied to to true
				end tell
			end repeat
		end tell
	end perform mail action with messages
end using terms from

extracting is not neccessary


using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		tell application "Mail"
			repeat with eachMessage in (get selection)
				set {name:newName, address:newAddress} to first recipient of eachMessage
				set newMessage to make new outgoing message
				tell newMessage
					make new to recipient at end of to recipients with properties {name:newName, address:newAddress}
					set visible to false
					set subject to "Re: " & subject of eachMessage
					set content to "###enter message content here###"
				end tell
				send newMessage
				tell eachMessage
					set was replied to to true
				end tell
			end repeat
		end tell
	end perform mail action with messages
end using terms from

Thank you sir!