2014-02-17 78 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