我正在尝试通过运行终端命令在 Finder 中显示隐藏文件。问题是看起来defaults write com.apple.finder AppleShowAllFiles TRUE
有多个启动路径。当我在终端中运行 which
命令后跟我尝试运行的命令时,它给了我三条路径:/usr/bin/defaults
、/usr/bin/write
和 /usr/bin/TRUE
。我无法将所有三个都设置为命令的启动路径,因为.launchPath
不接受数组。
如何运行此命令?
编辑:问题是我没有将参数分成单独的字符串。此代码有效:
@IBAction func showAllFiles(_ sender: NSMenuItem) {
let task = Process()
task.launchPath = "/usr/bin/defaults"
task.arguments = ["write", "com.apple.finder", "AppleShowAllFiles", "TRUE"]
task.launch()
task.waitUntilExit()
}
您正在寻找/usr/bin/defaults
.您获得此输出的原因是以下命令:
which defaults write com.apple.finder AppleShowAllFiles TRUE
就像做:
which defaults
which write
which com.apple.finder
which AppleShowAllFiles
which TRUE
其中打印:
/usr/bin/defaults
/usr/bin/write
(无)
(无)
(无)
/usr/bin/TRUE