Objective-c How to make GUI window from terminal app

macos, objective-c, user-interface, xcode

Solution

Sadly you need to have a NSApplication running, but you don't need to have an app bundle.

Make sure that when you create your project you select "Foundation" as the type. Then you can set it up like this:

#import <Foundation/Foundation.h>
#import <AppKit/AppKit.h>

@interface MyAppDelegate : NSObject <NSApplicationDelegate>
{
    NSWindow *window;
}
@end

@implementation MyAppDelegate

-(id) init
{
    self = [super init];
    if (self)
    {
        // total *main* screen frame size //
        NSRect mainDisplayRect = [[NSScreen mainScreen] frame];

        // calculate the window rect to be half the display and be centered //
        NSRect windowRect = NSMakeRect(mainDisplayRect.origin.x + (mainDisplayRect.size.width) * 0.25,
                                     mainDisplayRect.origin.y + (mainDisplayRect.size.height) * 0.25,
                                     mainDisplayRect.size.width * 0.5,
                                     mainDisplayRect.size.height * 0.5);

        /*
         Pick your window style:
         NSBorderlessWindowMask
         NSTitledWindowMask
         NSClosableWindowMask
         NSMiniaturizableWindowMask
         NSResizableWindowMask
         */
        NSUInteger windowStyle = NSTitledWindowMask | NSMiniaturizableWindowMask;

        // set the window level to be on top of everything else //
        NSInteger windowLevel = NSMainMenuWindowLevel + 1;

        // initialize the window and its properties // just examples of properties that can be changed //
        window = [[NSWindow alloc] initWithContentRect:windowRect styleMask:windowStyle backing:NSBackingStoreBuffered defer:NO];
        [window setLevel:windowLevel];
        [window setOpaque:YES];
        [window setHasShadow:YES];
        [window setPreferredBackingLocation:NSWindowBackingLocationVideoMemory];
        [window setHidesOnDeactivate:NO];
        [window setBackgroundColor:[NSColor greenColor]];
    }
    return self;
}

- (void)applicationWillFinishLaunching:(NSNotification *)notification
{
    // make the window visible when the app is about to finish launching //
    [window makeKeyAndOrderFront:self];
    /* do layout and cool stuff here */
}

- (void)applicationDidFinishLaunching:(NSNotification*)aNotification
{
    /* initialize your code stuff here */
}

- (void)dealloc
{
    // release your window and other stuff //
    [window release];
    [super dealloc];
}

@end


int main(int argc, const char * argv[])
{

    @autoreleasepool
    {
        NSApplication *app = [NSApplication sharedApplication];
        [app setDelegate:[[[MyAppDelegate alloc] init] autorelease]];
        [app run];
    }
    return 0;
}

Keep in mind that the line [app run]; is blocking so you would have to run your logic either within the application loop or on a new thread (application loop preferred).

Hope it helps.

EDIT: Boo.. I was too late!

Problem

Is there a way in obj-c to make a gui window, while there is no .app bundle, just extcutable? The problem is that I need a gui application(just one dialog). I tried: Import appkit.framework into the terminal app. - crashes with tons of reasons. I decided that using x11 is not a good idea because it is not included in OS X 10.8 So, any ideas?

Original source