使用Automator对macOS硬关闭脚本进行故障排除

时间:2019-07-02 15:45:39

标签: macos shutdown automator macos-mojave

我正在尝试构建一个执行硬关闭的脚本。一键式等效,可通过触摸栏或键盘快捷键按住电源按钮。不保存任何内容,关闭电源。

tell application "System Events" to set the visible of every process to true   
set white_list to {"Finder", "Automator"}
try    
    tell application "Finder"    
        set process_list to the name of every process whose visible is true    
    end tell    
    repeat with theProcessName in process_list    
        tell application "System Events"    
            set theId to (unix id of every process whose name is theProcessName)    
            do shell script "kill -9 " & theId    
        end tell    
    end repeat    
    tell application "System Events" to shut down    
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

这是我到目前为止所得到的;几乎总是会导致错误。

在此先感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

脚本由于行

而失败
set theId to (unix id of every process whose name is theProcessName)

返回一个列表(多个项目)。替换为

set theId to unix id of process theProcessName

whose子句是多余的,因为您已经有了正确的进程名称。

注意:

出于调试目的,强烈建议打印错误

on error e   
    tell the current application to display dialog "The error '" & e & "' has occurred!" & return & "This script will now quit" buttons {"Quit"} default button 1 with icon 0    
end try