从Cocoa Desktop应用程序执行终端命令

时间:2013-04-02 20:51:33

标签: objective-c macos nstask

我正在尝试执行一个命令,用于在我的Cocoa桌面应用程序中创建Cordova项目,但它不起作用。

这是我的代码:

NSTask *task = [NSTask new];
    [task setLaunchPath:@"/Documents/Cordova/bin/ ./create ~/Documents/Cordova/HelloWorld2 org.apache.cordova.HelloWorld2 HelloWorld2"];
    [task setArguments:[NSArray arrayWithObjects:@"-l", @"-a", @"-t", nil]];

    NSPipe *pipe = [NSPipe pipe];
    [task setStandardOutput:pipe];

    [task launch];

    NSData *data = [[pipe fileHandleForReading] readDataToEndOfFile];

    [task waitUntilExit];
   // [task release];

    NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog (@"got\n%@", string);

那是输出:

2013-04-02 23:48:18.968 Phonegap-ProjectCreator[2209:303] launch path not accessible
2013-04-02 23:48:18.970 Phonegap-ProjectCreator[2209:303] (
    0   CoreFoundation                      0x00007fff8b2b80a6 __exceptionPreprocess + 198
    1   libobjc.A.dylib                     0x00007fff88d7e3f0 objc_exception_throw + 43
    2   CoreFoundation                      0x00007fff8b2b7e7c +[NSException raise:format:] + 204
    3   Foundation                          0x00007fff8bd21cd2 -[NSConcreteTask launchWithDictionary:] + 409
    4   Phonegap-ProjectCreator             0x0000000100002a4a -[MainWindow createProject:] + 314
    5   AppKit                              0x00007fff94506a59 -[NSApplication sendAction:to:from:] + 342
    6   AppKit                              0x00007fff945068b7 -[NSControl sendAction:to:] + 85
    7   AppKit                              0x00007fff945067eb -[NSCell _sendActionFrom:] + 138
    8   AppKit                              0x00007fff94504cd3 -[NSCell trackMouse:inRect:ofView:untilMouseUp:] + 1855
    9   AppKit                              0x00007fff94504521 -[NSButtonCell trackMouse:inRect:ofView:untilMouseUp:] + 504
    10  AppKit                              0x00007fff94503c9c -[NSControl mouseDown:] + 820
    11  AppKit                              0x00007fff944fb60e -[NSWindow sendEvent:] + 6853
    12  AppKit                              0x00007fff944f7744 -[NSApplication sendEvent:] + 5761
    13  AppKit                              0x00007fff9440d2fa -[NSApplication run] + 636
    14  AppKit                              0x00007fff943b1cb6 NSApplicationMain + 869
    15  Phonegap-ProjectCreator             0x00000001000011d2 main + 34
    16  libdyld.dylib                       0x00007fff89ced7e1 start + 0
    17  ???                                 0x0000000000000003 0x0 + 3
)

我不明白是什么问题?它找不到执行路径或无法执行?

2 个答案:

答案 0 :(得分:3)

启动路径是要启动的路径,而不是要调用的命令行。它应该只包含要运行的可执行文件的路径。

arguments数组是应该传递的参数。你是混合两者。


您的可执行文件看起来像是“/ Documents / Cordova / bin / create”

参数似乎是:

 NSArray *args = @["-l", "~/Documents/Cordova/HelloWorld2", "-a", "org.apache.cordova.HelloWorld2", "-t", "HelloWorld2"];

即。参数具有值,并且值与arguments数组中的参数交错。

答案 1 :(得分:1)

我找到了解决方案。我是客观的新手,我意识到我犯了很多错误。

这就是解决方案:

/** Path to Application **/
NSString *path = @"/Users/username/Documents/Cordova/bin/create"; 

/** Arguments **/
NSArray *args = [NSArray arrayWithObjects:@"/Users/username/Documents/Cordova/HelloWorld3",@"org.apache.cordova.HelloWorld3",@"HelloWorld3", nil];
[[NSTask launchedTaskWithLaunchPath:path arguments:args] waitUntilExit];