Define the close button:
NSButton *closeButton = [self standardWindowButton:NSWindowCloseButton];
Connect the close button to the custom selector:
[closeButton setTarget:self.delegate]; [closeButton setAction:@selector(closeThisWindow)];
Run a special code and close the window manually.
-(void)closeThisWindow {
source share