Quit All Applications using Applescript?

我只是一个虾纸丫 提交于 2019-12-17 18:53:21

问题


How would I quit all running user applications using Applescript?


回答1:


It's okay... I think I found my answer:

tell application "System Events" to set the visible of every process to true

set white_list to {"Finder"}

try
    tell application "Finder"
        set process_list to the name of every process whose visible is true
    end tell
    repeat with i from 1 to (number of items in process_list)
        set this_process to item i of the process_list
        if this_process is not in white_list then
            tell application this_process
                quit
            end tell
        end if
    end repeat
on error
    tell the current application to display dialog "An error has occurred!" & return & "This script will now quit" buttons {"Quit"} default button 1 with icon 0
end try



回答2:


After some googling, I found a better approach:

  • It uses background only to build the initial app list, rather than visible is true. The difference is that the other scripts will fail to quit an app that's been hidden with ⌘H.
  • It provides an exclusions list so that, for example, you can prevent your script editor from quitting each time you test the script.

Adapted from a thread on MacScripter.

-- get list of open apps
tell application "System Events"
  set allApps to displayed name of (every process whose background only is false) as list
end tell

-- leave some apps open 
set exclusions to {"AppleScript Editor", "Automator", "Finder", "LaunchBar"}

-- quit each app
repeat with thisApp in allApps
  set thisApp to thisApp as text
  if thisApp is not in exclusions then
    tell application thisApp to quit
  end if
end repeat



回答3:


tell application "System Events" to set the visible of every process to true

set white_list to {"Finder"}

try   
    tell application "Finder"   
        set process_list to the name of every process whose visible is true   
    end tell   
    repeat with i from 1 to (number of items in process_list)   
        set this_process to item i of the process_list   
        if this_process is not in white_list then   
            tell application this_process   
                quit   
            end tell   
        end if   
    end repeat   
on error   
    tell the current application to display dialog "An error has occurred!" & return & "This script will now quit" buttons {"Quit"} default button 1 with icon 0   
end try



回答4:


    tell application "System Events" to set quitapps to name of every application process whose visible is true and name is not "Finder"

repeat with closeall in quitapps

quit application closeall

end repeat


来源:https://stackoverflow.com/questions/495323/quit-all-applications-using-applescript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!