1

我无法使用 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 会失败。有什么建议吗?谢谢

4

2 回答 2

0

你至少应该检查返回的 eventDescriptor 和 errorDict 看看出了什么问题!

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

顺便说一句,我怀疑 bash 脚本没有正确处理路径中的空格。

于 2014-08-25T03:02:33.280 回答
0

NSApplescript 将星号视为空格,因此这假设有效:

NSString *badPath  = @"path with spaces"
NSString *goodPath = [badPath stringByReplacingOccurrencesOfString:@" " withString:@"*"]; //path*with*spaces
于 2019-07-08T10:04:14.023 回答