2

I'm doing a simple OSX App to show/hide hidden files in the Finder from a StatuBar menu.

This is the IBAction to show/hide files:

 @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()
}

This gives me this error:

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

I tried to find out why but cannot find an answer.

I tried also to see which of the two launchPath was wrong by disabling one or the other and they both give the same error.

Can somebody help me?

Mr.Web
  • 6,992
  • 8
  • 51
  • 86
  • 1
    Shouldn't the launch path begin with a slash? "/usr/bin/killall", "/usr/bin/defaults" – Eric Aya May 10 '15 at 23:00
  • Yes!!!! That's the reason! Tks!!! (Place it as an answer so I can check it) ;D – Mr.Web May 11 '15 at 21:33
  • Any idea why I can't run a program which is in the /Users/me/Downloads/ folder? I keep getting "launch path not accessible" although I can run anything inside /usr/bin. – adamsfamily Dec 13 '17 at 12:38

1 Answers1

1

Both var and usr are at the same level, so you need to prefix usr with / like you did for var:

task.currentDirectoryPath = "/var/tmp/"
task.launchPath = "/usr/bin/defaults"
killTask.launchPath = "/usr/bin/killall"
Eric Aya
  • 69,473
  • 35
  • 181
  • 253