2

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?

4

3 回答 3

6

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!

于 2013-04-11T20:48:12.053 回答
1

What you are describing should be possible without creating a .app, however you will need to run the AppKit event loop (otherwise, where would user input events come from?).

The simplest way to do this would be to get the shared NSApplication object and call its run method. You may be able to set up an event loop manually using nsrunloop, however I'm not sure whether the input sources needed are a public API.

Why do you not want a .app? It is likely that whatever you are doing will be easier if you follow the standard patterns.

于 2013-04-11T20:24:43.640 回答
0

I think you should just make a cocoa application and simply drag and drop the files you need into the new application. Make sure to tick the box "add to target" and "copy items into destination. . ."

I have made many of these little helper applications that does a simple task just to help me out with something else. And this is usually the way I do it.

于 2013-04-11T20:14:53.467 回答