Let's get back to the technical issues here.
— On macOS, there is a difference between closing a window and quitting (terminating) an app.
— In general, there are single-window apps (which either have precisely one window, or have one main window, and other windows are auxiliary), and multiple-window apps (which have one window per document or task, or something similar). It is usual for single-window apps to quit automatically when their (main) window is closed. It is not usual for multiple-window apps to do that.
— If your app is a single-window app, then override the NSApplicationDelegate method "applicationShouldTerminateAfterLastWindowClosed" that Quinn referred to to get the automatic-termination behavior.
— If you want to put up an alert when the app is about to terminate, then you can override the NSApplicationDelegate method "applicationShouldTerminate" to cause the alert to be displayed, and return the ".terminateLater" result to give your alert a chance to be handled. You would use the NSAlert class for such an alert, typically.
— If you want to do something more complicated than put up an alert (such as segue to a different window in a storyboard), you likely can't use the above technique. Instead, you would likely need to intervene when windows are closed (as Claude was suggesting), but you're going to have to figure out the logic of terminating or not terminating yourself. I wouldn't recommend this approach if you can use the above technique instead. (For example, if you want to tell the user how long is left till the end of a trial period, a NSAlert should be all you need.)