Created
August 6, 2023 07:30
-
-
Save OKsign/b8032b79412298ce1df66e0fc085bf49 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- user options | |
set KillWindows_User to {"app1", "app2"} | |
set KillWindows_with_window to {"BCompare", "gimp", "soffice", "sublime_text"} | |
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 | |
-- option 'KillWindows_User' is desinged for killing apps that sit in the menubar. If the name of app is not correct (case sensitive), that app won't be killed. To check a name of menubar app, use this code -- tell application "System Events" to set frontapps to get the name of every process whose background only is true -- copy from 'tell' to 'true' then paste in Script Editor, click menu 'Script', click 'Run', check the name in tab 'Result' at the bottom. | |
-- option 'KillWindows_with_window' is for the situation when script is interrupted by dialog "Choose Application" with message "Where is (app name)?", put the app name in the list "KillWindows_with_window" | |
----------------------------------------------- | |
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.15") | |
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 | |
-- fixed for 23.14 - wrong code | |
tell application "System Events" | |
set frontapps to get the name of every process whose background only is false | |
delay 0.3 | |
end tell | |
set list_kill_process_psax to {"Audacity"} -- apps that can be killed with this code | |
try | |
repeat with i from 1 to (count list_kill_process_psax) | |
if (item i of list_kill_process_psax) is in frontapps 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 - fixed for 23.14 - wrong code | |
-- end - kill special apps - kill process psa,psax | |
-- 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 - 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 - 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 ignored_list to {"Finder", "Inkscape"} -- finder and others that are not quit with command 'quit saving no' - behavior: display dialog "ask for saving" | |
set list_quit_saving_no_apps to {} | |
if cfrontapps is not 0 then | |
repeat with i from 1 to cfrontapps | |
if (item i of frontapps) is not in ignored_list then | |
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 | |
else | |
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 | |
else | |
if menu item "Save File" 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 | |
else | |
if menu item "Save File As..." 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 | |
end if | |
end if | |
end if | |
end tell | |
end tell | |
end if -- (item i of frontapps) is not in ignored_list then | |
end repeat | |
end if | |
-- end - get app for quit saving no | |
-- start - quit - list saving no apps | |
if (count list_quit_saving_no_apps) is greater than 0 then | |
repeat with i from 1 to (count list_quit_saving_no_apps) | |
tell application (item i of list_quit_saving_no_apps) | |
try | |
quit saving no | |
delay 0.5 | |
end try | |
end tell | |
end repeat | |
end if | |
-- end - quit - list saving no apps | |
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 | |
-- 2023-08-06_08.07.23 | |
-- start - kill with quit command 'cause apple calendar app not killed | |
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 not 0 then | |
repeat with i from 1 to (count final_check_front_apps) | |
tell application (item i of final_check_front_apps) | |
quit | |
delay 0.7 | |
end tell | |
end repeat | |
end if | |
-- end - kill with quit command 'cause apple calendar app not killed | |
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