Let script send email, then restart computer via shell script?

I’m working on converting my MBP into a media center, and one of the apps I have set up on it will run a script if the computer gets too hot. If this happened, it would trigger an AppleScript that would send me an email (telling me what happened) and then restart the computer.

The problem however is that the restart won’t wait until Mail has sent the message. How can I remedy this?

tell application "Mail"
	set theNewMessage to make new outgoing message with properties {subject:"Media Center Alert", content:"Media Center has encountered a problem. It is now restarting. ", visible:false}
	tell theNewMessage
		make new to recipient at end of to recipients with properties {address:"myemail"}
		send
	end tell
end tell
tell application "Finder"
	do shell script "reboot now" password "mypass" with administrator privileges
end tell

Also, the reason why I am using a shell script to restart the computer is because I couldn’t find a way to dismiss the “Do you want to save…” dialogs while using just restart.

No ideas?

Well pxlcreations, you could put a delay in the script between the mail tell block, and the finder tell block.

Like this.


tell application "Mail"
   set theNewMessage to make new outgoing message with properties {subject:"Media Center Alert", content:"Media Center has encountered a problem. It is now restarting. ", visible:false}
   tell theNewMessage
       make new to recipient at end of to recipients with properties {address:"myemail"}
       send
   end tell
end tell

delay(3) --sets a 3 second delay

tell application "Finder"
   do shell script "reboot now" password "mypass" with administrator privileges
end tell

You may have to experiment with different delay times, so that the Mail app has enough time to send the message,
befor the restart command is sent.

Alternativly, you could shut down the Mail app after the message has been sent, and then use System Events to check
that the Mail app has actually quit, before continuing to restart the system, as System Events has commands to shut
down or restart the system, instead of using a shell script.

Hope this helps.

Regards Mark