0

我正在尝试在我的 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
)

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

4

2 回答 2

3

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

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


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

参数似乎是这样的:

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

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

于 2013-04-02T21:04:26.190 回答
1

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

这就是解决方案:

/** 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];
于 2013-04-02T21:22:05.007 回答