Interfacing with a mail client on Mac

Deprecated on Catalina and later — recent macOS versions have made scripting Mail (and Applescript generally) extremely unreliable, so MoneyWorks has transitioned to using the macOS Sharing APIs. This information may still apply if you are using other scriptable mail apps or earlier OS versions. Also note that New Outlook mode of Microsoft Outlook does not appear to support any scripting or automation at the time of writing. Don't use it.

MoneyWorks supports sending mail on Mac via Apple Mail or Microsoft Outlook automatically (or direct via an SMTP server). But you may want to customise the way mail is sent.

In MoneyWorks 7.1.6 there is an additional (simpler) way to provide custom glue for interfacing with the system mail client on Mac. If there exists a unix executable in the standard plugins Scripts folder named mail_client_helper, then that executable will be used to send emails. It will be passed 4 parameters: the path to a file to be attached, a recipient (or comma-delimited list of recipients), a subject, and a message (currently blank).

Here is an example implementation of mail_client_helper that sends to Apple Mail (and works around the longstanding Mail bug wherein the signature is deleted when an attachment is added).

#!/usr/bin/osascript
-- usage: mail_client_helper "attachmentpath"  "recip1,recip2" "subject" "message"
on run argv
	set attachment_path to item 1 of argv
	set reciplist to item 2 of argv
	set subjectvar to item 3 of argv
	tell application "Mail"
		set messagevar to (item 4 of argv) & return & return
		try
			set selsig to the name of the first signature -- 'the selected signature' does not work reliably: it can return an unusable UUID
			if the selected signature is not "None" then
				set messagevar to messagevar & (get the content of signature selsig) & return
			end if
		end try
		set AppleScript's text item delimiters to ","
		set delimitedList to every text item of reciplist
		set AppleScript's text item delimiters to ""
		set fileAttachThis to POSIX file attachment_path as alias
		set composeMessage to make new outgoing message with properties {visible:true, subject:subjectvar, content:messagevar}
		tell composeMessage
			repeat with thisr in delimitedList
				set this_addr to (do shell script "echo '" & thisr & "' | sed 's/.*< *//;s/ *>.*//;s/ //g'")
				make new to recipient at beginning of to recipients with properties {address:this_addr}
			end repeat
			make new attachment with properties {file name:fileAttachThis} at after the last paragraph
		end tell
		activate
	end tell
end run

Update: Here is (sort of) improvement that retains the formatting of styled signatures* using set message signature. The trick is that adding an attachment erases the signature, and adding a signature after adding the attachment also generally fails. However, it seems to work if you give the attachment adding process some time to complete before asking for the signature to be added...

*Further update. As of Sierra and High Sierra, bugs in Apple Mail prevent this from working. There is no known fix until Apple fixes the bugs.

#!/usr/bin/osascript
-- usage: mail_client_helper "attachmentpath"  "recip1,recip2" "subject" "message"
on run argv
	set attachment_path to item 1 of argv
	set reciplist to item 2 of argv
	set subjectvar to item 3 of argv
	tell application "Mail"
		set messagevar to (item 4 of argv) & return & return
		set AppleScript's text item delimiters to ","
		set delimitedList to every text item of reciplist
		set AppleScript's text item delimiters to ""
		set composeMessage to make new outgoing message with properties {visible:true, subject:subjectvar, content:messagevar}
		tell composeMessage
			repeat with thisr in delimitedList
				set this_addr to (do shell script "echo '" & thisr & "' | sed 's/.*< *//;s/ *>.*//;s/ //g'")
				make new to recipient at beginning of to recipients with properties {address:this_addr}
			end repeat
			if attachment_path is not ""
				set fileAttachThis to POSIX file attachment_path as alias
				make new attachment with properties {file name:fileAttachThis} at after the last paragraph
			end if
		end tell
		activate
		try
			set selsig to the name of the first signature -- 'the selected signature' does not work reliably: it can return an unusable UUID
			delay 2 -- wait a bit for the attachment to finish 'attaching'
			set message signature of composeMessage to signature selsig
		end try
	end tell
end run

How to use:

  1. Put the script into a plain text file at ~/Library/Application Support/Cognito/MoneyWorks Gold/MoneyWorks 7 Standard Plug-Ins/Scripts/mail_client_helper
  2. Make it executable:

    In Terminal, type

chmod a+x ~/Library/Application\ Support/Cognito/MoneyWorks\ Gold/MoneyWorks\ 7\ Standard\ Plug-Ins/Scripts/mail_client_helper
Posted in AppleScript | Comments Off on Interfacing with a mail client on Mac