簡體   English   中英

從Cocoa Desktop應用執行終端命令

[英]Execute a terminal command from a Cocoa Desktop app

我試圖在我的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
)

我不明白是什么問題? 它找不到執行路徑或無法執行?

啟動路徑是要啟動的路徑,而不是要調用的命令行。 它只應包含要運行的可執行文件的路徑。

arguments數組是應傳遞的參數。 您正在混合兩者。


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

參數似乎是這樣的:

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

即,參數具有值,並且值與arguments數組中的參數交織。

我找到了解決方案。 我是目標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];

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM