Macos 是否使用Applescript退出所有应用程序?

Macos 是否使用Applescript退出所有应用程序?,macos,applescript,Macos,Applescript,如何使用Applescript退出所有正在运行的用户应用程序?没关系。。。我想我找到了答案: 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 v

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

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

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

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

  • 它只使用
    background
    构建初始应用程序列表,而不是
    可见为真
    。区别在于其他脚本将失败 退出已被隐藏的应用程序的步骤⌘H
  • 它提供了一个排除条款 例如,您可以阻止脚本编辑器 每次测试脚本时退出
改编自


好东西。需要注意的一点是,一旦最后一个应用程序退出,操作系统(至少在10.7.4上)会自动激活Finder,并显示非最小化窗口。如果您更喜欢桌面的干净视图,您可以最小化所有查找窗口,
告诉应用程序“系统事件”单击(进程“查找器”的每个窗口的第一个按钮,其角色描述为“最小化按钮”)
,和/或关闭所有窗口,
告诉应用程序“查找器”关闭每个窗口
。如果关闭它们,首先将其最小化可防止闪烁。虽然使用process类的
name
属性通常有效,但有些应用程序的该值与UI中显示的应用程序名称不同,并且可以通过
tell application
语句理解。因此,最好使用
显示名称
属性(尽管在某些情况下,即使这样也可能不起作用)。因此,上面的相关行应该是
将process\u list设置为每个可见的进程的显示名称
。欢迎使用Stackoverflow。您是否介意将您的答案扩展一点,以解释它是如何解决问题的?这将安全地关闭除finder之外的所有可见应用程序,以便不会关闭可能影响计算机后台进程的其他正在运行的进程。这非常有效!这应该是公认的答案。非常感谢!
    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
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
-- 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