How to programmatically launch an OS X app in minimized mode?

cocoa, macos, objective-c, xcode

Solution

Uncheck visible at launch in your xib for main application window.

Implement `- (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag` in your app delegate class.

- (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag
{
    if (!flag) {
        [window makeKeyAndOrderFront:self];
        return YES;
    }
    return NO;
}

Problem

I need to launch my application on startup, but I want it to be "minimized", meaning, it will be opened in the dock but its window won't be displayed. Same like TeamViewer, if you know this application. I currently use `launchctl` with a plist I've added to `~Library\LaunchAgents`, and indeed on startup the application is launched, and its window is shown. How can I launch it in such a hidden / minimized state?

Original source