A Tutorial on NSTask (Cocoa Development)

I recently started developing for Mac OSX (a recent transition from iOS). My task included me to run some script to do background checks and this is where I needed NSTask. Now the code available is fairly straight forward, a simple copy paste will work – however a bit of explanation is always helpful. Lets start with the full code snippet below:

[sourcecode language=”objc”]
NSTask *task;
task = [[NSTask alloc] init];
[task setLaunchPath: launchPath];
[task setArguments: arguments];
[task setCurrentDirectoryPath:dir];

NSPipe *pipe;
pipe = [NSPipe pipe];
[task setStandardOutput: pipe];

NSFileHandle *file;
file = [pipe fileHandleForReading];

[task launch];

NSData *data;
data = [file readDataToEndOfFile];
NSString *response =  [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
[/sourcecode]

The first bit is the NSTask instantiation, pretty obvious. However the next three properties need some explaining. The launch path is the path of the executable you want to run. Most of the executables can be found under /usr/bin, and most of the “system” commands like mv, cat and chmod can be found under /bin. The second parameter is the list of arguments (takes an NSArray). The list is literally the parameters you would type next to the command, eg. mv /usr/bin/ruby /bin/ruby will have the launch path as @”/bin/mv” and arguments as @[@”/usr/bin/ruby”,@”/bin/ruby”]. The third property sets the current working directory, which can be useful for certain applications which require to work in current directories. So if we were to set the current directory path to @”/user/bin” then we could just type in @”ruby” as the first parmeter (in the arguments).

Next block of code is the pipe instantiation. This allows the task to communicate back to the application. We simply instantiate a NSPipe and link it to the task.

We launch the task.

Finally, when the launch returns, the data is read from the file pointer and converted to an NSString.

Final point: Incase you are using sudo, pass the parameter -s for a terminal window asking for the password. However, the right way to do is to implement Authorization Services. That will probably be my next article.

Also read...

Comments

  1. I do believe all the ideas you have presented in your post. They are really convincing and will certainly work. Still, the posts are very quick for starters. May you please prolong them a little from next time? Thank you for the post.

    Reply
  2. Wonderful goods from you, man. I have understand your stuff previous to and you’re just extremely great. I really like what you’ve acquired here, certainly like what you are stating and the way in which you say it. You make it entertaining and you still care for to keep it wise. I can not wait to read far more from you. This is actually a tremendous website.

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *