AppleScript: Close all open Windows

I'm trying to write an apple script to close all open Windows. The following is what I tried:

tell application "System Events"
  repeat with theProcess in (application processes where visible is true)
      tell application theProcess
          close
      end tell
   end repeat
end tell

This does not seem to work. I get:

Script Error: System Events got an error: Can't get application (item 1 of every application process whose visible = true). number -1728

I don't care if applications quit or if just the windows close.

I tried to debug this, but I could not get this script opened in Xcode.


Edit: Thanks to user3439894 I've settled on the following script which just sends Command + Q to each of the visible applications:

tell application "System Events"
    set theVisibleApps to (name of application processes where visible is true)
end tell



repeat with thisApp in theVisibleApps
    try
        tell application thisApp to activate
        tell application "System Events"
            keystroke "q" using command down
        end tell
    on error errMsg
        display dialog errMsg
    end try
end repeat

This works for me for now. As user3439894 suggested, I need to go through and understand the AppleScript Language Guide


The first issue is in what (application processes where visible is true) returns.

As an example, on a clean install of macOS 10.13 with just Finder and Script Editor open:

tell application "System Events" to get application processes where visible is true

Returns:

{application process "Script Editor" of application "System Events", application process "Finder" of application "System Events"}

What you really want is to get a list of names of applications that are visible, e.g.:

tell application "System Events" to get name of every application process where visible is true

Returns:

{"Script Editor", "Finder"}

The following example AppleScript code will try to close all open documents of each application and on error close all windows.

Now I opened some documents in TextEdit and Preview and some windows in Finder then ran the example AppleScript code. It closed all open documents in TextEdit and Preview and all windows in Finder, but not Script Editor.

Note: This will not close and Script Editor documents and silently errors out with:

error "The document can’t be closed while the script is running."

Example AppleScript code:

tell application "System Events"
    set theVisibleApps to (name of application processes where visible is true)
end tell

repeat with thisApp in theVisibleApps
    try
        tell application thisApp
            try
                close every document without saving
            on error
                close every window
            end try
        end tell
    end try
end repeat

Also note that with this example AppleScript code as coded will not close every document or close every window of an application that does not support those commands and should fail silently because of the try command.

This example AppleScript code is being presented to illustrate what's wrong with your present code and an example of how to help achieve your goal.