首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在"do shell script..“中使用空格对路径进行NSApplescript转义

在"do shell script..“中使用空格对路径进行NSApplescript转义
EN

Stack Overflow用户
提问于 2014-02-17 20:51:01
回答 2查看 437关注 0票数 1

我在使用NSApplescript转义包含空格的路径时遇到了问题:

代码语言:javascript
复制
    // 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会失败。有什么建议吗?谢谢

EN

回答 2

Stack Overflow用户

发布于 2014-08-25 11:02:33

您至少应该检查返回的eventDescriptor和errorDict,看看哪里出了问题!

代码语言:javascript
复制
NSLog(@"result: %@", eventDescriptor);
NSLog(@"errors: %@", errorDict);

现在,我怀疑bash脚本没有正确处理路径中的空格。

票数 0
EN

Stack Overflow用户

发布于 2019-07-08 18:04:14

NSApplescript认为星号就是空格,所以这个假设是可行的:

代码语言:javascript
复制
NSString *badPath  = @"path with spaces"
NSString *goodPath = [badPath stringByReplacingOccurrencesOfString:@" " withString:@"*"]; //path*with*spaces
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21829621

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档