关闭mac的脚本

时间:2011-04-06 11:35:17

标签: macos osx-snow-leopard applescript

我正在尝试自动关闭我的mac,我已经尝试了节能的预定关机,我想要睡觉,但这些似乎不起作用。 VLC播放器runnign似乎阻止了关机。我想我需要一个强制关闭mac的脚本,而不管运行的各个程序可能会在屏幕上抛出什么错误。

由于

确定,

这是我用来关闭may的mac的代码。我已将它添加为每晚运行的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

4 个答案:

答案 0 :(得分:6)

你能试试一个简单的AppleScript,就像这样...

tell application "System Events"
shut down
end tell

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

答案 1 :(得分:3)

我的解决方案(迟到了)。只是一个包含苹果的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

始终为我工作以确保关机(敲敲;-))

答案 2 :(得分:1)

这应该做:

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时,无论哪种方式都在玩火。

答案 3 :(得分:0)

做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分钟内关闭。

相关问题