I'm trying to build a GUI application for Mac OS X [El Capitan 10.11.1] using Swift [swift --version prints: Apple Swift version 1.2 (swiftlang-602.0.53.1 clang-602.0.53)], which is able to use my shell and ruby scripts.
The problem I'm having is that when I run a script (either .sh or .rb) using NSTask and NSBundle.mainBundle().pathForResource, the called script doesn't see required scripts from the same directory. It works when I'm using a hardcoded 'external' directory, but that's not an options since the scripts have to be a part of the final build.
I have also tried using relative paths such as './Contents/Resources/...' which didn't work.
(Relevant code below. I hope it's enough. Also, as additional information, I'm not a seasoned programmer and I'm rather new (roughly three months) to Mac, Bash & Ruby and very new (as in started this week) to Xcode 6.4, Swift & Cococa and have never used Objective-C, so apologies for poor style and/or very basic errors)
Swift Code:
class ShellTask {
var filePath: String
var arguments: [String]
init(filePath: String, arguments: [String]) {
self.filePath = filePath
self.arguments = arguments
}
func run() {
let task = NSTask()
task.launchPath = filePath
task.arguments = arguments as [String]
task.launch()
task.waitUntilExit()
}
}
class ViewController: NSViewController {
let shellTask = ShellTask(filePath: "", arguments: [""])
@IBAction func buttonDebug(sender: AnyObject) {
let mainBundle = NSBundle.mainBundle()
let resourceDataPath = mainBundle.pathForResource("test", ofType: ".sh")
shellTask.setFilePath(resourceDataPath!)
shellTask.run()
}
Shell/Ruby Code I'm currently using for testing (actual scripts are too much to post right now):
test.sh (entry-point):
#!/usr/bin/env bash
WORKDIR=$(dirname $0)
cd "$WORKDIR"
echo "from: test.sh"
ruby test1.rb
exit
test1.rb:
#!/usr/bin/env ruby
require './test2'
def test1()
puts "from: test1.rb"
end
test1()
test2()
test2.rb:
#!/usr/bin/env ruby
def test2()
puts "from: test2.rb"
end
Output when running test.sh from terminal:
<user>$ bash test.sh
from: test.sh
from: test1.rb
from: test2.rb
Output when running from Cocoa app:
ruby: No such file or directory -- test1.rb (LoadError)
I've been trying several things for the past two days and read multiple threads, but can't find a solution. But it works without problems when using hardcoded paths, such as '/User/user/Documents/Testfiles/test.sh'.
Also, it's ok if the Xcode terminal doesn't print anything from the test files, it just has to find them. For the actual program, the ruby scripts just have to use functions from modules in different files, which are needed in several scripts, so copy-pasting them in each of the called script would be very annoying later on, when changes have to be made. But surely, there has to be a way to get this here running :/
Thanks in advance
edit1: Using a relative path with NSFileManager.defaultManager().currentDirectoryPath and then adding the path to the resource folder ('/Contents/Resources/test.sh') gives error 'launch path not accessible'.