Unless you're dying to program this yourself, you should probably check out Webmailer. It does pretty much exactly what you're looking for — a handler for mailto
links and other system email actions that will redirect them to Gmail, or whatever else you want (it can work with scripts or any URL you want to give it). It's no longer maintained, but I believe it works with Mountain Lion, and it's not the sort of thing that's likely to break anytime soon.
This is certainly possible with AppleScript. Here are some resources and snippets to help you craft your ideal script.
The final AppleScript combines the content of any selected e-mails and prepares an outgoing e-mail ready for sending. You can embed this AppleScript within an Automator workflow, or save it as an application for double-clicking.
Getting the Contents of Selected Messages
From Automating Spamcop:
set raw to {}
tell application "Mail"
set msgs to selection
if length of msgs is not 0 then
repeat with msg in msgs
set messageSource to source of msg
set raw to raw & messageSource
set background color of msg to gray
end repeat
end if
end tell
Sending E-mail with AppleScript
From Send Email using Applescript:
set recipientName to "WhiteHat"
set recipientAddress to "someemail@here.com"
set theSubject to "Type your subject here!"
set theContent to "Type your message content here!"
tell application "Mail"
##Create the message
set theMessage to make new outgoing message with properties {subject:theSubject, content:theContent, visible:true}
##Set a recipient
tell theMessage
make new to recipient with properties {name:recipientName, address:recipientAddress}
##Send the Message
send
end tell
end tell
Combine, Create, and Send
Combining the two snippets above gives the following AppleScript:
set recipientName to "The Boss"
set recipientAddress to "boss@example.com"
set theSubject to "Type your subject here!"
set theCombinedContent to ""
tell application "Mail"
set msgs to selection
if length of msgs is not 0 then
repeat with msg in msgs
set theCombinedContent to theCombinedContent & (content of msg)
-- set background color of msg to gray
end repeat
set theMessage to make new outgoing message with properties {subject:theSubject, content:theCombinedContent, visible:true}
tell theMessage
make new to recipient with properties {name:recipientName, address:recipientAddress}
-- Uncomment line below to automatically send
-- send
end tell
end if
end tell
Best Answer
To use this (or any) script in a program like Apple Mail, you could create a service in Automator.
Launch the Automator program.
When it says Choose a type for your document, select Service and click Choose
In the top dialog, select Service receives no input in Mail.app (or, the name of your program, or any application if that's what you want).
You will insert one action: From the Utilities group, double-click Run AppleScript.
Select the text that says
and paste in the script you want to run. In your case, the script you want to paste is
Once you have done that, go to the File menu and click Save.
Give the service a name you will remember, like "Add Recipients to Group".
Then when you are in Mail, you can select a message or messages and go to the Mail menu in the menu bar, then the Services menu in the Mail menu, and select the service Add Recipients to Group.