user128511
user128511

Reputation:

Create alert / message box in command line OSX app

I'm trying to create a simple app that just opens an alert. So imagine this

int main(int argc, const char * argv[]) {
    int result = SomeMagicAlertFunction("Hello World", "Yes", "No");
    printf("User picked: %d¥n", result);
}

I've found some info about NSAlert but all the examples are for full OSX Apps, the kind with an app package as in

+-MyApp.app
  |
  +-Contents
    |
    +-MacOS
      |
      +-MyApp

etc, but I just want an alert in a command line app. One file, not an app package. Is that possible in OSX in either C/C++ or Objective C? I saw something about NSRunAlertPanel but that's been removed in Yosemite and says to use NSAlert.

Upvotes: 1

Views: 3704

Answers (1)

user128511
user128511

Reputation:

Found an answer moments later

#import <Cocoa/Cocoa.h>

void SomeMagicAlertFunction(void) {
    NSAlert *alert = [[NSAlert alloc] init];
    [alert addButtonWithTitle:@"OK"];
    [alert addButtonWithTitle:@"Cancel"];
    [alert setMessageText:@"Delete the record?"];
    [alert setInformativeText:@"Deleted records cannot be restored."];
    [alert setAlertStyle:NSWarningAlertStyle];

    if ([alert runModal] == NSAlertFirstButtonReturn) {
    }
    //[alert release];
}

Upvotes: 7

Related Questions