Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

goin

macrumors newbie
Original poster
Oct 26, 2007
7
0
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
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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]
 

goin

macrumors newbie
Original poster
Oct 26, 2007
7
0
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.
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
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]
 

goin

macrumors newbie
Original poster
Oct 26, 2007
7
0
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?
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
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

BTW: How do I get the nice code formatting here?

Put your code in between [ CODE ][ /CODE ] (but without the spaces)
 

goin

macrumors newbie
Original poster
Oct 26, 2007
7
0
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.