macos 使用 Applescript 退出所有应用程序?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/495323/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 06:01:38  来源:igfitidea点击:

Quit All Applications using Applescript?

macosapplescript

提问by Brock Woolf

How would I quit all running user applications using Applescript?

我将如何使用 Applescript 退出所有正在运行的用户应用程序?

采纳答案by Brock Woolf

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

回答by macscripter

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

回答by clozach

After some googling, I found a better approach:

经过一番谷歌搜索,我找到了一个更好的方法:

  • It uses background onlyto 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.
  • 它用于background only构建初始应用程序列表,而不是 visible is true. 不同之处在于其他脚本将无法退出被 ?H 隐藏的应用程序。
  • 它提供了一个排除列表,例如,您可以防止脚本编辑器在每次测试脚本时退出。

Adapted from a thread on MacScripter.

改编自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

回答by Jeff

    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