NSTask
last edited March 19, 2010 21:06:59 (98.176.135.74)
| |
|---|---|
| Edit / History / New / Search | Quick Links: Home / Recent Changes / Glossary / Jobs / Forums / Help |
|
Using NSTask, your program can run another program as a subprocess and can monitor that program’s execution. NSTask creates a separate executable entity; unlike NSThread, it does not share memory space with the parent process.
By default, a task inherits several characteristics of its parent's environment: the current directory, standard input, standard output, standard error, and the values of any environment variables. If you want to change any of these, e.g., the current directory, you must set a new value before you launch the task. A task’s environment is established once it has launched. An NSTask can only be run once. Subsequent attempts to run an NSTask raise an error.
If you run a task from a document in a document-based application, you should (at the very least) send the Other pages at CocoaDev related to using NSTask (and see particularly NSPipe and NSFileHandle):
To add a page to this list, simply put %%BEGINENTRY%%NSTask%%ENDENTRY%% somewhere on that page. You do not need to edit this page.
Example: Launching an NSTask (this is intended only as a simple example of launching a task, using the UNIX ls command, and passing arguments to it. Of course, if you just want to get a directory' contents, you should use NSFileManager. The usual way to redirect the standard input and output of the task is to use NSPipe and NSFileHandle, as shown below:
keywords: NSTask, NSFileHandle, NSPipe
For more, see the CocoaDev pages on NSPipe and NSFileHandle, and: http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSTask_Class/index.html http://developer.apple.com/documentation/Cocoa/Reference/Foundation/Classes/NSPipe_Class/index.html Tutorials that set up more sophisticated communication with the running task from within a Cocoa GUI interface include: Apple: Moriarity - http://developer.apple.com/samplecode/Moriarity/index.html Cocoadevcentral Tutorial: http://cocoadevcentral.com/articles/000025.php BookBuildingCocoaApplications presents an extended example developing GUI that communicates with an auxiliary task, as, I believe, does BookCoreMacOSXandUnix.
An NSTask will break Xcode's debug log entirely if you execute ANYTHING related with sh or bash (including scripts). printf, NSLog; all will cease to function as soon as the task has been launched. Even things like right clicking on an object in the debugger will yield nothing (straight GDB still prints though). The best way around this bug is to use a different shell, like zsh (which should be compatible with all your sh/bash commands).
Actually, I looked back in on the error again, after finding out that zsh was not the solution to my problem (my scripts still weren't working). I figured out that the problem lies with standard input, of all things. A quick fix for this would be to set your standard input up to something random, like a pipe, and do nothing with it.
| |
| Edit / History / New / Search | Quick Links: Home / Recent Changes / Glossary / Jobs / Forums / Help |