[英]Let AppleScript 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. 我正在将MBP转换为媒体中心,如果计算机过热,我在其中设置的应用程序之一将运行脚本。 If this happened, it would trigger an AppleScript that would send me an email (telling me what happened) and then restart the computer.
如果发生这种情况,它将触发AppleScript,该脚本会向我发送电子邮件(告诉我发生了什么),然后重新启动计算机。
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. 另外,之所以使用Shell脚本重新启动计算机,是因为在重新启动时无法找到消除“是否要保存...”对话框的方法。
dive's answer didn't work for me as there is no way to match an outgoing messages "id" with a mailbox messages "id" or "message id", are there is no "date sent" property in an outgoing mesage. 跳水的答案对我不起作用,因为无法将传出消息“ id”与邮箱消息“ id”或“ message id”匹配,传出消息中没有“发送日期”属性。 However doing a count of the number of messages in the sent mailbox works:
但是,对发送的邮箱中的邮件数进行计数是可行的:
tell application "Mail"
set numOutgoingMessages to 1
set secondsToWait to 10
set preSentCount to count messages in sent mailbox
set secondsPassed to 0
repeat while secondsPassed is less than secondsToWait
set curSentCount to count messages in sent mailbox
if curSentCount is greater than or equal to preSentCount + numOutgoingMessages then
set secondsPassed to secondsToWait + 1
end if
if secondsPassed is less than secondsToWait then
delay 1
end if
set secondsPassed to secondsPassed + 1
end repeat
end tell
and surely this should not be in the linux section? 并且确定这不应该在linux部分中吗?
check in loop properties 'id' of your message in sent mailbox and reboot if it there or reboot if mail can't sent for 15 minutes: 在已发送邮箱中检查邮件的循环属性“ id”,如果在其中,则重新启动;如果在15分钟内无法发送邮件,则重新启动:
tell application "Mail"
set my_id to id of theNewMessage
set startDate to current date
set rebootDate to (current date) + 15 * minutes
rebootDate + 15 * minutes
-- wait 15 minutes for Mail.app, then reboot if message cannot be sent
repeat while startDate is not equal to rebootDate
set last_msg to first message of sent mailbox
set last_msg_id to id of last_msg
if last_msg_id is equal to my_id then
-- reboot
end if
delay 15
end repeat
end tell
note: I haven't tested this code much, please test it by yourself if you like. 注意:我尚未对该代码进行过多测试,如果您愿意,请自己进行测试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.