0

I'm trying to get a button in Xcode to run a shell script with clicked.

This works

@IBAction func test(_ sender: NSButton) {
        let path = "/usr/bin/say"
        let arguments = ["hello world"]     
        sender.isEnabled = false
        let task = Process.launchedProcess(launchPath: path, arguments: arguments)
        task.waitUntilExit()
        sender.isEnabled = true
    }

But when I try this it does not work to run a script from the Desktop

@IBAction func test(_ sender: NSButton) {
     let path = "/bin/bash"
     let arguments = ["~/Desktop/test.sh"]      
     sender.isEnabled = false
     let task = Process.launchedProcess(launchPath: path, arguments: arguments)
     task.waitUntilExit()     
     sender.isEnabled = true
 }

I get this error output in Xcode

/bin/bash: ~/Desktop/test.sh: No such file or directory

If anyone can help me with some help or example that would great. Thank you.

7
  • Maybe it's not able to process the ~ as home dir. Try the full absolute path Commented Nov 2, 2017 at 19:38
  • Hello. I tried and got this /bin/bash: /Users/atejada/Desktop/test.sh: Operation not permitted Commented Nov 2, 2017 at 19:40
  • Ok, so it got past the first problem. Not familiar with XCode. Check permissions on that file. Are you running this process as yourself? Commented Nov 2, 2017 at 19:41
  • Great. Yes myself. The script is executable. I can run it via terminal Commented Nov 2, 2017 at 19:44
  • Not sure what to do then. I'd update your question with the latest error message as it is now a different problem Commented Nov 2, 2017 at 19:48

2 Answers 2

2

Turn off Xcode sandbox mode, it will fix the issue

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. For anyone that has this problem too. Go to the build settings of your target. Make sure that you have "All" selected instead of "Basic" Type "entitlements" into the search box. The result is the build setting where you can specify where your entitlements file is located.
1
  func shell(_ args: String) -> String {
    var outstr = ""
    let task = Process()
    task.launchPath = "/bin/sh"
    task.arguments = ["-c", args]
    let pipe = Pipe()
    task.standardOutput = pipe
    task.launch()
    let data = pipe.fileHandleForReading.readDataToEndOfFile()
    if let output = NSString(data: data, encoding: String.Encoding.utf8.rawValue) {
        outstr = output as String
    }
    task.waitUntilExit()
    return outstr
}

This Function returns output of Bash Script you're trying to run

 let cmd = "for i in $(ifconfig -lu); do if ifconfig $i | grep -q \"status: active\" ; then echo $i; fi; done"

Above Code Demonstrate how to use it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.