# NSTask Tutorial for OS X

**URL:** <https://forums.kodeco.com/t/nstask-tutorial-for-os-x/13767>\
**Category:** 🎓 Courses & Articles\
**Created:** [April 22, 2016, 1:03pm UTC](https://forums.kodeco.com/t/nstask-tutorial-for-os-x/13767 "2016-04-22T13:03:26Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![warrenburton](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/warrenburton/32/100024_2.png) [@warrenburton](https://forums.kodeco.com/u/warrenburton)\
**Post date:** [April 22, 2016, 1:03pm UTC](https://forums.kodeco.com/t/nstask-tutorial-for-os-x/13767/1 "2016-04-22T13:03:27Z")

</div>

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](https://www.raywenderlich.com/1197-nstask-tutorial-for-os-x)

---

<div class="post-metadata">

**Author:** ![karlostj](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/karlostj/32/110937_2.png) [@karlostj](https://forums.kodeco.com/u/karlostj)\
**Post date:** [April 22, 2016, 6:24pm UTC](https://forums.kodeco.com/t/nstask-tutorial-for-os-x/13767/2 "2016-04-22T18:24:24Z")

</div>

“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.

---

<div class="post-metadata">

**Author:** ![stick](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/stick/32/56543_2.png) [@stick](https://forums.kodeco.com/u/stick)\
**Post date:** [July 6, 2016, 12:58am UTC](https://forums.kodeco.com/t/nstask-tutorial-for-os-x/13767/3 "2016-07-06T00:58:22Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![warrenburton](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/warrenburton/32/100024_2.png) [@warrenburton](https://forums.kodeco.com/u/warrenburton)\
**Post date:** [July 6, 2016, 5:25am UTC](https://forums.kodeco.com/t/nstask-tutorial-for-os-x/13767/4 "2016-07-06T05:25:31Z")

</div>

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**

---

<div class="post-metadata">

**Author:** ![stick](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/stick/32/56543_2.png) [@stick](https://forums.kodeco.com/u/stick)\
**Post date:** [July 6, 2016, 5:10pm UTC](https://forums.kodeco.com/t/nstask-tutorial-for-os-x/13767/5 "2016-07-06T17:10:30Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jacobenvosloo](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jacobenvosloo/32/36561_2.png) [@jacobenvosloo](https://forums.kodeco.com/u/jacobenvosloo)\
**Post date:** [July 8, 2016, 2:57pm UTC](https://forums.kodeco.com/t/nstask-tutorial-for-os-x/13767/6 "2016-07-08T14:57:22Z")

</div>

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

> **[Dropbox - File Deleted](https://www.dropbox.com/s/zk1cqq16697vq8p/TasksProject-completed%20JBV.zip?dl=0)**
>
> Dropbox is a free service that lets you bring your photos, docs, and videos anywhere and share them easily. Never email yourself a file again!

Thank you in advance

---

<div class="post-metadata">

**Author:** ![warrenburton](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/warrenburton/32/100024_2.png) [@warrenburton](https://forums.kodeco.com/u/warrenburton)\
**Post date:** [July 8, 2016, 8:46pm UTC](https://forums.kodeco.com/t/nstask-tutorial-for-os-x/13767/7 "2016-07-08T20:46:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![jacobenvosloo](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jacobenvosloo/32/36561_2.png) [@jacobenvosloo](https://forums.kodeco.com/u/jacobenvosloo)\
**Post date:** [July 9, 2016, 5:36am UTC](https://forums.kodeco.com/t/nstask-tutorial-for-os-x/13767/8 "2016-07-09T05:36:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![stick](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/stick/32/56543_2.png) [@stick](https://forums.kodeco.com/u/stick)\
**Post date:** [September 19, 2016, 5:53pm UTC](https://forums.kodeco.com/t/nstask-tutorial-for-os-x/13767/9 "2016-09-19T17:53:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![warrenburton](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/warrenburton/32/100024_2.png) [@warrenburton](https://forums.kodeco.com/u/warrenburton)\
**Post date:** [September 20, 2016, 5:40am UTC](https://forums.kodeco.com/t/nstask-tutorial-for-os-x/13767/10 "2016-09-20T05:40:55Z")

</div>

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 :]

---

<div class="post-metadata">

**Author:** ![acca](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/acca/32/102075_2.png) [@acca](https://forums.kodeco.com/u/acca)\
**Post date:** [September 30, 2016, 6:33am UTC](https://forums.kodeco.com/t/nstask-tutorial-for-os-x/13767/11 "2016-09-30T06:33:11Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![warrenburton](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/warrenburton/32/100024_2.png) [@warrenburton](https://forums.kodeco.com/u/warrenburton)\
**Post date:** [September 30, 2016, 7:50am UTC](https://forums.kodeco.com/t/nstask-tutorial-for-os-x/13767/12 "2016-09-30T07:50:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![acca](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/acca/32/102075_2.png) [@acca](https://forums.kodeco.com/u/acca)\
**Post date:** [September 30, 2016, 9:26am UTC](https://forums.kodeco.com/t/nstask-tutorial-for-os-x/13767/13 "2016-09-30T09:26:12Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![warrenburton](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/warrenburton/32/100024_2.png) [@warrenburton](https://forums.kodeco.com/u/warrenburton)\
**Post date:** [October 2, 2016, 11:16am UTC](https://forums.kodeco.com/t/nstask-tutorial-for-os-x/13767/14 "2016-10-02T11:16:16Z")

</div>

Updated to Swift 3 - 02/10/16

---

<div class="post-metadata">

**Author:** ![acca](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/acca/32/102075_2.png) [@acca](https://forums.kodeco.com/u/acca)\
**Post date:** [October 5, 2016, 11:28am UTC](https://forums.kodeco.com/t/nstask-tutorial-for-os-x/13767/15 "2016-10-05T11:28:06Z")

</div>

async method is deprecated?

 ![](https://raywenderlich-com-v3-forums.s3.amazonaws.com/original/3X/c/0/c0baf3e89c930a3c8f65ac6d9a33b5e4a0cc20ea.png)

---

<div class="post-metadata">

**Author:** ![altares](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/altares/32/46866_2.png) [@altares](https://forums.kodeco.com/u/altares)\
**Post date:** [November 8, 2016, 5:09pm UTC](https://forums.kodeco.com/t/nstask-tutorial-for-os-x/13767/16 "2016-11-08T17:09:44Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![warrenburton](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/warrenburton/32/100024_2.png) [@warrenburton](https://forums.kodeco.com/u/warrenburton)\
**Post date:** [November 8, 2016, 10:27pm UTC](https://forums.kodeco.com/t/nstask-tutorial-for-os-x/13767/17 "2016-11-08T22:27:47Z")

</div>

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](https://christiantietze.de/posts/2015/01/xpc-helper-sandboxing-mac/) and of course the fantastic [objc.io](http://objc.io) has [XPC · objc.io](https://www.objc.io/issues/14-mac/xpc/)

Good luck :]

---

<div class="post-metadata">

**Author:** ![altares](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/altares/32/46866_2.png) [@altares](https://forums.kodeco.com/u/altares)\
**Post date:** [November 8, 2016, 10:42pm UTC](https://forums.kodeco.com/t/nstask-tutorial-for-os-x/13767/18 "2016-11-08T22:42:48Z")

</div>

Checking those now. Thank you!

---

<div class="post-metadata">

**Author:** ![altares](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/altares/32/46866_2.png) [@altares](https://forums.kodeco.com/u/altares)\
**Post date:** [November 9, 2016, 2:37am UTC](https://forums.kodeco.com/t/nstask-tutorial-for-os-x/13767/19 "2016-11-09T02:37:10Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![altares](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/altares/32/46866_2.png) [@altares](https://forums.kodeco.com/u/altares)\
**Post date:** [November 14, 2016, 6:24pm UTC](https://forums.kodeco.com/t/nstask-tutorial-for-os-x/13767/20 "2016-11-14T18:24:08Z")

</div>

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.

[Next page](https://forums.kodeco.com/t/nstask-tutorial-for-os-x/13767.md?page=2)
