0

I created a command line tool. I have my project almost done, but now I need to create an user interface to ask for user credentials. Is possible to add a window to my command line project, or I have to create a cocoa application to do that?

Ana
  • 603
  • 1
  • 5
  • 12
  • 2
    Command-line tools don't ask for credentials, they expect to be run from `sudo`. – hamstergene Oct 31 '11 at 13:16
  • Ok, but if I need to ask for credentials in a custom window, Do I have to create a cocoa application? – Ana Oct 31 '11 at 13:43
  • @Ana: If you need to ask for credentials, do it at the command line using stdio and, for the password, `getpass`. If you need root powers, do what hamstergene said: Expect to already have root powers, and complain to the user if you don't have them. – Peter Hosey Oct 31 '11 at 19:29

3 Answers3

1

A command-line tool won't have a connection to the window server (that's done by NSApplication), so no, it can't create a window. If you need to show a window for any reason, it probably should be an application at that point anyway, so you should go with that and make it one.

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
0

Yea Peter is right, you should make it into an application but that doesn't mean you need to convert the tool into a Cocoa app, you can keep a command line tool alive with a UI by utilising the NSApplication class but not using NSApplicationMain() to launch it. In your situation however I don't think this is the necessary method but anyway here is how I displayed a window in a command line tool:

@interface CustomAppClass : NSApplication
    @property (strong) NSWindow *theWindow;
@end
@implementation CustomAppClass {
    BOOL terminate;
}

- (void)finishLaunching {
    /* Draw windows and stuff */
    self.theWindow = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 300, 200) styleMask:NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable backing:NSBackingStoreBuffered defer:NO];
    [self.theWindow setTitle:@"Why Hello There!"];
    [self.theWindow center];
    [self.theWindow makeKeyAndOrderFront:self.theWindow];
}

// Override run and terminate: methods
- (void)run {
    terminate = NO;
    [self finishLaunching];
    do {
        NSEvent *event = [self nextEventMatchingMask:NSAnyEventMask untilDate:[NSDate distantFuture] inMode:NSDefaultRunLoopMode dequeue:YES];
        [self sendEvent:event];
        [self updateWindows];
    } while (!terminate);
}
- (void)terminate:(id)sender {
    terminate = YES;
}

@end


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

    // Do all your pre functions here

    /* Make NSApplication from CustomAppClass */
    CustomAppClass *appObject = [CustomAppClass sharedApplication];
    if ([appObject respondsToSelector:@selector(run)]) {
        [appObject performSelectorOnMainThread:@selector(run) withObject:nil waitUntilDone:YES];
    }

    // Do all your post functions here.

    return 0;
}

Sorry I didn't describe how to ask for credentials and stuff but if you want an official window based authentication (not sudo) you should probably look into the Security framework.

Some Credit to https://www.cocoawithlove.com/2009/01/demystifying-nsapplication-by.html

Hope this helps in any way.

YeaTheMans
  • 1,005
  • 8
  • 19
0

A Cocoa App would work. I haven't used Tcl/Tk or MacRuby, but if you're brand new to Cocoa you might have an easier time picking them up.

You can't do this in AppleScript without using AppKit APIs; see Prompt user for password with dialog window when using sudo.

If you're really asking an Xcode question, you might want to repost something more specific, like "how do I convert my Command-Line project so I can add a Cocoa window."

Community
  • 1
  • 1
paulmelnikow
  • 16,895
  • 8
  • 63
  • 114