Retired Document
Important: This document may not represent best practices for current development. Links to downloads and other resources may no longer be valid.
Creating and Launching an NSTask
There are two ways to create an NSTask
object. If it is sufficient for the task to run in the environment that it inherits from the process that creates it, use the class method launchedTaskWithLaunchPath:arguments:
. This method both creates and executes (launches) the task. If you need to change the task’s environment, create the task using alloc
and init
, use set...
methods to change parts of the environment, then use the launch
method to launch the task. For example, the following method runs tasks that take an input file and an output file as arguments. It reads these arguments, the task’s executable, and the current directory from text fields before it launches the task:
- (void)runTask:(id)sender |
{ |
NSTask *aTask = [[NSTask alloc] init]; |
NSMutableArray *args = [NSMutableArray array]; |
/* set arguments */ |
[args addObject:[[inputFile stringValue] lastPathComponent]]; |
[args addObject:[outputFile stringValue]]; |
[aTask setCurrentDirectoryPath:[[inputFile stringValue] |
stringByDeletingLastPathComponent]]; |
[aTask setLaunchPath:[taskField stringValue]]; |
[aTask setArguments:args]; |
[aTask launch]; |
} |
If you create an NSTask
object in this manner, you must be sure to set the executable name using setLaunchPath:
. If you don’t, an NSInvalidArgumentException
is raised.
Copyright © 2006 Apple Computer, Inc. All Rights Reserved. Terms of Use | Privacy Policy | Updated: 2006-04-04