【问题标题】:using open command with NSTask使用带有 NSTask 的 open 命令
【发布时间】:2016-07-12 19:49:50
【问题描述】:

我对 Swift 中的 NSTask 有相当基本的了解,并且可以在我的程序中运行一些命令,但是当我运行 open 时,它说文件不存在。我添加了我在终端中使用的确切参数,但我不知道我错过了什么。这是代码:

task.launchPath = "/usr/bin/open"
task.arguments = ["/Volumes/STS*/here.docx"]
task.launch()
task.waitUntilExit()

提前致谢。这是我的第一个问题,如果它很愚蠢,请原谅。

【问题讨论】:

  • NSTask 没有通配符扩展。
  • 谢谢,这解释了很多。
  • @MartinR,你对如何解决这个问题有什么建议吗?

标签: swift nstask


【解决方案1】:

NSTask 不会在您的路径中扩展 *。但如果你通过 shell 调用命令(即/bin/bash),它会:

let task = NSTask()
task.launchPath = "/bin/bash"
task.arguments = ["-c", "open /Volumes/STS*/here.docx"]
task.launch()
task.waitUntilExit()

-c 选项告诉 Bash 从字符串中读取命令。您可以查看手册页以获取更多信息。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-26
    • 2011-10-17
    • 1970-01-01
    • 2011-05-30
    • 2012-09-05
    • 1970-01-01
    相关资源
    最近更新 更多