2009-01-30 25 views

回答

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 
+0

好東東。需要注意的一點是,操作系統(至少在10.7.4)會在最後一次應用程序退出時自動激活Finder - 顯示非最小化窗口。如果你更喜歡桌面的清晰視圖,你可以最小化所有的Finder窗口,`告訴應用程序「系統事件」點擊(每個窗口中的第一個按鈕「Finder」,其角色描述是「最小化按鈕」), /或關閉它們全部,`告訴應用程序「Finder」關閉每個窗口「。如果關閉它們,則先將它們最小化,以防止閃爍。 – mklement0 2012-05-14 14:12:49

+0

雖然使用流程類的`name`屬性_usually_有效,但有些應用程序的值與UI中顯示的應用程序名稱不同,並且可以通過`tell application`語句來理解。因此,最好使用「顯示名稱」屬性(儘管可能仍然存在即使這樣也不起作用的情況)。因此,上面的相關行應該是`將process_list設置爲顯示爲真的每個進程的顯示名稱。 – mklement0 2012-05-26 22:13:23

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 
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 
0

一些google搜索後,我發現了一個更好的方法:

  • 它使用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