Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Oct 26, 2007, 02:32 AM   #1
goin
macrumors newbie
 
Join Date: Oct 2007
Problem with arguments with NSTask

Hi guys, I am fairly new to cocoa and objective-c but I wiggled my way through so far. But here is a one that I really need some help with:

I am trying to launch an application with NSTask and pass some arguments. Usually I do this from the terminal and a command would look like this:

render -start 1 -end 50 myFile.XY

The problem is that NSTask launches the application (render) in the desired application path but then only continues to work there with the filename (myFile.XY) and ignores all the flags that come after the command.
I have tried everything so far (from my point) and I would just like to kind of open terminal, write my command string and then execute. There must be some way of doing this, because I have seen it working in other apps.

My code looks like this so far:

NSTask *task;
task = [[NSTask alloc] init];
[task setLaunchPath: @"/usr/sbin/render"];

NSArray *arguments;
arguments = [NSArray arrayWithObjects: @"render -s 1 -e 2 redBall.ma", nil];
[task setArguments: arguments];
[task launch];


I appreciate any help or comments on this.

Cheers, Ingo
goin is offline   0 Reply With Quote
Old Oct 26, 2007, 02:48 AM   #2
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
1) Why are you passing the name of the command (render) as an argument to the command

2) Your array of arguments is not really an array of arguments is it? It's a single string. Have you tried:

Code:
[NSArray arrayWithObjects: @"-s",@"1",@"-e",@"2",@"redBall.ma", nil]
robbieduncan is offline   0 Reply With Quote
Old Oct 26, 2007, 03:36 AM   #3
goin
Thread Starter
macrumors newbie
 
Join Date: Oct 2007
1. that might have happend by accident here because I tried so many different version that this command slipped in there ;-)

2. well, it has to be a complete string otherwise I get the error from the render application that more than one filename is not allowed. I thought about this one too before but it actually didn't work out this way. So I suppose it has to be one complete string and not an array.
goin is offline   0 Reply With Quote
Old Oct 26, 2007, 03:47 AM   #4
robbieduncan
Moderator
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
Quote:
Originally Posted by goin View Post
2. well, it has to be a complete string otherwise I get the error from the render application that more than one filename is not allowed. I thought about this one too before but it actually didn't work out this way. So I suppose it has to be one complete string and not an array.
OK, that sounds like the render command is interpreting the argument switches are arguments wrongly when passed from NSTask. If you run it directly on the command line are you allowed spaces between the switches and arguments? I know isql for example does not allow this. Perhaps try

Code:
[NSArray arrayWithObjects: @"-s 1",@"-e 2",@"redBall.ma", nil]
or


Code:
[NSArray arrayWithObjects: @"-s1",@"-e2",@"redBall.ma", nil]
robbieduncan is offline   0 Reply With Quote
Old Oct 26, 2007, 04:05 AM   #5
goin
Thread Starter
macrumors newbie
 
Join Date: Oct 2007
No, there must be spaces all over the place, separating the flags from the values.

The following doesn't work, because the command sees different strings and interprets them as different files to process, but only one file is allowed to pass.

[NSArray arrayWithObjects: @"-s 1",@"-e 2",@"redBall.ma", nil]


Everything has to be in one string:

arguments = [NSArray arrayWithObjects: @"render -s 1 -e 2 redBall.mb", nil];

Then the process starts (even with the render command contained in the string) and completes but without paying attention to the flags. It's just behaving as I would be writing:


arguments = [NSArray arrayWithObjects: @"render redBall.mb", nil];

BTW: How do I get the nice code formatting here?
goin is offline   0 Reply With Quote
Old Oct 26, 2007, 07:55 AM   #6
kainjow
Moderator emeritus
 
kainjow's Avatar
 
Join Date: Jun 2000
I think you are misunderstanding how the Terminal and NSTask works.

If you have a command that you are entering into the Terminal exactly like so:

Code:
render -start 1 -end 50 myFile.XY
then "render" is the name of the script. The system will find where this program is located and launch it. "render" is just a shortcut.

Now when you enter your arguments as "-start 1 -end 50 myFile.XY", you are really entering 5 separate arguments that get passed into the script. Arguments start when you enter a space, ONLY if you are not within single or double quotes or have entered a backslash (e.g. "bob the builder", 'sally snow', Cat\ and\ dog). The script reads each argument from an array that gets passed into the program.

The Terminal takes your string of "render -start 1 -end 50 myFile.XY" and splits it up accordingly, so when you execute your NSTask, you need to do this all manually.

Therefore, when you use something like this:

Code:
[NSArray arrayWithObjects: @"render -s 1 -e 2 redBall.ma", nil]
this is the equivalent of typing into the Terminal:

Code:
render "render -s 1 -e 2 redBall.ma"
which won't work.

I think you need to try using exactly what robbieduncan suggested in his first post.

HTH

Quote:
Originally Posted by goin View Post
BTW: How do I get the nice code formatting here?
Put your code in between [ CODE ][ /CODE ] (but without the spaces)

Last edited by kainjow; Oct 26, 2007 at 08:06 AM.
kainjow is offline   0 Reply With Quote
Old Oct 26, 2007, 08:41 AM   #7
goin
Thread Starter
macrumors newbie
 
Join Date: Oct 2007
Ok, it works now. I did how both of you said and tried the version of separating everything, arguments and values. Now it passes on everything correctly and does what it is supposed to do. My mistake was that when I first tried that the fileName was referring to a different location and thus the command couldn't find the darn thing. After I changed that in the main app it seems to work now.

Thanks kainjow and robbieduncan for your help and patience. I guess there is a lot to learn. But on the other side I am just trying to get through it because writing programs is not my main subject and all I want to do is make my life a little easier... which sometimes gets pretty hard, eh? ;-)

Thanks guys, Ingo
goin is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Is it possible to run NSTask as user ? GokulKrish Mac Programming 1 Apr 12, 2014 09:07 AM
NSTask Not terminating. DavidBlack Mac Programming 5 Aug 3, 2013 09:07 PM
NSRegularExpression to capture C function arguments? ArtOfWarfare Mac Programming 13 Jun 17, 2013 11:17 PM
Accepting arguments of multiple types? moonman239 iPhone/iPad Programming 8 Jun 5, 2013 01:35 AM

Forum Jump

All times are GMT -5. The time now is 01:18 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC