我在使用NSApplescript转义包含空格的路径时遇到了问题:
// 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会失败。有什么建议吗?谢谢
发布于 2014-08-25 11:02:33
您至少应该检查返回的eventDescriptor和errorDict,看看哪里出了问题!
NSLog(@"result: %@", eventDescriptor);
NSLog(@"errors: %@", errorDict);现在,我怀疑bash脚本没有正确处理路径中的空格。
发布于 2019-07-08 18:04:14
NSApplescript认为星号就是空格,所以这个假设是可行的:
NSString *badPath = @"path with spaces"
NSString *goodPath = [badPath stringByReplacingOccurrencesOfString:@" " withString:@"*"]; //path*with*spaceshttps://stackoverflow.com/questions/21829621
复制相似问题