【问题标题】:Commands with NSTask带有 NSTask 的命令
【发布时间】:2012-06-09 22:23:31
【问题描述】:

所以我试图在我的 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
)

我做错了什么?请帮忙。谢谢大家。

【问题讨论】:

  • 应该@"usr/bin/git commit -m '%@'"@"/usr/bin/git commit -m '%@'"

标签: objective-c macos


【解决方案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

【讨论】:

  • 好的,你能拿我的代码给我和例子,让我们说提交吗?我还是不太明白
  • 好的,我该如何使用哪个 git?
  • which git 仅用于查找启动路径。你在程序中并不需要它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-05-30
  • 1970-01-01
  • 1970-01-01
  • 2012-11-03
  • 2011-08-07
  • 2015-03-26
相关资源
最近更新 更多