Hello. Not a problem at all. I’m glad I was able to help you get to a solution. We’re here to help!
So, for others wondering what happened:
NSTask *task = [NSTask new];
//prepare task
// One or more settings here in preparing the task
// had an issue. In this case, a directory being
// referenced did not actually exist.
[task launch]; //crashed
And though you probably know all of this, for the benefit of those learning, let me cover some other basics about NSTask() (now Process() in Swift 4).
Note in Swift 4, you now use “Process()”
Process | Apple Developer Documentation
It’s interesting to note for others that after you create an instance of NSTask/Process you have to set other properties (updated for Swift 4):
- directoryPath ← must exist in order to avoid a crash
- lauchPath ← must exist in order to avoid a crash
- arguments ← applicable to whatever the application you’re launching requires
Below is a very simple example that calls up a directory list (“ls -la”) on your root folder.
Drop this into a playground to see your ROOT directory content in the xcode console.
import Foundation
// 1
let task = Process()
// 2
task.launchPath = "/bin/ls"
// 3
task.arguments = ["-la"]
// 4
task.launch()
task.waitUntilExit()
- Setup a new instance of the Process class (used to be NSTask).
- Set the path of the executable. In this case, we’re calling up “ls” (directory list), but this could be anything as long as the path is correct.
- Pass arguments to the command, in this case “-la” to get a column formatted list.
- Launch and wait.
Note, I saw examples of folks calling “/bin/bash” (a shell) and then using the “-c” argument to pass the command as an argument. For example, the above cold be rewritten as follows.
Drop this into a playground to see your ROOT directory content in the xcode console.
import Foundation
let task2 = Process()
task2.launchPath = "/bin/bash"
task2.arguments = ["-c", "ls -la"]
task2.launch()
task2.waitUntilExit()
Once again, thanks for your question and for fostering this discussion.
Eric.