关闭mac的脚本
我正在尝试自动关闭我的 mac,我已经尝试了节能器中的计划关闭,我想睡觉,但这些似乎不起作用。 VLC 播放器运行似乎可以防止关闭。我想我需要一个脚本来强制关闭Mac,无论运行的各种程序可能会在屏幕上抛出什么错误。
谢谢
好的,
这是我用来关闭 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
I'm trying to automate the shutdown of my mac, I've tried the scheduled shutdown in energy saver and I wanna sleep but these don;t seem to work. VLC player runnign seems to prevent the shutdown. I think I need a script to forcefully shutdown the mac regardless of of what errors may thrown to screen by various programs running.
Thanks
Ok,
This is the applescript code im using to shutdown may mac. I've added it as an iCal event thats runs nightly.
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你可以尝试一个简单的苹果脚本,它是这样的......
看看它是否有效,然后你可以让它在某个时间通过 Automator 运行,等等。
Could you try a simple applescript, which goes something like this...
See if it works, and then you can make it run through Automator at certain time, etc.
我的解决方案(有点晚了)。只是一个包含 apple 的 bash 脚本:
我还编辑了 /etc/sudoers (和 /private/etc/sudoers)文件并添加了以下行:
ALL=NOPASSWD: /sbin/shutdown
始终为我工作,确保关机(敲敲;-))
my solution (somwhat late). Just a bash script with apple in it:
I also edited the /etc/sudoers (and /private/etc/sudoers) file(s) and added the line:
ALL=NOPASSWD: /sbin/shutdown
Always worked for me for an assured shutdown (knock knock ;-) )
应该这样做:
如果您想从钥匙串传递管理员密码,而不提示:
但不要在任何地方以明文形式存储管理员密码。而是使用钥匙串访问。
或者,您可以通过以下方式杀死所有用户进程:
但是,这也会杀死您自己的 Applescript 进程,从而阻止它随后请求关闭/重新启动。
无论哪种方式,当使用
sudo
或kill
时,你都是在玩火。This should do:
If you want to pass the admin password from key chain, with no prompt:
But do not store the admin password in clear anywhere. Instead use the keychain access.
Alternatively you could kill all user processes, via:
This however would also kill your own Applescript process, preventing it from requesting the shutdown/restart afterwards.
Either way you're playing with fire, when using
sudo
orkill
.做 Linux 用户做的事情。使用 bash 脚本。如果您不知道如何创建一个,请继续使用互联网搜索下载您找到的任何 bash 脚本,然后使用文本编辑应用程序打开它并粘贴以下内容:
(小心,如果很多人使用电脑,那么不建议使用这种方法,因为他们可以从这个脚本中获取您的用户登录密码)
它的工作方式与在Linux中的工作方式相同。终端会弹出一条消息并要求您输入一个号码。如果我们选择例如 50 ,那么 PC ( niresh ) 或 Mac 将在 50 分钟后关闭。
do what linux users do. use a bash script. if u dont know how to create one just go ahead and download ANY bash script u find using your internet search and open it with text edit app and paste the following:
( be careful if many people use the pc , then this method is not recommended, cause they can learn your user login password from inside this script )
it will work the same way it works in linux. the terminal will pop up a message and ask you to enter a number. if we choose for exaple 50 , then the pc ( niresh ) or mac will shutdown in 50 minutes.