0

So currently I'm working on a little personal project that runs a bash script in a resources bundle, and I can get it to properly output the console log to NSLog, but the strings won't rewrite an NSTextField or redraw an NSTextView

- (IBAction)doIt:(NSButton *)sender
{
    NSTask *task;
    task = [[NSTask alloc] init];
    [task setLaunchPath: @"/bin/sh"];

    NSArray *arguments;
    NSString* newpath = [[NSBundle mainBundle] pathForResource:@"run" ofType:@"sh"];
    NSLog(@"shell script path: %@",newpath);
    arguments = [NSArray arrayWithObjects:newpath, nil];
    [task setArguments: arguments];

    NSPipe *pipe;
    pipe = [NSPipe pipe];
    NSPipe *input = [NSPipe pipe];
    [task setStandardOutput: pipe];
    [task setStandardInput: input];

    NSFileHandle *file;
    file = [pipe fileHandleForReading];
    [task launch];

    NSData *data;
    data = [file readDataToEndOfFile];

    NSString *string;
    string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];


    NSLog (@"script returned:\n%@", string);
}

So the string at the end will output properly with NSLog, but that's all I can do... any ideas?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
conedmiro
  • 103
  • 10
  • the answer to this [related question](http://stackoverflow.com/questions/6635086/nstask-character-output-to-nstextfield-unbuffered-as-continuous-stream?rq=1) might help you out... – Michael Dautermann Oct 30 '15 at 01:38

1 Answers1

-1

You will need to create an NSTextField or NSTextView, either in your XIB file or programmatically in your code.

If you'd like to create it in your XIB file, here's how to do it (for ARC):

1) Add an IBOutlet property to your .h file in your controller (e.g. AppDelegate):

@property (nonatomic, weak) NSTextField *textField;

OR

@property (nonatomic, assign) NSTextView *textView;

2) Go to your XIB file, create a new NSTextField or NSTextView and put it on a window. Right-click on your controller class and drag a connection to your NSTextField or NSTextView.

3) Now that the IBOutlet is connected to your view in your XIB, all you need to do is set the string value in your .m file by calling

[self.textField setStringValue:[NSString stringWithFormat:@"script returned:\n%@", string]];

for an NSTextField

OR

[self.textView setString:[NSString stringWithFormat:@"script returned:\n%@", string]];

for an NSTextView

I hope this helps.

Toby
  • 181
  • 1
  • 9