Skip to content

Instantly share code, notes, and snippets.

@OKsign
Created January 4, 2022 13:24
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/c22756c78b7069b549156d6c821a4cbf to your computer and use it in GitHub Desktop.
Save OKsign/c22756c78b7069b549156d6c821a4cbf to your computer and use it in GitHub Desktop.
-- user options
set KillWindows_User to {"app1", "app2"}
set KillWindows_with_window to {"BCompare", "app2"}
set output_volume_before_shutdown_and_restart to "off" -- "on" or "off"
set output_volume to 0 -- you can set from 0 - 100
-- 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.12")
if user_input is not false then
-- start - kill list_tested_quit_saving_no_apps
set list_tested_quit_saving_no_apps to {"TextEdit", "Script Editor", "Preview"}
repeat with i from 1 to (count list_tested_quit_saving_no_apps)
if application (item i of list_tested_quit_saving_no_apps) is running then
tell application (item i of list_tested_quit_saving_no_apps)
quit saving no
delay 0.2
end tell
end if
end repeat
-- end - kill list_tested_quit_saving_no_apps
-- start - kill special apps - kill process psa,psax
set list_kill_process_psa to {"FreeFileSync", "wine", "wine-preloader", "Atom", "TextMate", "BCompare"}
repeat with i from 1 to (count list_kill_process_psa)
try
set the_pid to (do shell script "ps -A | grep -m1 " & (quoted form of (item i of list_kill_process_psa)) & " | awk '{print $1}'")
if the_pid is not "" then do shell script ("kill -9 " & the_pid)
end try
end repeat
set list_kill_process_psax to {"Audacity"} -- for app that is running only, if not, error
try
repeat with i from 1 to (count list_kill_process_psax)
if application (item i of list_kill_process_psax) is running then
set the_pid to (do shell script "ps ax | grep -m1 " & (item i of list_kill_process_psax) & " | awk '{print $1}'")
if the_pid is not "" then do shell script ("kill -9 " & the_pid)
end if
end repeat
end try
-- end - kill special apps - kill process psa,psax
-- start - get app for quit saving no
tell application "System Events"
set frontapps to get the name of every process whose background only is false
delay 0.3
end tell
set cfrontapps to count frontapps
set list_quit_saving_no_apps to {}
if cfrontapps is not 0 then
repeat with i from 1 to cfrontapps
tell application "System Events"
tell process (item i of frontapps)
if menu item "Save" of menu 1 of menu bar item "File" of menu bar 1 exists then
copy (item i of frontapps) to the end of list_quit_saving_no_apps
delay 0.2
end if
if menu item "Save..." of menu 1 of menu bar item "File" of menu bar 1 exists then
if (item i of frontapps) is not in list_quit_saving_no_apps then
copy (item i of frontapps) to the end of list_quit_saving_no_apps
end if
delay 0.2
end if
if menu item "Save File" of menu 1 of menu bar item "File" of menu bar 1 exists then
if (item i of frontapps) is not in list_quit_saving_no_apps then
copy (item i of frontapps) to the end of list_quit_saving_no_apps
end if
delay 0.2
end if
if menu item "Save File As..." of menu 1 of menu bar item "File" of menu bar 1 exists then
if (item i of frontapps) is not in list_quit_saving_no_apps then
copy (item i of frontapps) to the end of list_quit_saving_no_apps
end if
delay 0.2
end if
end tell
end tell
end repeat
end if
-- end - get app for quit saving no
-- start - quit list KillWindows_User
if (count KillWindows_User) is not 0 then
repeat with i from 1 to (count KillWindows_User)
if (item i of KillWindows_User) is not in list_kill_process_psa then
if (item i of KillWindows_User) is not in list_kill_process_psax then
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 do shell script ("kill -9 " & the_pid)
end try
end if
end if
end repeat
end if
-- end - start quit list KillWindows_User
-- start - KillWindows_with_window
if (count KillWindows_with_window) is not 0 then
repeat with i from 1 to (count KillWindows_with_window)
if (item i of KillWindows_with_window) is not in list_kill_process_psa then
if (item i of KillWindows_with_window) is not in list_kill_process_psax then
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 do shell script ("kill -9 " & the_pid)
end try
end if
end if
end repeat
end if
-- end - KillWindows_with_window
-- start - kill finder windows
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 - kill finder windows
-- start - quit saving no
set ignored_list to {"Finder", "soffice"}
set list_quit_saving_no_apps_2 to {}
try
if (count list_quit_saving_no_apps) is greater than 0 then
repeat with i from 1 to (count list_quit_saving_no_apps)
if (item i of list_quit_saving_no_apps) is not in ignored_list then
copy (item i of list_quit_saving_no_apps) to the end of list_quit_saving_no_apps_2
delay 0.4
end if
end repeat
end if
if (count list_quit_saving_no_apps_2) is greater than 0 then
repeat with i from 1 to (count list_quit_saving_no_apps_2)
tell application (item i of list_quit_saving_no_apps_2)
quit saving no
delay 0.5
end tell
end repeat
end if
end try
-- end - quit saving no
delay 3
-- start - kill - final_check_front_apps
repeat 2 times
tell application "System Events"
set final_check_front_apps to get the name of every process whose background only is false
delay 0.5
end tell
if (count final_check_front_apps) is greater than 1 then -- finder is ignored
repeat with i from 1 to (count final_check_front_apps)
if (item i of final_check_front_apps) is not "Finder" then
try
set the_pid to (do shell script "ps -A | grep -m1 " & (quoted form of (item i of final_check_front_apps)) & " | awk '{print $1}'")
if the_pid is not "" then do shell script ("kill -9 " & the_pid)
end try
end if
end repeat
end if
delay 2
end repeat
-- end - kill - final_check_front_apps
if (item 1 of user_input) is "Kill and Shut Down" then
if output_volume_before_shutdown_and_restart is "on" then
set volume output volume output_volume
delay 0.5
end if
delay 0.5
tell application "Finder"
shut down
end tell
return
end if
if (item 1 of user_input) is "Kill and Restart" then
if output_volume_before_shutdown_and_restart is "on" then
set volume output volume output_volume
delay 0.5
end if
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 -- user choose something in kill and shut, kill and re, kill only
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment