12
votes

I am creating an OS X status bar application, so I want the application to start hidden.

I have created a "storyboard" application, and the initial window always shows up, even if "Visible at launch" is unchecked (was unchecked by default).


Note: if I disable "Is initial controller" then the app correctly starts without any window, but my (now orphan) window seems to never be added to the storyboard:

var mainWindow = NSStoryboard(name: "Main", bundle: nil)?.instantiateControllerWithIdentifier("mainWindow")

The "mainWindow" controller is not found (even though I correctly set "Storyboard ID" on the Window Controller).

So I think it's better to leave "Is initial controller" but simply have the main window hidden at the start…

4
Forgive me for responding to the question more than 1 year ago, but the window can be instantiated from the storyboard even if you don't set it as initial view controller. Maybe you don't hold the window or window controller instance, I guess.Blaszard

4 Answers

33
votes

Uncheck the "Is Initial Controller" box on the storyboard, leaving your app without an initial controller. Your app will run, but will have no window.

screenshot

11
votes

This might be a bit of a hack, but you can do this

func applicationDidFinishLaunching(notification: NSNotification) {
    // Insert code here to initialize your application
    NSApplication.sharedApplication().windows.last!.close()
}

And then later on...

NSApplication.sharedApplication().windows.last!.makeKeyAndOrderFront(nil)
NSApplication.sharedApplication().activateIgnoringOtherApps(true)
7
votes

Uncheck "Is Initial Controller", but then you need to set the storyboard and its associated NSWindowController manually.

The precise way of doing that is shown in this answer, which I'll quote here:

[...] in your AppDelegate, set up a property for the window controller:

@property NSWindowController *myController;

In your applicationDidFinishLaunching: method implementation, create a reference to the Storyboard. This way you get access your window controller from the storyboard. After that, the only thing left to do is to display the window by sending your window controller the showWindow: method.

#import "AppDelegate.h"

@interface AppDelegate ()
@end

@implementation AppDelegate

@synthesize myController;

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    // get a reference to the storyboard
    NSStoryboard *storyBoard = [NSStoryboard storyboardWithName:@"Main" bundle:nil]; 
    // instantiate your window controller 
    myController = [storyBoard instantiateControllerWithIdentifier:@"secondWindowController"];
    // show the window
    [myController showWindow:self];
}

@end
1
votes

The way to do this is just like you tried:

let storyboard = NSStoryboard(name: "Main", bundle: nil)
guard let mainWC = storyboard.instantiateControllerWithIdentifier("MainWindowController") as? MainWindowController else {
   fatalError("Error getting main window controller")
}
// optionally store the reference here
self.mainWindowController = mainWC

mainWC.window?.makeKeyAndOrderFront(nil) // or use `.showWindow(self)`

The only thing you probably forgot was to uncheck "Release when closed". This would immediately release the window and prevents the storyboard loading mechanism from finding it even if you had the right identifier.