(由于过长而无法放入评论中,因此将其作为单独的答案发布)
感谢@MatthieuRiegler提供原始脚本。
这在10.12.6上有效,是对原始脚本的较小修改(在我自己调查之后,看到@CharlieGorichanaz的评论):
set textToSearchForInProcessName to "Not Responding"
-- Run Activity Monitor
tell application "Activity Monitor" to activate
tell application "System Events" to tell process "Activity Monitor"
-- Wait for the Activity Monitor window to open
repeat until (exists window 1)
delay 1
end repeat
--display notification "Window appeared"
-- Wait for the Menubar to be present
repeat until (exists menu 1 of menu bar item "View" of menu bar 1)
delay 1
end repeat
--display notification "Menubar appeared"
-- Make sure View -> My Processes is selected
click menu item "My Processes" of menu 1 of menu bar item "View" of menu bar 1
-- Click the 'CPU View' button ( **1 )
click radio button 1 of radio group 1 ¬
of group 2 of toolbar 1 ¬
of window 1
-- Working with the list of processes
tell outline 1 of scroll area 1 of window 1
-- Looking for Not responding process
set notResponding to rows whose value of ¬
first static text contains textToSearchForInProcessName
repeat with aProcess in notResponding
-- For each non responding process retrieve the PID
set pid to value of text field 1 of aProcess -- ( **2 )
-- Kill that process using pid
if pid is not "" then do shell script ("kill -9 " & pid)
end repeat
end tell
end tell
** 1
在MACOS 10.12.x,工具栏包含一个额外的图标由于该组按钮(CPU,内存,能源等)都在 group 2 of toolbar 1
代替group 1 of toolbar 1
。如果没有该图标(我尚未在较早的macOS版本中确认),我相信CPU等按钮将位于group 1 of toolbar 1
** 2
如果您曾经将“活动”列中的PID列拖动到其他位置,则适用此规则。我将PID列拖到最左侧,因此在这一行上,我不得不将索引更改为1
:
set pid to value of text field 1 of aProcess
列从1的最左边开始编号。因此,如果需要,请在上一行中相应地调整突出显示的索引。