【问题标题】:Script to shutdown mac关闭mac的脚本
【发布时间】:2011-07-30 18:07:49
【问题描述】:

我正在尝试自动关闭我的 mac,我已尝试在节能器中按计划关机,我想睡觉,但这些似乎不起作用。 VLC player runnign 似乎可以防止关机。我想我需要一个脚本来强制关闭 mac,不管运行的各种程序可能会抛出什么错误。

谢谢

好的,

这是我用来关闭may mac的applescript代码。我已将其添加为每晚运行的 iCal 活动。

tell application "System Events" to set the visible of every process to true

set white_list to {"Finder"}

try
    tell application "Finder"
        set process_list to the name of every process whose visible is true
    end tell
    repeat with i from 1 to (number of items in process_list)
        set this_process to item i of the process_list
        if this_process is not in white_list then
            do shell script "killall \"" & this_process & "\""
        end if
    end repeat
on error
    tell the current application to display dialog "An error has occurred!" & return & "This script will now quit" buttons {"Quit"} default button 1 with icon 0
end try



tell application "System Events"
    shut down
end tell

【问题讨论】:

标签: macos osx-snow-leopard applescript


【解决方案1】:

做 linux 用户做的事。使用 bash 脚本。如果您不知道如何创建一个,请继续下载您使用互联网搜索找到的任何 bash 脚本,然后使用文本编辑应用程序打开它并粘贴以下内容:
(如果很多人使用电脑,请小心,那么不推荐这种方法,因为他们可以从这个脚本中得知你的用户登录密码)

#!/bin/bash

echo -n "Enter a number > "
read x
echo [your password] | sudo -S shutdown -h +$x

它的工作方式与在 linux 中的工作方式相同。终端将弹出一条消息并要求您输入一个数字。如果我们选择 exaple 50 ,那么 pc ( niresh ) 或 mac 将在 50 分钟内关闭。

【讨论】:

    【解决方案2】:

    我的解决方案(有点晚了)。只是一个带有苹果的 bash 脚本:

    
    #!/bin/bash
    # OK, just shutdown all ... applications after n minutes
    sudo shutdown -h +2 &
    # Try normal shutdown in the meantime
    osascript -e 'tell application "System Events" to shut down'
    

    我还编辑了 /etc/sudoers(和 /private/etc/sudoers)文件并添加了以下行: ALL=NOPASSWD: /sbin/shutdown

    一直为我工作以确保关机(敲敲;-))

    【讨论】:

      【解决方案3】:

      应该这样做:

      do shell script "shutdown" with administrator privileges
      

      如果你想通过钥匙串传递管理员密码,没有提示:

      do shell script "shutdown" with administrator privileges password "password here"
      

      但不要在任何地方明文存储管理员密码。而是使用钥匙串访问。


      您也可以通过以下方式杀死所有用户进程:

      do shell script "kill -9 -1"
      

      然而,这也会杀死您自己的 Applescript 进程,阻止它在之后请求关闭/重新启动。

      使用sudokill 时,无论哪种方式,您都是在玩火。

      【讨论】:

      • sudo reboot - 这不会重新启动 mac 吗?
      • 已修复。使用“sudo halt”进行睡眠,使用“sudo restart”进行重启或使用“sudo shutdown”进行关机。
      • ...您不需要具有“管理员权限”的 sudo,您已经是 root。还有reboot 用于重启。 halt是不那么小心的shutdown,不是sleep,就是pmset sleepnow
      • 感谢修复。我在 Applescript 世界的旅程是很久以前的事了。实际上,我对此感到有点高兴。 :P
      【解决方案4】:

      你能不能尝试一个简单的applescript,它是这样的......

      tell application "System Events"
      shut down
      end tell
      

      看看它是否有效,然后你可以让它在特定时间通过Automator运行等等。

      【讨论】:

      • 或者等等..是不是因为所有其他取消睡眠的程序而取消了这样的呼叫?
      • 是的,这就是问题所在。由于其他程序正在运行,呼叫似乎被取消,导致关机取消。以上都没有奏效。感谢您的 cmets。
      • 好的。您能否尝试查看在关闭之前运行此脚本是否有助于解决应用程序取消关闭的问题。stackoverflow.com/questions/495323/…
      猜你喜欢
      • 2011-09-20
      • 2016-05-24
      • 2012-11-12
      • 2015-02-12
      • 2021-10-05
      • 2010-12-12
      • 2016-09-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多