Apple Mail - How to Select Specific Message and Send Again

I am attempting to end up with a script that will open mail, select a specific email (that will be kept in a specific location as “template”), and evoke the MESSAGE > SEND AGAIN feature in Mail.

I started with seeing what Automator could accomplish. I’ve been able to use 1. Get Specified Mail Items, and 2. Display Mail Messages. I thought great! Now I just need to use 3. Execute AppleScript to run a System Events keystroke for the command-shift-d for SEND AGAIN.

Unfortunately, this did not work as even though the first two automator steps pull up a window with the message in question displayed, it’s not selected (highlighted), so the System Events Keystroke fails.

I’ve been searching for several hours now and tried many different things to step 3. to no avail.

I looked at the Results in Automator of the 1st step to find the information for the specific file. The result was in this format: {message id 69587 of mailbox “INBOX/TEMPLATES” of account “larry@imagine” of application “Mail”}

I tried several things I found on this forum. The last I tried was this…

tell application "Mail"
	set selected messages of first message viewer to (1st message of TEMPLATES of account "larry@imagine") whose id is 69587
end tell

tell application "System Events"
	tell process "Mail" to keystroke "d" using {command down, shift down}
end tell

The ERROR I receive is: Mail got an error: Can’t make TEMPLATES of account “larry@imagine” into type reference.

I’m sure all of this can be done independent of Automator… but without a solid background in AppleScript, I cannot seem to piece together code that works.

Thank you for taking a look.

Got a little further…

tell application "Mail"
	activate
	set selected messages of message viewer to message of mailbox "INBOX/TEMPLATES" of account "larry@imagine" whose id is 69587
end tell

tell application "System Events"
	tell process "Mail" to keystroke "d" using {command down, shift down}
end tell

… but now the ERROR IS: Mail got an error: Can’t get id of mailbox.

I looked in the mail dictionary and there is nothing for the send again command but I was thinking that if this message in question is always the same. We could use Applescript to create a new message with everything you need in it. Subject, content… and attachment too if there is.

The message in question is always the same. However, the “Template” I made by pasting some content (including a table) from Pages so it has a certain formatting. Automator has a one step method for creating a new message with content, but I would not accept the formatting (it converted it to plain text, stripping the table, etc.).

The reason behind the formatting is that I’m wanting a receptionist to be able to use the script menu to bring up a company formatted MEMO email that has areas for who called, contact info, nature of the call, etc. I could do it in plain text if I absolutely have to, but I was hoping to be able to make this work.

Would the script allow for a formatted body like I have described?

I was thinking I don’t necessarily have to use “Send Again” since the To: will be changed anyhow. “Redirect” is in the script Library, but I’m not getting the following oversimplified script to work. Again, new at scripting so I’m likely missing something terribly obvious.

tell application "Mail"
	activate
	set theMessage to 1st message of mailbox "INBOX/TEMPLATES" of account "larry@imagine" whose id is 58927
	redirect theMessage
end tell

The error I get is

Can’t make «class mssg» id 58927 of «class mbxp» “INBOX/TEMPLATES” of «class mact» “larry@imagine” of application “Mail” into the expected type. (-1700)

OK, figured it out through some trial and error. This works!

Thanks for all the help!

tell application "Mail"
	activate
	open (1st message of mailbox "INBOX/TEMPLATES" of account "larry@imagine" whose id is 58927)
end tell

tell application "System Events"
	tell process "Mail"
		tell menu bar 1
			tell menu bar item "Message"
				tell menu "Message"
					click menu item "Send Again"
				end tell
			end tell
		end tell
	end tell
end tell

Oh, a quick and easy way to get the EXACT information about a specific mail message is to use Automator.

Specifically drop in the action “Get Specified Mail Items”, then choose “Add…”, then browse to the message in the appropriate account and select it. For simplicity, I created a new Mailbox just for Templates I want to send.

Click on the “Results” button at the bottom of the action and then the “{ }” button. Hit “Run” to see the results.

There may be an easier way to retrieve these details, but it works.