NSTask文件路径转换为ShellPath?

时间:2013-05-15 02:30:31

标签: objective-c macos shell osx-mountain-lion nstask

我有一个 Mac (不是iOS)应用程序。我想在用户选择 NSOpenPanel 的文件夹后运行shell命令'find'。以下是我所拥有的。

NSString *path = [url path];
NSString *folderName = [path lastPathComponent];

NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:@"/usr/bin/find"];
NSMutableString *command = [[NSMutableString alloc] initWithString:path];
[command appendString:@" -name '._*' -type f "];
NSArray* args = [NSArray arrayWithObjects:@"commit",command,nil];
[task setArguments:args];
NSPipe *pipe;
pipe = [NSPipe pipe];
[task setStandardOutput: pipe];
[task launch];
NSFileHandle *file;
file = [pipe fileHandleForReading];
NSData *data;
data = [file readDataToEndOfFile];
NSString *output = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog(@"%@",output);

这是我第一次使用使用Objective-C开发的Mac应用程序运行shell命令。我想我是在正确的轨道上。无论如何,当我选择一个文件夹时,我得到以下调试器输出消息。

  • find:commit:没有这样的文件或目录
  • find:find:/ Volumes / SDHC 16 GB / More -name'._ *'-type f:没有这样的文件或目录

我认为shell命令无法读取此类文件路径。我是否需要将其转换为shell路径或其理解的内容?如果是这样,怎么样?

感谢您的建议。

1 个答案:

答案 0 :(得分:1)

我明白了。

NSString *path = [url path];
NSString *folderName = [path lastPathComponent];

NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:@"/usr/bin/find"];
NSMutableString *command = [[NSMutableString alloc] initWithString:@""];
[command appendString:@" -name '._*' -type f"];
NSArray* args = [NSArray arrayWithObjects:path,command,nil];
[task setArguments:args];
NSPipe *pipe;
pipe = [NSPipe pipe];
[task setStandardOutput: pipe];
[task launch];
NSFileHandle *file;
file = [pipe fileHandleForReading];
NSData *data;
data = [file readDataToEndOfFile];
NSString *output = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog(@"%@",output);