2011-02-22 93 views
0

我有一個獨立的(第三方)應用程序,我嘗試使用命令「kick」啓動。我已經設置了我的〜/ .bash_profile和/ etc/bashrc文件,這樣我就可以在終端窗口中鍵入kick [command]並且完美地工作。所以我假設我已經正確設置了一切。試圖運行NSTask,但得到一個錯誤

問題出現在我嘗試使用NSTask時。

基本上我正在做的是創建兩個可變數組,kickBuild和kickOut。一個用來組裝命令(這是一串標誌),另一個用於NSTask。我使用kickBuild並將其轉換爲由空格分隔的字符串,然後將其作爲對象添加到第二個數組中。

所以我的命令看起來應該像「踢-i /path/to/input/file.ext -as 2 -g 2.2」等。而如果我輸入到一個終端窗口這個它的偉大工程。

kickBuild = [[NSMutableArray alloc] initWithCapacity:100]; 
kickOut = [[NSMutableArray alloc] initWithCapacity:2]; // Thinking that this should be "kick" and then "-i /path/to/input/file.ext -as 2 -g 2.2" 
kickPath = [kickLocationTextField stringValue]; // This is just the path to my kick executable. NOT /bin/sh. Is that correct? 
NSString *tempKick = [kickBuild componentsJoinedByString: @" "]; 
[kickOut addObject:tempKick]; 
[NSTask launchedTaskWithLaunchPath:kickPath arguments:kickOut]; 

當我建立我的應用程序,並運行此代碼,我得到這個錯誤...

使dyld:庫未加載:建立/ darwin_x86_64/gcc_opt_dynamic /核心/ libai.dylib

/Users/Gene/Kick/Kick-3.3.4.0/bin/kick

原因:從引用圖像沒有發現

這kickOut的NSLog的... 踢-i /Users/Gene/Test_Main.0001.ext -r 960 540 -as 2 -g 2.2 -v 5 -dp

這東西,我」米做錯了嗎?或者這是踢球的問題?

如何測試NSTask的一些基本的終端任務,以確保我正確使用NSTask?

kickBuild = [[NSMutableArray alloc] initWithCapacity:5]; 
kickOut = [[NSMutableArray alloc] initWithCapacity:2]; 
kickPath = @"/bin/sh"; 
[kickBuild addObject:@"-c"]; // Do I need this? 
[kickBuild addObject:@"ls"]; 
[kickBuild addObject:@"~/Desktop"]; 
NSString *tempKick = [kickBuild componentsJoinedByString: @" "]; 
[kickOut addObject:tempKick]; 
[NSTask launchedTaskWithLaunchPath:kickPath arguments:kickOut]; 

如果我運行這個沒有@ 「 - C」,我得到:/bin/sh的:LS〜/桌面:沒有這樣的文件或目錄

任何幫助表示讚賞。

謝謝百萬

回答

1

這裏有一個簡單的測試NSTask應該爲你工作(注:參數:(NSArray的*)參數):

/* 

gcc -Wall -O3 -x objective-c -fobjc-exceptions -framework Foundation -o nstask nstask.m 

./nstask 

http://stackoverflow.com/questions/5081846/trying-to-run-nstask-but-getting-an-error 


launchedTaskWithLaunchPath:arguments: 

Creates and launches a task with a specified executable and arguments. 

+ (NSTask *)launchedTaskWithLaunchPath:(NSString *)path arguments:(NSArray *)arguments 


*/ 

#import <Foundation/Foundation.h> 

int main(int argc, const char *argv[]) 
{ 

NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 

NSTask *task = [NSTask new]; 

NSMutableArray *kickBuild = [[NSMutableArray alloc] initWithCapacity:5]; 
//NSMutableArray *kickOut = [[NSMutableArray alloc] initWithCapacity:2]; 
NSString *kickPath = @"/bin/ls"; 

//[kickBuild addObject:@"/bin/ls"]; // Do I need this? 
[kickBuild addObject: [@"~/Desktop" stringByExpandingTildeInPath]]; 

task = [NSTask launchedTaskWithLaunchPath: kickPath arguments: kickBuild]; 
[task waitUntilExit]; 


[pool release]; 

return 0; 

} 
1

執行NSTask時,您的環境設置不會被讀取。

幾年前我問了this question

+0

感謝您的答覆Abizem。我知道〜/ .bash_profile只適用於交互式終端,但是不會/ etc/bashrc適用於所有內容?互動還是非互動? – crewshin 2011-02-22 18:57:17

+0

嘗試在〜/ .MacOSX/environment.plist中設置將被讀取的路徑。 – Abizern 2011-02-22 19:09:50

相關問題