Can't reopen app from menu after closed

Hello:


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


@NSApplicationMain
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")
        statusBarMenu.addItem(
            withTitle: "Launch ScorCent Master Review",
            action: #selector(AppDelegate.LaunchScorCentMasterReview),
            keyEquivalent: "")

        statusBarMenu.addItem(
            withTitle: "Quit ScorCent Master Review",
            action: #selector(AppDelegate.QuitScorCentMasterReview),
            keyEquivalent: "")
       
        statusBarMenu.addItem(
             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{
               window.makeKeyAndOrderFront(nil)
                for window: AnyObject in theApplication.windows {
                window.makeKeyAndOrderFront(self)
            }

        }
          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?) {
        NSApplication.shared.hide(QuitScorCentMasterReview)
          print("Close the App")
        }

    @objc func LaunchScorCentMasterReview(_ sender: NSStatusBarButton) {
       NSApplication.shared.unhide(AppDelegate.LaunchScorCentMasterReview)
        NSApplication.shared.unhide(AppDelegate.init())
        print("Launch the App")
        }
  
    @objc func StopScorCentMasterReviewApplication(_sender:  Any?) {
        NSApplication.shared.terminate(AppDelegate.StopScorCentMasterReviewApplication)
        print("Stop the App")
    }
func applicationWillTerminate(_ aNotification: Notification) {
        
    }

Replies

Maybe check to see if the window still exists when you try to open it. If it doesn't allocate it before trying to show it. Speaking of showing it, try using showWindow to show it.

Hello Janabanana:


Right now I am not familiar with the process of how to check to see if a window is still open. I'll have to research that. But lets assume that the window is NOT open, what method is used to set it open before calling for it to re-open? OR ensuring that it doesn't cease to exist after clicking the red x. Isn't that just hiding the window?

You wrote in a previous post that you would clarify Apple's request.


Did you get this clarification ?


Otherwise, the answer to your older post still applies.

Closing a window with the red button is not the same as hiding a window. Depending on other settings, closing the window may release it which means it no longer exists.


I'm not a swifty so you will have to translate this objective C code on your own.


Assuming Preferences is a class which, upon initialization, creates its window.

Assuming preferences has been declared in the interface:


Note that this could be any window, not just a Preferences window.

Preferences *preferences; // in the interface

-(IBAction)showMyPreferences:(id)sender { // the action to show the preferences
    if(!preferences) {
        preferences = [[Preferences alloc] init];
    }
    [preferences showWindow:self]; // the code to actually show the preferences window
}


In the Preferences class:

-(instancetype)init {
    self = [super init];
    if ((self = [self initWithWindowNibName:@"Preferences"])) { // Preferences is also the nib name of the window
       // do other initialization stuff here
    }
    return self;
}


If you are determined to keep all your code in one class, the AppDelegate, then you can experiment with testing to see if the window still exists and if not, recreate it and then show it.

Hello Claude31:


I sent Apple some videos and here is the I got this morning:



Thank you for your response and videos.


However, we still find that the app does not behave in the same manner shown.


We have now provided a video showing the issues in action.


You may be using your app in a privileged environment or have access to content not available in the binary.


We look forward to reviewing your revised binary.


Best regards,


App Store Review



Do you know what settings in Xcode may be allowing me to see my app acting differently than a binary sent to App Store Connect?


Despite the App Store Review comments, I still need to solve the problem of getting a menu item to re-open after I click the red x. I included a menu item in the window menu called "Re-Open Main Mindow" but when the window is closed with the red x that item becomes disabled in the menu. The item was connected to First Responder "openWindow" and by an @IBOutlet in AppDelegate Class.

Hello janabanana:


Despite the App Store Review comments I shared with Claude31, I still need to solve the problem of getting a menu item to re-open after I click the red x. I included a menu item in the window menu called "Re-Open Main Mindow" but when the window is closed with the red x that item becomes disabled in the menu. The item was connected to First Responder "openWindow" and by an @IBOutlet in AppDelegate Class.

Could you post links to the videos ?

Did you read what I posted? Pressing the red x closes the window. Most likely closing the window also deallocates it. If the window is deallocated, this means it no longer exists. You can do showWindow, unHideWindow, etc. for ever and nothing will happen since there isn't a window to show or unHide. You can't re-open the main window if the main window does not exist. When you want to reopen it, you need to test to see if it still exists. If it doesn't, you need to recreate it before you try to open it.

I have not been able to finsd a workable posting or in Swift to test to see if a window still exists.


Some code will be helpful.


Thanks

I suppose the code in your previous reply is the code i need to implement in App delegate to test if the Main Window is still open?

Hello Claude:


I posted the links but it is saying Reply is "Currently is being moderated". So I assume you cant see it.


In the meantime I am trying to follow-up with some log responses. The following says:


Launch Main Window[76488:14123793] [Nib Loading] Failed to connect (reopenMainWindow) outlet from (Launch_Main_Window.AppDelegate) to (NSMenuItem): missing setter or instance variable


Does this say anything to you about what is going on?

I got rid of the error in the last post but the one with the links is still saying it is being moderated.

Followed the link, but nothing as Appstore Connect reponse…


Could you show the code where reopenMainWindow is declared ?


Note: moderation is because you wrote a valid URL.

start your URL with h. ttps (with a space) and it will go (there is no more risk for someone to fire the URL accidentally).