Skip to content

Instantly share code, notes, and snippets.

@OKsign
Created August 25, 2020 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OKsign/82aaa7fb8d3e3e53eb0c439c2ea4087a to your computer and use it in GitHub Desktop.
Save OKsign/82aaa7fb8d3e3e53eb0c439c2ea4087a to your computer and use it in GitHub Desktop.
-- s23.4 - fixed: closing windows of Finder on Mac Catalina, hang caused by some apps | improved: killing apps listed in KillWindows_User
-- s23.3 - supported: Audacity
-- s23.2 - supported: wine-preloader, wine
-- user options
set KillWindows_User to {"app1", "app2"}
-- end of user options
set input_list_choose to {"Kill and Shut Down", "Kill and Restart", "Kill All Windows"}
set user_input to (choose from list input_list_choose with prompt "" with title "Script 23.4")
if user_input is not false then
delay 0.5
try
if (count KillWindows_User) is not 0 then
repeat with i from 1 to (count KillWindows_User)
try
set the_pid to (do shell script "ps -A | grep -m1 " & (quoted form of (item i of KillWindows_User)) & " | awk '{print $1}'")
delay 0.3
if the_pid is not "" then
try
do shell script ("kill -9 " & the_pid)
end try
end if
end try
end repeat
repeat with i from 1 to (count KillWindows_User)
try
set the_pid2 to (do shell script "ps ax | grep -m1 " & (quoted form of (item i of KillWindows_User)) & " | awk '{print $1}'")
delay 0.3
if the_pid2 is not "" then
try
do shell script ("kill -9 " & the_pid2)
end try
end if
end try
end repeat
end if
end try
-- s23.4 - 2020-08-23_21.46.24
repeat 2 times
tell application "System Events"
tell application "Finder"
set windowCount to count of windows
end tell
end tell
if windowCount > 0 then
tell application "System Events"
tell process "Finder"
set frontmost to true
delay 0.3
end tell
end tell
repeat windowCount times
tell application "System Events"
keystroke "w" using {command down, option down}
end tell
end repeat
end if
delay 1
end repeat
-- end - s23.4 - 2020-08-23_21.46.24
try
tell application "System Events"
set foregroundapps to get the name of every process whose background only is false
delay 0.3
end tell
-- kill windows of special apps
-- added - s 23.3 --2019-12-10_03.38.45
if "Audacity" is in foregroundapps then
tell application "System Events"
set activeApp to "Audacity"
set the_pid to (do shell script "ps ax | grep -m1 " & activeApp & " | awk '{print $1}'")
if the_pid is not "" then do shell script ("kill -9 " & the_pid)
delay 2
tell application "System Events"
set foregroundapps to get the name of every process whose background only is false
delay 0.3
end tell
if "Audacity" is in foregroundapps then
set the_pid to (do shell script "ps ax | grep -m1 " & activeApp & " | awk '{print $1}'")
if the_pid is not "" then do shell script ("kill -9 " & the_pid)
end if
end tell
end if
-- end - added - s 23.3 --2019-12-10_03.38.45
-- added - s23.2 - 2019-09-18_15.22.00
if "wine" is in foregroundapps then
tell application "System Events"
set activeApp to "wine"
set the_pid to (do shell script "ps -A | grep -m1 " & activeApp & " | awk '{print $1}'")
if the_pid is not "" then do shell script ("kill -9 " & the_pid)
end tell
end if
if "wine-preloader" is in foregroundapps then
tell application "System Events"
set activeApp to "wine-preloader"
set the_pid to (do shell script "ps -A | grep -m1 " & activeApp & " | awk '{print $1}'")
if the_pid is not "" then do shell script ("kill -9 " & the_pid)
end tell
end if
-- end - added - s23.2 - 2019-09-18_15.22.00
-- Atom
if "Atom" is in foregroundapps then
tell application "System Events"
tell process "Atom"
set frontmost to true
delay 0.8
tell application "System Events"
key code 53 -- cancle dialog
delay 0.8
keystroke "q" using command down
end tell
end tell
end tell
tell application "System Events"
set activeApp to "Atom"
set the_pid to (do shell script "ps -A | grep -m1 " & activeApp & " | awk '{print $1}'")
if the_pid is not "" then do shell script ("kill -9 " & the_pid)
end tell
end if
-- end - kill windows of special apps
set tar_app_list to {}
set ignored_list to {"Finder", "soffice", "Atom"}
if (count foregroundapps) is greater than 1 then
repeat with i from 1 to (count foregroundapps)
if (item i of foregroundapps) is not in ignored_list then
tell application "System Events"
tell process (item i of foregroundapps)
if menu bar item "Window" of menu bar 1 exists then
copy (item i of foregroundapps) to the end of tar_app_list
delay 0.2
end if
end tell
end tell
end if
end repeat
end if
set c_tar_app_list to count tar_app_list
if c_tar_app_list > 0 then
repeat with i from 1 to c_tar_app_list
tell application (item i of tar_app_list)
try
quit saving no
end try
delay 0.5
end tell
end repeat
end if
end try
delay 1
tell application "System Events"
set foregroundapps to get the name of every process whose background only is false
delay 0.5
end tell
if (count foregroundapps) is greater than 1 then
repeat with i from 1 to (count foregroundapps)
if (item i of foregroundapps) is not "Finder" then
try
set the_pid to (do shell script "ps -A | grep -m1 " & (quoted form of (item i of foregroundapps)) & " | awk '{print $1}'")
if the_pid is not "" then do shell script ("kill -9 " & the_pid)
end try
end if
end repeat
end if -- if (count foregroundapps) is greater than 1 then
tell application "System Events"
set foregroundapps to get the name of every process whose background only is false
delay 0.5
end tell
if (count foregroundapps) is greater than 1 then
repeat with i from 1 to (count foregroundapps)
if (item i of foregroundapps) is not "Finder" then
set tar_app to (item i of foregroundapps)
try
set the_pid2 to (do shell script "ps ax | grep -m1 " & (quoted form of (tar_app)) & " | awk '{print $1}'")
if the_pid2 is not "" then
do shell script ("kill -9 " & the_pid2)
end if
end try
end if
end repeat
end if
if (item 1 of user_input) is "Kill and Shut Down" then
tell application "Finder"
shut down
end tell
return
end if
if (item 1 of user_input) is "Kill and Restart" then
tell application "Finder"
restart
end tell
return
end if
if (item 1 of user_input) is "Kill All Windows" then
tell application "Finder"
display notification "Done!" with title "Kill All Windows without Saving."
end tell
end if
end if -- if user_input is not false then
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment