我正在努力将我的 MBP 转换为媒体中心,如果计算机变得太热,我在其上设置的应用程序之一将运行一个脚本。如果发生这种情况,它会触发一个 AppleScript,它会向我发送一封电子邮件(告诉我发生了什么),然后重新启动计算机。
但问题是,重新启动不会等到 Mail 发送消息。我该如何解决这个问题?
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
此外,我使用 shell 脚本重新启动计算机的原因是因为我无法找到一种方法来在使用重新启动时关闭“是否要保存...”对话框。
最佳答案
检查已发送邮箱中邮件的循环属性“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
注意:我没有对这段代码进行太多测试,如果您愿意,请自行测试。
关于linux - 让AppleScript发送电子邮件,然后通过shell脚本重新启动计算机?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8957903/