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

时间:2012-01-22 00:12:16

标签: 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脚本重新启动计算机的原因是因为我无法找到一种方法来在仅使用重新启动时关闭“是否要保存...”对话框。

2 个答案:

答案 0 :(得分:0)

检查已发送邮箱中邮件的循环属性“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

注意:我没有对此代码进行过多次测试,如果您愿意,请自行测试。

答案 1 :(得分:0)

潜水的回答对我不起作用,因为没有办法将外发消息“id”与邮箱消息“id”或“message id”匹配,是否在传出消息中没有“date sent”属性。但是,计算已发送邮箱中的邮件数量是有效的:

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

当然这不应该在linux部分吗?