我有一个 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命令。我想我是在正确的轨道上。无论如何,当我选择一个文件夹时,我得到以下调试器输出消息。
我认为shell命令无法读取此类文件路径。我是否需要将其转换为shell路径或其理解的内容?如果是这样,怎么样?
感谢您的建议。
答案 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);