Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
-- user options
set KillWindows_User to {"app1", "app2"}
set KillWindows_with_window to {"BCompare", "app2"}
set display_dialog_if_magic_mouse_or_magic_keyboard_connected to "on" -- "on" or "off"
set keyboard to "Magic Keyboard" -- name of magic keybord: system preferences > bluetooth
set mouse to "Magic Mouse 2" -- name of magic mouse: system preferences > bluetooth
-- end of user options
-----------------------------------------------
-- s23.6 - supported: TextMate
-- s23.5 - supported: FreeFileSync
-- 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
-----------------------------------------------
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.9")
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
-- s 23.5
repeat with i from 1 to (count foregroundapps)
if (item i of foregroundapps) contains "FreeFileSync" 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
exit repeat
end if
end repeat
-- end - s 23.5
-- 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
if (count KillWindows_with_window) is not 0 then
repeat with i from 1 to (count KillWindows_with_window)
try
set the_pid to (do shell script "ps -A | grep -m1 " & (quoted form of (item i of KillWindows_with_window)) & " | 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_with_window)
try
set the_pid2 to (do shell script "ps ax | grep -m1 " & (quoted form of (item i of KillWindows_with_window)) & " | 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 - kill windows of special apps
tell application "System Events"
set foregroundapps to get the name of every process whose background only is false
delay 0.3
end tell
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
if (item i of foregroundapps) is not in KillWindows_with_window 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 if
end repeat
end if
set c_tar_app_list to count tar_app_list
set hang_quit_saving_no to {"TextMate"} -- 'quit saving no' > asking for saving dialog > killing process not work
if c_tar_app_list > 0 then
repeat with i from 1 to c_tar_app_list
if (item i of tar_app_list) is not in hang_quit_saving_no then
tell application (item i of tar_app_list)
try
quit saving no
end try
delay 0.5
end tell
end if
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
if display_dialog_if_magic_mouse_or_magic_keyboard_connected is "on" then
set aa to do shell script "system_profiler SPBluetoothDataType | grep '" & keyboard & "\\|Connected'"
set apple_std to text item delimiters
set text item delimiters to {keyboard, ":"}
set aa_list to aa's text items
repeat with i from 1 to count aa_list
if (item i of aa_list) contains "Services" then
set n1 to i
exit repeat
end if
end repeat
repeat with j from 1 to count aa_list
if j > n1 or j = n1 then
if (item j of aa_list) contains "Connected" then
set n2 to j
exit repeat
end if
end if
end repeat
repeat with y from 1 to count aa_list
if y > n2 or y = n2 then
if (item y of aa_list) contains "Yes" or (item y of aa_list) contains "No" then
if (item y of aa_list) contains "Yes" then
set s_key to 1
else
set s_key to 0
end if
exit repeat
end if
end if
end repeat
set text item delimiters to apple_std
set aa to do shell script "system_profiler SPBluetoothDataType | grep '" & mouse & "\\|Connected'"
set apple_std to text item delimiters
set text item delimiters to {mouse, ":"}
set aa_list to aa's text items
--return item 4 of aa_list
repeat with i from 1 to count aa_list
if (item i of aa_list) contains "Services" then
set n1 to i
exit repeat
end if
end repeat
repeat with j from 1 to count aa_list
if j > n1 or j = n1 then
if (item j of aa_list) contains "Connected" then
set n2 to j
exit repeat
end if
end if
end repeat
repeat with y from 1 to count aa_list
if y > n2 or y = n2 then
if (item y of aa_list) contains "Yes" or (item y of aa_list) contains "No" then
if (item y of aa_list) contains "Yes" then
set s_mouse to 1
else
set s_mouse to 0
end if
exit repeat
end if
end if
end repeat
set text item delimiters to apple_std
if s_key is 1 and s_mouse is 1 then
display dialog keyboard & " and " & mouse & " are Still Connected!" with title "script 23.9"
end if
if s_key is 1 and s_mouse is 0 then
display dialog keyboard & " is Still Connected!" with title "script 23.9"
end if
if s_key is 0 and s_mouse is 1 then
display dialog mouse & " is Still Connected!" with title "script 23.9"
end if
end if
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