I'm working with Ionic which has a command line interface. I would like to create a small Mac app that helps to execute certain commands.
In the terminal I cd Users/me/Desktop/Repos/ionic-project/myApp
After changing I would e.g. ionic run
NSTask *task = [[NSTask alloc]init];
task.launchPath = @"/bin/bash";
task.currentDirectoryPath = @"cd Users/me/Desktop/Repos/ionic-tryouts/myApp";
task.arguments = @[@"ionic run"];
[task launch];
But this gives me 'working directory doesn't exist.' I've already read quite some threads here on SO. Whats my mistake?
EDIT:
Thanks Christik for the detailed answer. I now have this code:
NSTask *task = [[NSTask alloc]init];
task.launchPath = @"/usr/local/bin/ionic";
task.currentDirectoryPath = @"/Users/me/ionic-tryouts/myApp";
task.arguments = @[@"run"];
[task launch];
Now I get the following error: env: node: No such file or directory
. I guess this comes from a problem, that node.js isn't found (ionic is build on top of node). I found this issue - a missing symlink with the right name might be the cause. But setting the symlink for node didn't help. Any ideas?
EDIT2: I gave Christik the correct answer, even though I couldn't finally get it to work. I'm still investigation. Maybe it's the node installation thats wrong.
EDIT3: I saw some posts that mentioned that it might be better, if node was re-installed by homebrew since homebrew installs it without sudo. Short comment: didn't help
If you want to use bash
to launch ionic
, the equivalent command that you'll need to execute is /bin/bash -c ionic run
, thus you'll need to change the following:
cd
from currentDirectoryPath
(that's probably a typo from a copy+paste from terminal) and add a leading /
to have an absolute patharguments
to @[@"-c",@"ionic",@"run"]
, as each argument to NSTask should represent an item of the array.If you have problems with /usr/bin/bash
due to some other tools not being found, you can try using /bin/sh
.
Update As Aditya Vaidyam pointed out here you might also need to setup the environment variables (especially the PATH
one), to simulate the same conditions as for the terminal. If you want to find out which environment variables are set, just run the env
command.
On top of what Cristik suggested, you need to add to the environment variables (i.e. PATH), the location of node.js, unless it's already in /usr/bin/ or so.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With