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. By Warren Burton.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Your Mac has a fully-fledged version of UNIX as its operating system, which means it has a massive amount of pre-installed command line utilities and scripting languages. Swift, Perl, Python, Bash, Ruby, plus anything you can install. Wouldn’t it be nice to use to that power to create awesome apps?
NSTask
allows you to execute another program on your machine as a subprocess and monitor its execution state while your main program continues to run. For example, you could run the ls
command to display a directory listing — from right inside your app!
A good analogy for NSTask
is a parent-child relationship. A parent can create a child and tell it to do certain things, and (theoretically) the child must obey. NSTask
behaves in a similar fashion; you start a “child” program, give it instructions and tell it where to report any output or errors. Better yet — it’s way more obedient than your average toddler :]
A great use for NSTask
is to provide a front-end GUI to command line programs. Command line programs are powerful, but they require you to remember exactly where they live on your machine, how to call them and what options or arguments you can provide to the program. Adding a GUI on the front end can provide the user with a great deal of control — without having to be a command line guru!
This tutorial includes an NSTask
example that shows you how to execute a simple command program with arguments and display its standard output as it runs in a text field. By the end, you’ll be ready to use NSTasks in your own apps!
Note: This tutorial assumes you have some basic familiarity with Mac OS X development and the Terminal. If you are completely new to programming for the Mac, check out our beginning Mac OS X development tutorial series.
Note: This tutorial assumes you have some basic familiarity with Mac OS X development and the Terminal. If you are completely new to programming for the Mac, check out our beginning Mac OS X development tutorial series.
Getting Started
To keep the focus squarely on NSTask, I’ve created a starter project for you that contains a basic user interface. Download the project, open it in Xcode, and build and run.
The starter app has one window, as shown:
This window has the title “TasksProject”. It has a simple GUI that will, by invoking a shell script, let you build an iOS project, create an ipa
and observe what is happening.
Creating Your First NSTask
The NSTask
example will be to build and package an iOS app into an ipa
file by using NSTask
to run some command line tools in the background. Most of the basic UI functionality is in place — your job is do the heavy lifting with NSTask
.
Note: It’s recommended that you have an iOS Developer account with Apple, as you’ll need the proper certificates and provisioning profile to create an ipa
file that can be installed on one of your devices. Don’t worry if you don’t, though, as you’ll be able to follow the entire tutorial even without an account.
Note: It’s recommended that you have an iOS Developer account with Apple, as you’ll need the proper certificates and provisioning profile to create an ipa
file that can be installed on one of your devices. Don’t worry if you don’t, though, as you’ll be able to follow the entire tutorial even without an account.
You are now going to work on the embedded View Controller titled “Tasks View Controller”. The first section in the window asks the user to select an Xcode project directory. To save time, rather than having to select a directory manually every time you run this app while testing, you’ll hard-code it to one of your own Xcode project directories.
To do this, head back to Xcode and open TasksViewController.swift. Take a look at the properties and methods under the comment “Window Outlets”:
//View Controller Outlets
@IBOutlet var outputText:NSTextView!
@IBOutlet var spinner:NSProgressIndicator!
@IBOutlet var projectPath:NSPathControl!
@IBOutlet var repoPath:NSPathControl!
@IBOutlet var buildButton:NSButton!
@IBOutlet var targetName:NSTextField!
All of these properties correspond to the Tasks View Controller Scene in Main.storyboard. Notice the projectPath
property — this is the one you want to change.
Open Main.storyboard and click on the Project Location item. You’ll find it 4 levels deep in the object hierarchy; it’s a child of the Stack View. In the Attributes Inspector
, under Path Control, find the Path element:
Set Path to a directory on your machine that contains an iOS project. Make sure you use the parent directory of a project, not the .xcodeproj
file itself.
Note: If you don’t have any iOS projects on your machine, download a simple project here and unzip it to a location on your machine. Then set the Path
property in your application using the instructions above. For example, if you unzip the package on your desktop, you would set Path
to
.
/Users/YOUR_USERNAME_HERE/Desktop/SuperDuperApp
Note: If you don’t have any iOS projects on your machine, download a simple project here and unzip it to a location on your machine. Then set the Path
property in your application using the instructions above. For example, if you unzip the package on your desktop, you would set Path
to
.
/Users/YOUR_USERNAME_HERE/Desktop/SuperDuperApp
Now that you have a default source project path in your app to facilitate testing, you will also need a default destination path. Open Main.storyboard and click on the Build Repository item.
In the Attributes Inspector, find Path item under Path Control:
Set the Path entry to a directory on your machine that’s easy to find, like the Desktop. This is where the .ipa
file created by this application will be placed.
There are two additional fields in the Tasks View Controller Scene
you need to know about: the Target Name and an associated text field.
-
Target Name is designated for the name of the iOS
Target
you want to build. - The text area below Target Name will display output from the
NSTask
object in your project as it runs.
Don’t know the target name of your iOS project? To find it, select your project in Xcode’s project navigator and look under TARGETS in the Info tab. The screenshot below shows where to find this for a sample project called “SuperDuperApp”:
Remember the target name — you’ll be entering it into the running app later on.
Let’s start fleshing out the bits of code that will run when the “Build” button is pressed.