16 Replies
      Latest reply on Mar 26, 2020 2:51 PM by Claude31
      wlionel Level 1 Level 1 (0 points)



        Since the beginning of March, I have been trying to respond to a demand by Apple without success:


        The requirement is that when a user closes the main window of my app by clicking on the red x, I must they must be able to relaunch the main window from a menu.


        I have been able to hide the window and relaunch it beu am unable to relaunch the window when the red x is clicked, EVEN THOUGH THE APP ICON IS STILL ON THE DOCK.


        I hope someone can point me in the right direction. Here is my code: (I'm sure I have stuff here I don't need. Would appreciate to know which lines to cut.


        import Cocoa
        import ServiceManagement
        class AppDelegate: NSObject, NSApplicationDelegate {
            @IBOutlet weak var showMainMenu: NSMenu!
            var window: NSWindow!
            var statusBarItem: NSStatusItem?
            func applicationDidFinishLaunching(_ aNotification: Notification) {
                window = NSApplication.shared.windows[0]
                  let statusBar = NSStatusBar.system
             statusBarItem = statusBar.statusItem(
                 withLength: NSStatusItem.squareLength)
                let itemImage = NSImage(named: "SMR_icon")
                itemImage?.isTemplate = true
                statusBarItem?.button?.image = itemImage
                statusBarItem?.button?.action = #selector(self.statusBarButtonClicked(sender:))
                statusBarItem?.button?.sendAction(on: [.leftMouseUp, .rightMouseUp]) // << send action in both cases
                let statusBarMenu = NSMenu(title: "Status Bar Menu")
                    withTitle: "Launch ScorCent Master Review",
                    action: #selector(AppDelegate.LaunchScorCentMasterReview),
                    keyEquivalent: "")
                    withTitle: "Quit ScorCent Master Review",
                    action: #selector(AppDelegate.QuitScorCentMasterReview),
                    keyEquivalent: "")
                     withTitle: "Terminate ScorCent Master Review",
                     action: #selector(AppDelegate.StopScorCentMasterReviewApplication),
                     keyEquivalent: "")
                // Setting menu
                statusBarItem?.button?.menu = statusBarMenu // << store menu in button, not item
            @IBAction func LaunchWindowMain(_ sender: NSMenuItem) {
             func applicationShouldHandleReopen(theApplication: NSApplication, hasVisibleWindows flag: Bool) -> Bool {
                    if !flag{
                        for window: AnyObject in theApplication.windows {
                  return true
            @objc func statusBarButtonClicked(sender: NSStatusBarButton) {
                let event = NSApp.currentEvent!
                if event.type ==  NSEvent.EventType.leftMouseUp {
                    print("Left click!")
                    if let button = statusBarItem?.button { // << pop up menu programmatically
                        button.menu?.popUp(positioning: nil, at: CGPoint(x: -1, y: button.bounds.maxY + 5), in: button)
                } else {
                    print("Left click!")
            @objc func QuitScorCentMasterReview(_ sender: Any?) {
                  print("Close the App")
            @objc func LaunchScorCentMasterReview(_ sender: NSStatusBarButton) {
                print("Launch the App")
            @objc func StopScorCentMasterReviewApplication(_sender:  Any?) {
                print("Stop the App")
        func applicationWillTerminate(_ aNotification: Notification) {