0

I have created an automator quick action that runs the following apple script

tell application "System Events"
    set frontApp to name of first application process whose frontmost is true --retrieves name of open application
end tell

tell application frontApp
    repeat while window 2 exists
        close window 2
    end repeat
end tell

It works; however, it takes a while to run if I have a lot of windows open.

Is there a way to close all windows simultaneously except for the frontmost one?

I typed in tell application "Finder" to close every window from 2 to 5 After I compiled it, it automatically changed to tell application "Finder" to close ( windows 2 thru 5 ). However, I get the error message error "Finder got an error: Can’t get windows 2 thru 3." number -1728 from windows 2 thru 3. So, then, it accepts the structure of the command, but I'm still missing something.

Edit: I tried the following script

tell application "Finder"
    close (every window where index is greater than 1)
end tell

However, I get the error "Finder got an error: Handler can’t handle objects of this class." number -10010

Why does this not work?

  • @user3439894 Sorry, I entered the code in wrong. I have now changed tell application "Finder" to tell application frontApp. This code is meant to be run as an Automator service. It has to first retrieve the name of the frontmost application because it needs to be able to run for any application. – Peter Schorn Dec 4 '19 at 23:23
1

Please try this. It might run faster. (edited my original answer with new code).

tell application "Finder"

    set theWindows to {}
    set theCount to (count of windows)
    if theCount is greater than 1 then
        repeat with i from 2 to theCount
            set theWindow to window i
            copy theWindow to end of theWindows
        end repeat
        close theWindows
    end if
end tell
  • It works, although it is just as slow as the original version. I'm still looking for a faster method. – Peter Schorn Dec 4 '19 at 22:47
  • @PeterSchorn I edited my answer with new code. Please try that. – Chris Norman Dec 5 '19 at 0:02
  • 2
    It worked. You're a god damn genius. – Peter Schorn Dec 5 '19 at 0:12
  • Oh, wait. I spoke too soon. This only works with Finder. If I try it with another application, I get [Application] got an error: Can’t make {window id 39921, window id 39922} into type specifier. I think the problem is that the script is referring to the windows by their ID, rather than by their index. – Peter Schorn Dec 5 '19 at 0:27
  • 1
    Your code does EXACTLY what I want—except it only worked with the Finder application. Have you tried it with other applications to see if you get the same error I mentioned in a comment above? By the way, I really appreciate all the effort you're putting into this. – Peter Schorn Dec 5 '19 at 6:14
3

If you're running it as an Automator quick action, then you can close all windows except the frontmost window of the frontmost application by using the code below inside a Run AppleScript action (you can delete any sample code that appears, including on run {input, parameters}...end run:

tell application id "com.apple.SystemEvents" to tell the first process ¬
    whose frontmost = true to tell a reference to windows 2 thru -1 to ¬
    if exists then tell the value of attribute "AXCloseButton" to click
  • 1
    This should be the accepted answer. – Chris Norman Dec 6 '19 at 18:36

You must log in to answer this question.

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