Close the window

How do I close a window using ui scripting. The nasty_windows list returns the windows I want to close but sys events doesn’t recognise the close command… Like

close window aWin -- or 
close aWin -- or
close every window of nasty_window -- or
tell window aWin
      close
end tell
-- ive also set a variable to {x, y} position of button 1 of a window (which is the red x button and tried to use clilclick, to no avail. 
(BTW Logic Pro doesn't accept the click command (well it does but creates a 5 sec delay before it moves on to the next command))

Here’s a script that returns all the windows that are nasty. What do I do to close them either one at a time in the loop or all at once with the nasty_windows


tell application "System Events"
	set cnt_win to get every window of process "Logic Pro"
	set nasty_window to {}
	repeat with i from 1 to count of cnt_win
		set aWin to item i of cnt_win
		if name of aWin does not contain "logicx" then
			set end of nasty_window to aWin
		end if
	end repeat
end tell

thanks in advance

In Logic Pro X clicking the close button works fine and fast

tell application "System Events"
	tell process "Logic Pro X"
		click (first button of window 1 whose description is "close button")
	end tell
end tell

Without access to Logic Pro, it’s hard to comment on what impact of the 5-second delay. But, Generally, to close all windows of an application visible in the current desktop space :

tell application id "com.apple.systemevents" to tell ¬
        process "{process name}" to tell its windows ¬
        to click value of attribute "AXCloseButton"

To target a subset of windows, e.g. the one’s that don’t have "logicx" in their title:

tell application id "com.apple.systemevents" to tell ¬
        the process "Logic Pro" to tell (its windows ¬
        whose name doesn't contain "logicx") to tell ¬
        attribute "AXCloseButton" to click its value

Intuitively, I’d say that doing it as a single (compound) command would negate the opportunity for the 5-second delay to arise, or, if it did, then hopefully it’s restricted to arising either immediately before or immediately after enacting the above. Iterating through windows may potentially incur a 5-second delay between every window closure, but you can experiment with that.

1 Like

Apple + w will close all the main project windows (names containing logicx) before it closes any plug-in windows.

Thanks CJK,

Your second script works.

Much appreciated.

1 Like