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

smknappy

macrumors member
Original poster
Hi all,

I'm new to Mac dev and was wondering if there is an easy way to execute a command line program from an objective-c app.

Thanks!
 
Re: Calling a command line program in Objective-C

Originally posted by smknappy
Hi all,

I'm new to Mac dev and was wondering if there is an easy way to execute a command line program from an objective-c app.

Thanks!

To start you may want to join some number of Mac developer lists http://lists.apple.com/ . And second to actually do this is something like this:

#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>

int main()
{
pid_t pid = fork();
if(pid != 0)
{
// parent
waitpid(pid, &status, 0);
}
else
{
// child
execlp("grep", "grep", "foo", NULL);
}

return 0;
}

This will only run the command line tool, any output it makes will still be directed elsewhere, so if you need that you will need to dig a bit deeper.
 
if you search apple's developer connection, you can find a cocoa wrapper for the "mpg123" utility that will allow you to make a quick and easy mp3 player by calling the command line utility. you can just lift the code right from that example.

p.s. i think this is in the wrong section😉
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.