How to tell Mail to quit but -after- the outgoing message is sent?

So the subject is my question. I have a script which is monitoring a folder and, when that folder has something new put into it, Mail is told to attach that file to an eMail and mail it to me, then quit.

The problem is that the “quit” command does so immediately and doesn’t wait until the message is successfully sent. I do see in the Mail Library this:

send‚v : Sends a message.
send outgoing message : the message to send
→ boolean : true if sending was successful, false if not

…but I don’t know what to put prior to the quit command to test for the successful sending. So I’m wondering if this might be the way:

send newmessage waiting until completion
quit mail

When I try this in Automator, I don’t see anything besides “Send Outgoing Messages” so there doesn’t seem to be anything to test for before I tell Mail to quit.

Any help is appreciated. Thanks!


Model: iMac Core i7
AppleScript: 2.1.2
Browser: Firefox 4.0.1
Operating System: Mac OS X (10.6)

Check it out dude! I made it for ya

to sendAlertEmail()
log “▷ sendAlertEmail”
set AppMailWasNotRunning to (application “Mail” is not running)
log "(application "Mail" is not running): " & (application “Mail” is not running) & “”
set timeoutt to 0
set timeouttt to 0
set whattodo to 123
tell application “Mail”

	--set mabo to the sent mailbox
	log "Khi cbi dùng lệnh send, (unread count of outbox) thời điểm hiện tại là: " & (unread count of outbox) & ""
	tell (make new outgoing message)
		set subject to "Alert" -- (the clipboard)
		set content to "Alert"
		make new to recipient at end of to recipients with properties {address:""} --YOU JUST NEED TO EDIT YOUR EMAIL here!!!! 
		log "Made new message sending"
	end tell
	log "(unread count of outbox) thời điểm hiện tại là: " & (unread count of outbox) & ""
		log "Khi vào repeat to, (unread count of outbox) thời điểm hiện tại là: " & (unread count of outbox) & ""
		set timeoutt to timeoutt + 1
		if timeoutt is 600 then exit repeat -- 600 s là out
		if (unread count of outbox) is not 0 then
			log "thấy unread count outbox change là đã có mail (vì số message trong outbox: " & (unread count of outbox) & ")"
				if timeouttt is 0 then
					log "delay 1.5 cho lần đầu trong repeat vì đằng nào send 1 email cũng mất tầm 2 s"
					delay 1.5 -- lần đầu cho delay 1 vì đằng nào send 1 email cũng mất gần 2 giây
				end if
				set timeouttt to timeouttt + 1
				if timeouttt is 600 then exit repeat
				if (unread count of outbox) is 0 then
					log "(unread count of outbox) is back to 0 (số Message trong Outbox là 0)"
					set whattodo to "exit repeat"
					log "'exit repeat'"
					exit repeat
				end if
				log "Chưa sent xong, vì hiện tại số message trong outbox là " & (unread count of outbox) & " ==> Mail vẫn đang cố send nó đi"
				delay 0.3
				log "delay 0.3 until it's back to 0"
			end repeat
		end if
		delay 0.1
		log "delay 0.1 until thấy số message trong outbox change, hiện tại là " & (unread count of outbox) & ""
		if whattodo is "exit repeat" then exit repeat
	end repeat
	if AppMailWasNotRunning is true then
		log "quit application Mail"
	end if
end tell

log " ⃞ sendAlertEmail Done"

end sendAlertEmail

contact me on facebook to ask more: only0nce