I want to run a simple command from my cocoa app through code, NOT creating a shell script and running it that way, but by running it through the application, being able to define everything and change it on the fly
Thanks a bunch!!
Matt S.
2009-12-01 23:07:06
thats very stange hmmm...
streetparade
2009-12-01 23:12:18
+3
A:
The Function
void runSystemCommand(NSString *cmd)
{
[[NSTask launchedTaskWithLaunchPath:@"/bin/sh"
arguments:[NSArray arrayWithObjects:@"-c", cmd, nil]]
waitUntilExit];
}
usage example:
#import <Foundation/Foundation.h>
void runSystemCommand(NSString *cmd)
{
[[NSTask launchedTaskWithLaunchPath:@"/bin/sh"
arguments:[NSArray arrayWithObjects:@"-c", cmd, nil]]
waitUntilExit];
}
int main(int argc, const char **argv)
{
NSAutoreleasePool *pool;
pool = [NSAutoreleasePool new];
runSystemCommand(@"ls");
[pool release];
return 0;
}
streetparade
2009-12-01 22:54:47
+1
A:
the answer streetparade gave will not work most of the time, refer to this article
Madcapslaugh
2010-03-11 21:29:45
That doesn't directly address streetparade's answer. What's wrong with the answer (aside from the general folly of using the shell to run commands instead of just running them yourself)?
Peter Hosey
2010-03-13 08:59:27