Mail.app: Can't edit msg created by Safari

Our nonprofit refurbishes used wheelchairs and then re-sells them real affordably to those who couldn’t otherwise afford them. We get a lot of our chairs because I request them from people who list them on Craigslist.

I click on the link for a wheelchair in Craigslist, which then opens an email in Eudora with all the info from the listing populated in it. Then I use applescript to reply to the email, ask them to donate their chair, and tell them about our nonprofit.

Recently I’ve had to upgrade to Mail.app and I can’t seem to figure out how to accomplish that in Mail. Safari creates a new email in Mail, but I can’t change/edit it with Applescript to tell people about our nonprofit and ask them to donate their chair. I’ve tried using the “source” of the new email to create a second new email but just can’t seem to get the syntax right. Any help would be appreciated.

Here’s what I’ve got so far. (I miss Eudora.)


tell application "Mail"
	#I don't know how to even talk to the newly-created message
	#tell message 0 of mailbox "Drafts" of account "Members" ?
	tell last outgoing message
		set EmailSubject to the subject
		set EmailContent to the content
		set SourceText to the source
	end tell
	
	#get their email from the source text
	set StartText to offset of "To:" in SourceText
	set SourceText to strings StartText thru -1 in SourceText
	set EndText to offset of "
" in SourceText
	set EndText to EndText - 1
	set EmailTo to strings 5 thru EndText in SourceText
	
	#Create the email
	set TheOutgoingMessage to make new outgoing message with properties {subject:EmailSubject, content:"Info about our nonprofit and asking them for their chair."}
	tell TheOutgoingMessage to make new to recipient at end of to recipients with properties {address:EmailTo}
end tell

Model: Mac Mini
AppleScript: 2.4
Browser: Safari 537.36 OPR/45.0.2552.888
Operating System: Mac OS X (10.10)

If I read you correctly, you will be receiving an e-mail in Mail from which you garner the information for requesting the wheel chair. With this assumption, all you need to do is to select (highlight–no need to open) the message containing the information and run the following slightly modified version of your script.


tell application "Mail"
	set theSelection to selection
	set theMsg to item 1 of theSelection
	
	set EmailSubject to the subject of theMsg as string
	set EmailContent to the content of theMsg as string
	set SourceText to the source of theMsg as string
	set EmailRecipient to the to recipient of theMsg
	
	#get their email from the source text
	set StartText to offset of "To:" in SourceText
	set SourceText to strings StartText thru -1 in SourceText
	set EndText to offset of "
" in SourceText
	set EndText to EndText - 1
	set EmailTo to strings 5 thru EndText in SourceText
	
	#Create the email
	set TheOutgoingMessage to make new outgoing message with properties {subject:EmailSubject, content:"Info about our nonprofit and asking them for their chair."}
	tell TheOutgoingMessage to make new to recipient at end of to recipients with properties {address:EmailTo}
end tell

So sorry, you do not need to include the Set EmailRecipient script step.

This is great; almost there. Thanks!

The one remaining problem is that when Safari (via Craigslist) creates a new outgoing message in Mail, it is not listed anywhere in the browser window. So


set theSelection to selection

doesn’t work unless I can select that email in the browser window. Otherwise I’m left trying to “tell” the new outgoing message something but not knowing how to identify it.

In Eudora I could just “Tell message 0”, which identified the top message. Doesn’t work here. Is there a way to identify it directly without referring to the browser window?

Try using a Mail Rule to identify what you want to process as they come in.