I have a MacRuby app, and after 开发者_如何转开发the app launches, I would like to launch a second process using an NSTask. The second process is a Ruby script bundled with the app. I would like to launch it using the MacRuby macruby interpreter that gets compiled into the app bundle. How can I do that?
First, remove the .rb extension from the ruby script, otherwise if you compile the macruby project using macruby_deploy, it will be compiled to rbo file. The script file should have this as its first line:
#!/usr/bin/env ruby
Make sure the script will be copied to Resources folder.
Then create and call a NSTask:
path = NSBundle.mainBundle.pathForResource('test', ofType:nil)
task = NSTask.alloc.init
task.setLaunchPath(path)
task.launch
Well, have you tried just calling NSTask?
NSTask.launchedTaskWithLaunchPath('script.rb', nil)
Then click around in Xcode to make sure that script.rb is in place during execution.
精彩评论