6

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

4

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.

  • Thank you @user3439894. If I display the error, I find {ApplicationName} got an error: every document does not understand the "close" message. Is there anyway to do something like click button 1 of window 1 or send each window a Ctrl + W or Ctrl + Q ? – O.O. Nov 24 '17 at 19:46
  • @O.O., As I stated in my answer, "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." and the code is just that, example code and not intended to be a complete solution to whatever it is you're trying to accomplish. The example AppleScript code certainly can be modified to accommodate additional conditions/scenarios, you just have to code it to accommodate your needs as appropriate. – user3439894 Nov 25 '17 at 18:49
  • Can you at least point me to some documentation or anything that can help me. I also did not understand why we needed the application names rather than application processes? Is there anyway to iterate over windows? i.e. for every application, for every window, close or send keycode. – O.O. Nov 27 '17 at 16:33
  • @O.O., Re: "Can you at least point me to some documentation ..." In Script Editor, click the Help menu and select Show AppleScript Language Guide. Re: "I also did not understand why we needed the application names rather than application processes?" Because your code errors out and is not the proper way to code it. In your code tell application theProcess became e.g. tell application item 1 of every application process whose visible = true which would translate to e.g. tell application application process "Script Editor" of application "System Events", and is an improper form of coding. – user3439894 Nov 27 '17 at 18:47
  • @O.O., You work with a list of application names so as to loop through the list and preform some action(s) on each app. Not every app is directly AppleScript scriptable and or responds to the same command and why in some cases you have to do a given process differently and or use UI Scripting too. If you want a brute force method, then in the repeat with thisApp in theVisibleApps loop, use do shell script "killall " & quoted form of thisApp That will, with force, close every open visible app based on the rest of the code. – user3439894 Nov 27 '17 at 18:48

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .