MacOS – List all programs without any window and kill them

applescriptmacos

has anyone an idea for an AppleScript that checks the number of windows of all applications and kills programs with window count 0?

Thanks a lot!

Best Answer

Interesting problem.
Here's a snippet to help you start listing windows of apps:

tell application "System Events"
    set appProcs to every application process whose background only is false
    set appWins to ""
    repeat with eachProc in appProcs
        set appWinNum to title of every window of eachProc
-- Put "quit..." command into "if...then" :
        if (count of appWinNum) is equal to 0 then display ¬
                alert "No window found for app: \r\r" & name of eachProc
        set appWins to appWins & return & name of eachProc & ¬
                ":\n" & appWinNum & return
    end repeat
    display alert appWins
end tell

I didn't bother to set line feeds for multiple windows -- as there is a TREMENOUS drawback to this approach:

This script does not and will not find windows on each and every "space" set in Mission Control.
It's a pity, but as yet Mission Control is un-scriptable.

Your best -if ugly- solution would be to have each app put frontmost, then checked, then quit ...