Gregor Isack
Gregor Isack

Reputation: 1141

NSTask output into a list

I try to list files/folder in a director by using NSTask, how do I get the output in each line into a list? I try print it out and it only display the first line.

NSTask * list = [[NSTask alloc] init];
[list setLaunchPath:@"/bin/ls"];
[list setCurrentDirectoryPath:@"/"];

NSPipe * out = [NSPipe pipe];
[list setStandardOutput:out];

[list launch];
[list waitUntilExit];

NSFileHandle * read = [out fileHandleForReading];
NSData * dataRead = [read readDataToEndOfFile];
NSString * stringRead = [[NSString alloc] initWithData:dataRead encoding:NSUTF8StringEncoding]];
HBLogDebug(@"PATH: %@", stringRead); //Only the first line printed

NSMutableArray *outputlist = [[NSMutableArray alloc] init];

//This part here I'm not so sure how to proceed since it's only one line, I expected multiples line
for (NSString *s in stringRead?dataRead?){
    [outputlist addObject:s];
}

Thanks in advance!

Upvotes: 0

Views: 190

Answers (2)

CRD
CRD

Reputation: 53010

After the comments here is a working example built in Xcode. The runCommand method is based on this answer updated and with optional use of waitUntilExit so you can see not using it does not freeze the app.

@implementation AppDelegate

// Arguments:
//    atPath: full pathname of executable
//    arguments: array of arguments to pass, or nil if none
// Return:
//    the standard output, or nil if any error
//
// This method blocks the caller until the called command completes
// (due to both readDataToEndOfFile & waitUntilExit)
// If this an issue either run on a thread or use the asynchronous read alternatives to readDataToEndOfFile
// If there is no need to check terminatation status the waitUntilExit and terminationStatus can be skipped
// by defining RUN_COMMAND_CHECK_TERMINATION as 0

#define RUN_COMMAND_CHECK_TERMINATION 0

+ (NSString *) runCommand:(NSString *)atPath withArguments:(nullable NSArray *)arguments
{
   NSTask *task = [NSTask new];
   NSPipe *pipe = [NSPipe new];

   task.standardOutput = pipe;     // pipe standard output

   task.launchPath = atPath;       // set path
   if(arguments)
      task.arguments = arguments;  // set arguments

   [task launch];                  // execute

   NSData *data = pipe.fileHandleForReading.readDataToEndOfFile; // read standard output

#if RUN_COMMAND_CHECK_TERMINATION
   [task waitUntilExit];           // verify process has exited to prevent terminationStatus faulting
                                   // must do this *after* read otherwise if pipe fills wait can block forever

   if (task.terminationStatus != 0) // check termination status & have output
      return nil;
#endif

   if (data == nil) // check termination status & have output
      return nil;

   return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; // return stdout as string
}

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
   NSString *stringRead =  [AppDelegate runCommand:@"/bin/ls" withArguments:@[@"/"]];
   NSLog(@"PATH: %@", stringRead);
   NSArray *outputList = [stringRead componentsSeparatedByString:@"\n"]; // Note: will produce an empty last line, removing that is left as an exercise
   NSLog(@"Lines: %@", outputList);
}

@end

If your code is freezing either its down to something strange with HBLogDebug (as the above uses NSLog) or something else in your code which you'll have to ferret out.

HTH

Upvotes: 1

Shakir Zareen
Shakir Zareen

Reputation: 240

1.Use NSLog instead of HBLogDebug

`NSLog(@"PATH: %@", stringRead); //it prints all lines in my case`
  1. split the stringRead by newline characters

    for (NSString *s in [stringRead componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]]){ [outputlist addObject:s]; }

  2. Better way to list files in objective-c for MACOS

    [[NSFileManager defaultManager] contentsOfDirectoryAtPath:@"<path>" error:nil]

Upvotes: 0

Related Questions