2014-02-17 76 views
0

我無法逃避使用NSApplescript包含空格的路徑:NSApplescript與空間逃逸路徑「做shell腳本。」

// mybashscript is in the bundle app (NSlog grant that is ok!) 

    NSDictionary*errorDict = nil; 

    NSAppleScript*mycommand; 
    NSString *mycommand = [mybashscript stringByReplacingOccurrencesOfString:@" " withString:@"\\ "]; 
    // NSString *mycommand = [[mybashscript stringByReplacingOccurrencesOfString:@" " withString:@"\\ "] stringByReplacingOccurrencesOfString:@"/" withString:@":"]; // another test made 

    mycommand = [[NSAppleScript alloc] initWithSource:[NSString stringWithFormat:@"do shell script \"%@\" with administrator privileges", escapedPath]]; 

    NSAppleEventDescriptor *eventDescriptor = [sudoPandoraMaker executeAndReturnError: &errorDict]; 


    if (([eventDescriptor descriptorType]) && (errorDict==nil)) { 
     // if error is nil....is ok.. not this case :-(
    } else { 
     NSLog(@"escapedPath è:%@", escapedPath); 
     // what's was wrong??? 
    } 

上面的代碼工作得很好,只有當應用程序是在做路徑不包含空格,但是當它移動到名稱中包含空格的文件夾或硬盤中時,NSAppleScript將失敗。任何建議?謝謝

回答

0

你應該至少檢查返回的eventDescriptor和errorDict,看看出了什麼問題!

NSLog(@"result: %@", eventDescriptor); 
NSLog(@"errors: %@", errorDict); 

非親愛的,我會懷疑bash腳本沒有正確處理路徑中的空格。

+0

感謝您的回覆,但正如您可以看到「事件錯誤處理程序」已設置,所以我已經建立的問題是具有空格的路徑很難解決(使用NSString)。 我解決了它的解決方法:應用程序必須在/應用程序文件夾中... – Mike97