Close every window

Hi
is there any way to close every open window ?

Do you mean every open window on your screen or in your app? If you mean in your app, I don’t know of any single command that will do that, but you could use NSApplication’s windows() method to return an array of all the windows, and then loop through the array sending a close or orderOut message to each.

Ric

i mean every open window in my app.

what do you mean with “NSApplication’s windows() method to return an array of all the windows, and then loop through the array sending a close or orderOut message to each.”

Have you got an example ?

Thanks

set theApp to current application's NSApp
		repeat with aWindow in theApp's |windows|()
			aWindow's |close|() -- or aWindow's orderOut_(me) 
		end repeat

That should do it.

Ric

:frowning: It doesn’t work.

May be there will be an other way to explain my problem

I’ve a window which is the menu (menuWindow)
With button (in menuWindow) i call other window for example cartoucheWindow. So when i want an other window i need to close cartoucheWindow (but not menuwindow) and open the new one (for example documentWindow).

In every case, i don’t know which window is open (except of course menuWindow)

Thanks for your help

This code will close all the windows except the one with the title “Menu”, which has a button that calls the “push_” method when clicked.

on push_(sender) --IBAction for a button in menuWindow
		set theApp to current application's NSApp
		repeat with aWindow in theApp's |windows|()
			if aWindow's |title| as string is not "Menu" then
				aWindow's |close|() -- or aWindow's orderOut_(me) 
			end if
		end repeat
	end push_

I can’t tell from your description how you want to open other windows. Do you have a different button for each window? The code above only closes windows, it doesn’t open any – I would have to know how you want to open windows to make the opening and closing happen in one method.

Ric

:slight_smile: i find an other to make it work


script CPo_ToolBoxAppDelegate
	property parent : class "NSObject"
	
	-- IBoutlet -------------------------------------------------------------
	property menuWindow : missing value
	property cartoucheWindow : missing value
	property documentWindow : missing value
	property artboardWindow : missing value
	property exportWindow : missing value
	
	-- Variable definition -------------------------------------------------------------
	property activeWindow : ""
	
	
	-- IBaction ---------------------------------------------------------------------
	on openCartoucheWindow_(sender)
		my closeActiveWindow_()
		my openNewWindow_(cartoucheWindow)
	end openCartoucheWindow_
	
	on openDocumentWindow_(sender)
		my closeActiveWindow_()
		my openNewWindow_(documentWindow)
	end openDocumentWindow_
	
	on openArtboardWindow_(sender)
		my closeActiveWindow_()
		my openNewWindow_(artboardWindow)
	end openArtboardWindow_
	
	on openExportWindow_(sender)
		my closeActiveWindow_()
		my openNewWindow_(exportWindow)
	end openExportWindow_
	
	
	
	-- Routine ---------------------------------------------------------------------
	on closeActiveWindow_()
		activeWindow's orderOut_(me)
	end closeActiveWindow_
	
	on openNewWindow_(newWindow)
		newWindow's orderFront_(me)
		set activeWindow to newWindow
		activeWindow's makeKeyAndOrderFront_(me)
	end openNewWindow_
	
	
	-- Application ---------------------------------------------------------------------
	on applicationWillFinishLaunching_(aNotification)
		-- Insert code here to initialize your application before any files are opened 
		delay 2
		menuWindow's orderFront_(me)
		tell every window to center
	end applicationWillFinishLaunching_

In fact, i attribute the name of the new window to activeWindow