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

时间:2009-01-30 12:15:48

标签: macos applescript

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

4 个答案:

答案 0 :(得分:10)

没关系......我想我找到了答案:

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

答案 1 :(得分:1)

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 :(得分:1)

经过一些谷歌搜索,我发现了一个更好的方法:

  • 它使用background only来构建初始应用列表,而不是 visible is true。不同之处在于其他脚本将失败 退出使用⌘H隐藏的应用。
  • 它提供了排除 列表,以便,例如,您可以阻止您的脚本编辑器 每次测试脚本时都退出。

改编自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 :(得分:0)

    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