1

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];
eshin2020
  • 35
  • 4

1 Answers1

0

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.

CRD
  • 52,522
  • 5
  • 70
  • 86
  • Probably better to leave off the `-c` and just pass the file path as the script to run. For example, the file might not be an executable or might not start with a shebang (`#!`) line, in which case it's not a proper command for the `-c` option. – Ken Thomases Dec 17 '14 at 07:22
  • 1
    On thinking about it some more, I'm going to upgrade my warning about `-c` to: you really must not use it. Without `-c` the argument is unambiguously interpreted as a path to a file from which to read commands. With `-c`, the argument is interpreted as a command, just as if it were typed at a shell. That means that it will be broken up into "words" at spaces, which could mean that it fails to run the specified file. It could even mean it runs a different file. Much worse, special characters like `$` will be interpreted. If the path is `/tmp/foo$(rm -rf ~)bar`, you'll delete the home dir. – Ken Thomases Dec 18 '14 at 03:28
  • @KenThomases - I thought about such things, but the OP is using a path from within their own bundle not user input or anything so considered it would over complicate the issue. The app author couldn't do anything "tricky" here they couldn't do directly in the app. However as you say omitting the -c should avoid the problem so is a better way to go. – CRD Dec 18 '14 at 03:38
  • It's not so much about the app author doing something tricky. It's about an accident. For example, the app bundle could be placed (by the user) in a folder with a space or a dollar sign in the name. That would mean that the path for any item in the bundle has that in the path. – Ken Thomases Dec 18 '14 at 06:04