【问题标题】:Command does not return results in SWIFT命令不会在 SWIFT 中返回结果
【发布时间】:2015-08-26 08:06:08
【问题描述】:

我正在尝试运行命令行工具并获取结果。 我在终端检查了它:

/usr/bin/exiftool -filename -directory -createdate -model "/Users/dirk/Desktop\" -t -S -q -r -f >"RenamerOutfile0.txt"

这运行良好并在文件中提供结果。 使用 SWIFT 我试过这个:

let task = NSTask()
task.launchPath = "/usr/bin/exiftool"
task.arguments = ["-filename -directory -createdate -model \"/Users/dirk/Desktop\" -t -S -q -r -f"]
let pipe = NSPipe()
task.standardOutput = pipe
task.launch()

let data = pipe.fileHandleForReading.readDataToEndOfFile()

不幸的是,什么也没发生。数据被分配 0 字节 - 没有结果。 如果我将重定向插入文件,则不会创建任何文件。 知道从终端调用该工具与使用此任务有什么区别吗?

【问题讨论】:

  • 使用:["-filename", "-directory", "-createdate" ...] 例如参数数组。您也可能希望将数据转换为纯字符串。
  • 我添加了: task.arguments = ["-filename", "-directory", "-createdate", "-model", "\"/Users/dirk/Desktop\" ", "-t", "-S", "-q", "-r", "-f"] 稍后我想使用 let output: String = NSString(data: data, encoding: NSUTF8StringEncoding )!.stringByAppendingString("") 但数据仍然是 0 字节长。
  • 无法格式化我的文本,因为我太慢了! :-) 5 分钟限制

标签: shell redirect terminal command nstask


【解决方案1】:

我找到了解决方法:

let x = system("/usr/bin/exiftool -filename -directory -createdate -model \"/Users/dirk/Desktop\" -t -S -q -r -f >\"/var/tmp/RenamerOutfile0.txt\"")

这很好用。也许是因为我为输出文件添加了路径 /var/tmp?没关系! :-)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-03-10
    • 1970-01-01
    • 1970-01-01
    • 2017-12-28
    • 1970-01-01
    • 2016-11-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多