Swift错误:无法访问启动路径

时间:2015-05-10 21:57:07

标签: shell swift

我正在做一个简单的OSX应用程序,以便从StatuBar菜单中显示/隐藏Finder中的隐藏文件。

这是显示/隐藏文件的IBAction:

 @IBAction func menuClicked(sender: NSMenuItem) {
    let task = NSTask()
    task.currentDirectoryPath = "/var/tmp/"

    task.launchPath = "usr/bin/defaults"

    if(sender.state == NSOnState){
        sender.state = NSOffState
        task.arguments = ["write", "com.apple.finder", "AppleShowAllFiles", "NO"]
    }else{
        sender.state = NSOnState
        task.arguments = ["write", "com.apple.finder", "AppleShowAllFiles", "YES"]
    }

    task.launch()
    task.waitUntilExit()

    let killTask = NSTask()
    killTask.launchPath = "usr/bin/killall"
    killTask.arguments = ["Finder"]
    killTask.launch()
}

这给了我这个错误:

2015-05-10 23:54:22.237 ShowHideFiles[1234:303] An uncaught exception was raised
2015-05-10 23:54:22.238 ShowHideFiles[1234:303] launch path not accessible

我试图找出原因但无法找到答案。

我还尝试通过禁用其中一个来查看两个launchPath中的哪个是错误的,并且它们都给出了相同的错误。

有人能帮助我吗?

1 个答案:

答案 0 :(得分:1)

varusr都处于同一级别,因此您需要像usr一样/var前缀:

task.currentDirectoryPath = "/var/tmp/"
task.launchPath = "/usr/bin/defaults"
killTask.launchPath = "/usr/bin/killall"