【问题标题】:NSTask File Path Converting Into ShellPath?NSTask 文件路径转换为 ​​ShellPath?
【发布时间】:2013-05-15 02:30:30
【问题描述】:

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

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: find: /Volumes/SDHC 16 GB/More -name '._*' -type f : 没有这样的文件或目录

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

感谢您的建议。

【问题讨论】:

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


    【解决方案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);
    

    【讨论】:

      猜你喜欢
      • 2015-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-11
      • 1970-01-01
      • 2014-05-04
      • 2011-07-28
      • 2014-07-19
      相关资源
      最近更新 更多