Whether or not an Open Mail Composition has an Attachment

I’ve been searching around for a resolution to this issue for a few weeks now without being able to figure out a good answer:

Given an open Compose Mail window in Mail.app (Sierra), is it possible to run a script to determine whether or not that mail has an Attachment?

In an ideal proof of concept:

  • Open Mail.app
  • File->New Message
  • Add an Attachment to the message
  • Run a script, and display an alert if the message has an attachment. Do nothing if this script is run and the message has no attachment.

Thanks for any help or final answer to this question!


Operating System: Mac OS X (10.10)

This is working for me:

-- Assumes the message is in front and relies on it being the most recent message with the same title in the Drafts folder.

tell application "Mail"
	set windowName to the name of the first window
	repeat with aMessage in the messages of drafts mailbox
		if the subject of aMessage is windowName then
			set theMessage to contents of aMessage
			exit repeat
		end if
	end repeat
	set foundAttachments to every mail attachment of theMessage
	if (count of foundAttachments) > 0 then
		set attachmentNames to ""
		repeat with anAttachment in foundAttachments
			set attachmentNames to attachmentNames & the name of anAttachment & return
		end repeat
		set dialogText to "The following files were attached to the currently open draft message \"" & windowName & "\":" & return & attachmentNames
		display dialog dialogText
	end if
end tell