linux - 让AppleScript发送电子邮件,然后通过shell脚本重新启动计算机?

标签 linux shell unix applescript

我正在努力将我的 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/

相关文章:

python - 随机 : not enough state (1 bytes); ignored

java - 如何通过 Java 在 Linux 上可靠地检测磁盘已关闭

c++ - _GNU_SOURCE 和 __USE_GNU

xml - 使用 unix 终端解析 XML

unix - 我可以同时运行多个 npm 安装吗

regex - 创建一个正则表达式来匹配文件中 1 - 999 之间的数字

linux - 使用 bash,如何找到包含特定字符串的所有文件并将它们替换为现有文件?

java - 无法从 java : java. io.IOException : . 运行 ksh 脚本:未找到

linux - 如何识别终端中运行的是哪个 REPL?

java - 什么样的应用程序使用 getopt "return in order"行为?