【问题标题】:Troubleshooting a macOS hard shutdown script with automator使用自动机对 macOS 硬关机脚本进行故障排除
【发布时间】:2019-11-13 07:28:50
【问题描述】:

我正在尝试构建一个执行硬关机的脚本。相当于 1 次点击,通过触摸栏或键盘快捷键按住电源按钮。什么都不保存,关机。

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

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

提前感谢您的帮助。

【问题讨论】:

    标签: macos shutdown automator macos-mojave


    【解决方案1】:

    脚本失败,因为该行

    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
    

    【讨论】:

    • 非常感谢,成功了! -- 它与键盘快捷键一起使用,使用触摸栏它抱怨苹果脚本由于某种原因改变了系统事件;但键盘足够好。
    猜你喜欢
    • 1970-01-01
    • 2023-04-07
    • 1970-01-01
    • 2013-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-20
    • 1970-01-01
    相关资源
    最近更新 更多