In this OS X NSTask tutorial, learn how to execute another program on your machine as a subprocess and monitor its execution state while your main program continues to run.
The script that this tutorial runs is centred around building an Xcode project but the general technique of using an NSTask to launch a command line instruction can be used for anything you would do in Terminal
Thank you this tutorial helped me out a lot, but everything is still not working as it should… so as a last resort I simple took your completed file, changed the BuildScript.command with my command line code, added my bunch of Java files to the project…and…still get an error. When I run the BuildScript.command everything works 100% but running it in Xcode it does not work. Please can you check it out for me, it is probably a simple referencing error
Sure - NSTask is just a programatic interface to anything you might do at the command line.
If you take a look at the docs for NSTask you can see theres the launchPath property which is how you define the executable e.g /usr/local/bin/lua and the arguments property which are passed in as [String] e.g [“-foo”,“-bar”,“/path/to/myscript.lua”]
I haven’t used lua myself so took a guess but you can find the actual binary path once you have installed it using which lua at the command line.
The tutorial shows how you’d do this with a shell script but the general principles would apply to any command line task.
At the end of your tutorial you talk about using XPC services as a mean for sandboxing such an app but really, is it even possible to sandbox an app that calls terminal command-lines and have it successfully be validated for the MAS?
Yes its possible. I haven’t done it it for myself yet but a quick browse through the App Store digs up a few apps that wouldnt be possible without using XPC calls to the system. e.g CodeRunner2.
Thanks again for the links but after some reading I’m now more confused than before. XPC services are meant to separate critical parts of an app into sandboxed mini-apps which communicate securely. Aren’t they? I still don’t see how making an XPC service which contains code to run a command-line utility (say for exemple: pmset sleepnow) can be possible? What kind of entitlements would such an app have?
Or, did I misinterpret?
Please, can you read my last comment and tell me what you think? I honestly am a little bit confused right now about sandboxing a shell command. Take as an exemple: pmset.