Kodeco Forums

NSTask Tutorial for OS X

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.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/1197-nstask-tutorial-for-os-x

“Note: The pipe character “|” on your keyboard is usually located on the forward slash \ key, just above the enter/return key.”

I think the \ key is the backslash key.

Thank you very much for this tutorial. Is there a way to make it build things other than Xcode projects?

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

Ok thank you. This tutorial helped me out a lot!

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

The file can be found below

Thank you in advance

Hi Jaco

The issue is that your jar files are added to your project but not to the bundle resources. You can correct this by

  1. selecting the project in the project navigator.
  2. select the Build Phases tab .
  3. expand the Copy Bundle Resources section.
  4. Add the jar files to the bundle using the + button.

Thanks for reading
Warren

Thank you so much for the speedy and accurate resolution of my problem!

I only need Xcode for a very simple frontend and to get my simulation apps to the AppStore. Your tutorials are the best!

Regards

Can you make it run files other than shell scripts like lua scripts?

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.

Happy scripting :]

Will this post update to Swift 3 version, it has big change in new release.

Hi - It has been scheduled to be updated in the next 7 days.

Seems NSTask had been change to Process in new Swift version. Please update this post either. Thanks.

Updated to Swift 3 - 02/10/16

1 Like

async method is deprecated?

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.

Its not a trivial exercise but a quick Google digs up Getting XPC to a Helper App Working with Sandboxing Enabled • Christian Tietze and of course the fantastic objc.io has XPC · objc.io

Good luck :]

Checking those now. Thank you!

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.