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

发布于 2024-12-28 04:51:34 字数 721 浏览 5 评论 0原文

我正在努力将我的 MBP 转换为媒体中心,如果计算机变得太热,我在其上设置的应用程序之一将运行一个脚本。如果发生这种情况,它会触发一个 AppleScript,它会向我发送一封电子邮件(告诉我发生了什么),然后重新启动计算机。

但问题是,重新启动不会等到邮件发送消息。我该如何补救?

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

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

渔村楼浪 2025-01-04 04:51:35

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

注意:我没有对这段代码进行太多测试,如果您愿意,请自行测试。

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:

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.

风尘浪孓 2025-01-04 04:51:35

潜水的答案对我不起作用,因为如果传出消息中没有“发送日期”属性,则无法将传出消息“id”与邮箱消息“id”或“消息 id”进行匹配。然而,对发送邮箱中的邮件数量进行计数是可行的:

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 部分中?

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. 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文