Reputation:
So im trying to run some basic terminal commands in my mac app and for some reason(s) I just can't to it.
Heres my code:
NSString *commitText = [commitMessage stringValue];
NSString *a = [NSString stringWithFormat:@"cd %@", dirPath];
NSString *c = [NSString stringWithFormat:@"usr/bin/git commit -m '%@'", commitText];
NSTask *aTask = [[NSTask alloc] init];
NSMutableArray *args = [NSMutableArray array];
[args addObject:@"-c"];
[args addObject:a];
[args addObject:@"git add *"];
[args addObject:c];
[args addObject:@"git push origin HEAD"];
NSPipe *pipe;
pipe = [NSPipe pipe];
[aTask setStandardOutput: pipe];
NSFileHandle *file;
file = [pipe fileHandleForReading];
[aTask setLaunchPath:@"bin/sh"];
[aTask setArguments:args];
[aTask launch];
Here are the errors:
2012-06-09 15:18:50.293 Auto Git[9404:403] launch path not accessible
2012-06-09 15:18:50.297 Auto Git[9404:403] (
0 CoreFoundation 0x00007fff870b4f56 __exceptionPreprocess + 198
1 libobjc.A.dylib 0x00007fff90e35d5e objc_exception_throw + 43
2 CoreFoundation 0x00007fff870b4d8a +[NSException raise:format:arguments:] + 106
3 CoreFoundation 0x00007fff870b4d14 +[NSException raise:format:] + 116
4 Foundation 0x00007fff9174f1f4 -[NSConcreteTask launchWithDictionary:] + 470
5 Auto Git 0x0000000109e1365d -[Push push:] + 765
6 CoreFoundation 0x00007fff870a470d -[NSObject performSelector:withObject:] + 61
7 AppKit 0x00007fff8e0f8f7e -[NSApplication sendAction:to:from:] + 139
8 AppKit 0x00007fff8e0f8eb2 -[NSControl sendAction:to:] + 88
9 AppKit 0x00007fff8e0f8ddd -[NSCell _sendActionFrom:] + 137
10 AppKit 0x00007fff8e0f82a0 -[NSCell trackMouse:inRect:ofView:untilMouseUp:] + 2014
11 AppKit 0x00007fff8e177fc4 -[NSButtonCell trackMouse:inRect:ofView:untilMouseUp:] + 489
12 AppKit 0x00007fff8e0f6eaa -[NSControl mouseDown:] + 786
13 AppKit 0x00007fff8e0c2348 -[NSWindow sendEvent:] + 6306
14 AppKit 0x00007fff8e05ba55 -[NSApplication sendEvent:] + 5593
15 AppKit 0x00007fff8dff20c6 -[NSApplication run] + 555
16 AppKit 0x00007fff8e26e244 NSApplicationMain + 867
17 Auto Git 0x0000000109e12eb2 main + 34
18 Auto Git 0x0000000109e12e84 start + 52
19 ??? 0x0000000000000003 0x0 + 3
)
What am I doing wrong? Please help. Thanks guys.
Upvotes: 0
Views: 2207
Reputation: 6325
The launch path should be /usr/bin/git
and your array should be @[@"commit",@"-m",yourarg]
You need to create a different task for each command. Your add, push need to be seperate tasks. To find the path of git, in the terminal run the command which git
This will be the path for your tasks.
NSTask* task = [[Task alloc] init]
//set the input output pipes
[task setLaunchPath:@"/usr/bin/git"];
NSArray* args = [NSArray arrayWithObjects:@"commit",@"-m",commitMessage];
[task setArguments:args];
//set up the notifications
[task launch];
If you want to use a shell script i think you would need to give it more formatting such as:
NSString* shScript = [NSString stringWithFormat:@"#!bin/sh\ngit commit -m %@\ngit add ...\ngit..."
set it up the same as above with the only argument in the array that string and the launch path /bin/sh
Upvotes: 1