2009-01-30 5 views

Trả lời

10

Không sao đâu ... Tôi nghĩ rằng tôi đã tìm thấy câu trả lời của tôi:

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

Công cụ tuyệt vời. Một điều cần lưu ý là hệ điều hành (ít nhất là trên 10.7.4) tự động kích hoạt Finder - với các cửa sổ không thu nhỏ hiển thị - khi ứng dụng cuối cùng thoát. Thay vào đó, nếu bạn thích xem sạch màn hình của mình, bạn có thể thu nhỏ tất cả cửa sổ Trình tìm kiếm, "báo cho ứng dụng" Sự kiện hệ thống "để nhấp (nút đầu tiên của mỗi cửa sổ của quá trình" Trình tìm kiếm "có mô tả vai trò là" nút thu nhỏ ")' và/hoặc đóng tất cả, 'tell ứng dụng" Finder "để đóng mỗi cửa sổ'. Nếu bạn đóng chúng lại, việc giảm thiểu chúng trước tiên sẽ ngăn ngừa nhấp nháy. – mklement0

+0

Trong khi sử dụng thuộc tính 'name' của lớp xử lý _usually_ hoạt động, có các ứng dụng mà giá trị đó khác với tên của ứng dụng như được hiển thị trong giao diện người dùng và được hiểu bởi câu lệnh' tell application'. Do đó, tốt hơn nên sử dụng thuộc tính 'display name' (mặc dù vẫn có thể có trường hợp thậm chí không hoạt động). Do đó, dòng có liên quan ở trên phải là 'set process_list thành tên hiển thị của mọi tiến trình có thể nhìn thấy là true'. – mklement0

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

Chào mừng bạn đến với Stackoverflow. Bạn có muốn mở rộng câu trả lời của mình một chút để giải thích cách giải quyết vấn đề không? – Daenarys

+0

điều này đóng tất cả các ứng dụng hiển thị, ngoại trừ công cụ tìm, an toàn để nó không đóng các tiến trình đang chạy khác có thể ảnh hưởng đến các tiến trình nền của máy tính. – Jeff

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

Sau khi một số googling, tôi tìm thấy một cách tiếp cận tốt hơn:

  • Nó sử dụng để xây dựng background only danh sách ứng dụng ban đầu, thay vì visible is true. Sự khác biệt là các tập lệnh khác sẽ không thành công để thoát khỏi ứng dụng bị ẩn với ⌘H.
  • Danh sách này cung cấp một danh sách loại trừ để, ví dụ: bạn có thể ngăn trình chỉnh sửa tập lệnh của mình không bị bỏ mỗi khi bạn kiểm tra tập lệnh.

Được điều chỉnh từ 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