Add recipient to a forwarded email. Syntax error

Hi All,

Trying to select an email and forward it to a nominated account. I have scripts that generate new emails and the ‘make new recipient’ works fine, but it appears to be failing here:


tell application "Mail"
	activate
	tell application "System Events" to keystroke "f" using {command down, shift down}
	
	set mymsg to the selection
	
	tell mymsg
		
		make new recipient at the end of recipients with properties {name:"staff", address:"staff@company.com"}
		
	end tell
end tell

The error that’s generated is :

{«class mssg» id 31050 of «class mbxp» “# Precheck Job Bags 2013” of application “Mail”} doesn’t understand the make message.

Is the ‘set mymsg to the selection’ correct?
Also tried ‘current message’
and ‘window 1’

Not sure what the correct syntax is.

Thanks in Advance

Hello again Matt,

I understand that you’re having problems with mail rules. Well forwarding a mail message doesn’t have to be done with system events because mail also have the forward command. According to the documentation from Apple to have an Mail rule script you should wrap your code in an special handler:

using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		--insert code here
	end perform mail action with messages
end using terms from

This handler gives you two paramaters theMessages and theRule. theMessages is an list of mails that match your rule, so you don’t have to work on selection or anything but on the given messages by this handler.


using terms from application "Mail"
	on perform mail action with messages theMessages for rule theRule
		tell application "Mail"
			repeat with thisMessage in theMessages
				set newMessage to forward thisMessage without opening window
				make new to recipient at end of to recipients of newMessage with properties {name:"staff", address:"staff@company.com"}
				send newMessage
			end repeat
		end tell
	end perform mail action with messages
end using terms from

This code is tested (with an valid mail adres) and works as expected.

I’ll hope the code speaks for itself

edit: There is a small note about mail rules mountain lion

Hi,

outgoing messages created with ⇧⌘F are not mutable with AppleScript,
but there is a forward command in the dictionary


tell application "Mail"
	set messageToForward to item 1 of (get selection)
	set outgoingMessage to forward messageToForward opening window yes
	tell outgoingMessage
		make new to recipient at the end of to recipients with properties {name:"staff", address:"staff@company.com"}
	end tell
end tell

consider that there are to recipients, cc recipients and bcc recipients, so just recipients is not sufficient