1
votes

Sorry, english is not my first language, and I have a poor grammar skill.

Hi, I am a developer that is very new to Objective-C, and I have some problems with using NSTask. I have a .command file that I want to execute in this cocoa application, but if I use the "open" command using NSTask to execute the file, it launches terminal. Is there a way to just execute it without launching it, like a typical NSTask? Or can I just have a text file with the command to be executed? Thank You.

Here is my code...

NSString *pathtorb = [[NSBundle mainBundle] pathForResource:@"rightSpace" ofType:@"command"];

NSTask *DfileBlankSpace = [[NSTask alloc]init];
DfileBlankSpace.launchPath = @"/usr/bin/open";
DfileBlankSpace.arguments = @[pathtorb];
[DfileBlankSpace launch];
[DfileBlankSpace waitUntilExit];

NSTask *killDock = [[NSTask alloc]init];
killDock.launchPath = @"/usr/bin/killall";
killDock.arguments = @[@"Dock"];
[killDock launch];
[killDock waitUntilExit];
1
You are running a command application called 'open' to open a file at pathtorb.El Tomato

1 Answers

0
votes

A .command is just a shell script, instead of using open - which associates these files with Terminal - run sh passing it -c and the file pathname as arguments:

DfileBlankSpace.launchPath = @"/bin/sh";
DfileBlankSpace.arguments = @[@"-c", pathtorb];

HTH

Addendum

As @KenThomases as pointed out in the comments, if you have not escaped pathorb to make it a valid bash pathname then you should omit the @"-c" argument. This has the shell read from the file without parsing its file name as though typed on the command line.