(Posting this as a separate answer since too long to fit in a comment)
Credit to @MatthieuRiegler for the original script.
This worked on 10.12.6 and is a minor modification of the original script (saw @CharlieGorichanaz's comment after I'd done my own investigation):
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)
--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)
--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)
In macOS 10.12.x, the toolbar contains an additional icon due to which the set of buttons (CPU, Memory, Energy, etc) are in
group 2 of toolbar 1 instead of
group 1 of toolbar 1. In absence of that icon (I haven't confirmed in older macOS versions), I believe the CPU etc buttons would be in
group 1 of toolbar 1
This applies if you've ever dragged the PID column in Activity column to a different position. I'd dragged the PID column to the leftmost position so on this line, I had to change the index to
set pid to value of text field 1 of aProcess
The columns are numbered from the leftmost, starting at 1. So adjust the highlighted index in the above line accordingly if needed.