Page 1 of 1

Send email messages

Posted: 2009-03-30 20:39:09
by Kino
With this macro, you can send email messages extracted from the frontmost document via Mail.app.

Before using this macro...

 1. In Mail.app, you have to add and configure mail accounts for From and/or Bcc addresses if you have not.

 2. Set $default{'From'}, $default{'Bcc'} and '$default{'Subject'} at the beginning of this macro.

 3. Create a document in the following format:

    From: MyName <myname@mac.com>
    To: Paul X <paulX@xxx.xxx.xx>
    Subject: Good News
    
    Hello Paul,
     blah blah blah blah blah blah blah blah blah blah
    blah blah blah blah blah blah blah blah blah blah blah
    blah blah blah blah
    ...
    --------------------- Page Break ---------------------
    To: Geoff Heartful <gheartful@xxx.xxx.xx>
    Bcc: MyName <myname@yahoo.co.uk>
    Subject: something witty
    
    Hiya Geoff!
     blah blah blah blah blah blah blah blah blah blah
    blah blah blah blah blah blah blah blah blah blah blah
    blah blah blah blah
    ...
    --------------------- Page Break ---------------------
    From: MyName <myname@gmail.com>
    To: Louise Dupont <luised@xxx.xxx.xx>
    Subject: Love...
    
    Dear Louise,
     blah blah blah blah blah blah blah blah blah blah
    blah blah blah blah blah blah blah blah blah blah blah
    blah blah blah blah
    ...

  or a document consisting of just a single message.

    From: MyName <myname@mac.com>
    To: Peter Pinton <pp@xxx.xxx.xx>
    Subject: re: Happy Birthday
    
    Peter,
     blah blah blah blah blah blah blah blah blah blah
    blah blah blah blah blah blah blah blah blah blah blah
    blah blah blah blah
    ...

You can omit From and Bcc (and even Subject) fields. $default{'From|Bcc|Subject'} will be used for the omitted field(s).

Instead of Page Break, you can use Section Break as message separator.

When you run the macro, a dialog box will appear and ask you to choose one of the actions:

    1 Open messages in Mail
    2 Open messages in Mail (force Bcc)
    3 Send messages by Mail
    4 Send messages by Mail (force Bcc)

In cases 1 & 2, messages will be opened in Mail.app without being sent.
In cases 1 & 3, messages not having Bcc field will not be sent to Bcc address.
In cases 2 & 4, messages not having Bcc field will be sent to $default{'Bcc'}.

LIMITATIONS:

- Headers other than From, To, Bcc and Subject are ignored.
- You cannot put multiple addresses in To or Bcc fields.
- You cannot send an attachment.
- You cannot send a rich text message.

This macro may/should contain bugs. Please try it with caution if you are interested in it.

Code: Select all

$default = Hash.new
$default{'From'} = 'MyName <myname@mac.com>'
$default{'Bcc'} = 'MyName "myname@yahoo.com.au"'
$default{'Subject'} = 'new message'

Require Pro Version 1.2
Debug.setDestination 'none'

$doc = Document.active
if $doc == undefined  # if there is no open document...
	exit
end

$OpenInMail = 'Open messages in Mail'
$BccAndOpen = 'Open messages in Mail (force Bcc)'
$SendByMail = 'Send messages by Mail'
$BccAndSend = 'Send messages by Mail (force Bcc)'

$sendMessage = $addBcc = false
$options = Array.new $OpenInMail, $BccAndOpen, $SendByMail, $BccAndSend
$input = Prompt Options 'Send messages...', 'Select one of the options', '', $options

$sendMessage = $addBcc = false
if $input == $BccAndOpen
	$addBcc = true
elsif $input == $SendByMail
	$sendMessage = true
elsif $input == $BccAndSend
	$sendMessage = $addBcc = true
end

$messages = Cast to String $doc.text.copy
$messages = $messages.split "\x0C"  # \x0C: Page/Section Break

$headers = Array.new 'To', 'Subject', 'From', 'Bcc'
# The expression below is based on <http://www.regular-expressions.info/email.html>
$findAddress1 = '[<\'\‘\“\"]*(?=[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b)'
$findAddress2 = '(?:(\S?(?:.*\S)?)[\t\x20\xA0\x{3000}]*)?(\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}\b)'

# The script below is based on "/Library/Scripts/Mail Scripts/Create New Message.scpt"
$scriptLatterHalf = "set newMessage to make new outgoing message with properties {subject:theSubject, content:theBody & return & return}\n"
$scriptLatterHalf &= "tell newMessage\nset visible to true\nset sender to theFrom\n"
$scriptLatterHalf &= "make new to recipient at end of to recipients with properties {name:theToName, address:theToAddress}\n"

$n = 1
$errmsg = ''

foreach $message in $messages
	$elements = Hash.new
	$found = $message.find '\A\s*((?:[A-Z-]+:.*\n)+)\s+(\S\p{Any}*)', 'Ei$'
	if $found != undefined
		$headerBlock = $1
		$elements{'body'} = $2
		$1 = $2 = undefined
		foreach $header in $headers
			$findHeaderValue = '(?<=' & $header
			$findHeaderValue &= ':)[\t\x20\xA0\x{3000}]*(\S.*\S)[\t\x20\xA0\x{3000}]*$'
			$headerBlock.find $findHeaderValue, 'Ei$'
			if $1 != undefined
				$elements.setValueForKey $1, $header
			end
			$1 = undefined
		end
		Debug.log $elements
		if $elements{'To'} != undefined
			$keys = $default.keys
			foreach $key in $keys
				if $elements{$key} == undefined
					if $key == 'Bcc'
						if $addBcc == true
							$elements{$key} = $default{$key}
						end
					else
						$elements{$key} = $default{$key}
					end
				end
			end
			$ToText = $elements{'To'}
			$elements.removeKey 'To'
			$ToText.findAndReplace $findAddress1, '\x20', 'Ei'
			$ToText.find $findAddress2, 'Ei$'
			$elements{'ToName'} = $1
			$elements{'ToAddress'} = $2
			$1 = $2 = undefined
			$BccText = $elements{'Bcc'}
			if $BccText != undefined
				$elements.removeKey 'Bcc'
				$BccText.findAndReplace $findAddress1, '\x20', 'Ei'
				$BccText.find $findAddress2, 'Ei$'
				$elements{'BccName'} = $1
				$elements{'BccAddress'} = $2
				$1 = $2 = undefined
			end
			$keys = $elements.keys
			Debug.log $elements
			foreach $key in $keys
				$value = $elements{$key}
				if $value != undefined
					$value.replaceAll '(?=["\x5C])', '\x5C', 'E'
					$elements{$key} = $value
				end
			end
			Debug.log $elements
			$ToName = $elements{'ToName'}
			$ToAddress = $elements{'ToAddress'}
			$Subject = $elements{'Subject'}
			$From = $elements{'From'}
			$body = $elements{'body'}
			# The script below is based on "/Library/Scripts/Mail Scripts/Create New Message.scpt"
			$script = "set theToName to \"$ToName\"\nset theToAddress to \"$ToAddress\"\n"
			if $elements{'BccAddress'} != undefined
				$BccName = $elements{'BccName'}
				$BccAddress = $elements{'BccAddress'}
				$script &= "set theBccName to \"$BccName\"\nset theBccAddress to \"$BccAddress\"\n"
			end
			$script &= "set theSubject to \"$Subject\"\nset theFrom to \"$From\"\n"
			$script &= "set theBody to \"$body\"\ntell application \"Mail\"\n"
			$script &= $scriptLatterHalf
			if $elements{'BccAddress'} != undefined
				$script &= "make new bcc recipient at end of bcc recipients with properties {name:theBccName, address:theBccAddress}\n"
			end
			if $sendMessage == true
				$script &= "send\n"
			end
			$script &= "end tell\nend tell\n"
			Debug.log $script
			$error = ''
			Run AppleScript $script, $error
			if $error
				$errmsg &= "Message no. $n could not be sent to Mail.app.\n"
			end
		else
			$errmsg &= "Message no. $n discarded due to the lack of recipient address.\n"
		end
	else
		$errmsg &= "Message no. $n discarded due to invalid message format.\n"
	end
	$n += 1
end

if $errmsg
	exit $errmsg
end
Formatted macro file:
http://www2.odn.ne.jp/alt-quinon/files/ ... es_nwm.zip