0

所以我试图在我的 mac 应用程序中运行一些基本的终端命令,但由于某种原因,我无法做到。

这是我的代码:

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];

以下是错误:

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
)

我究竟做错了什么?请帮忙。多谢你们。

4

1 回答 1

1

启动路径应该是/usr/bin/git并且您的阵列应该是@[@"commit",@"-m",yourarg]您需要为每个命令创建不同的任务。您的添加、推送需要是单独的任务。要找到 git 的路径,请在终端中运行命令which git这将是您的任务的路径。

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];

如果您想使用 shell 脚本,我认为您需要为其提供更多格式,例如:

NSString* shScript = [NSString stringWithFormat:@"#!bin/sh\ngit commit -m %@\ngit add ...\ngit..."

将其设置为与上面相同的数组中的唯一参数该字符串和启动路径 /bin/sh

于 2012-06-10T02:04:08.870 回答