我试图以编程方式在文件和目录中添加注释。
有可能吗?如果是,请帮助我实现这一点。
我从cocoa框架中寻找了一个直接的api,但是没有成功,我很乐意以任何方式(cocoa、shell或任何脚本)这样做。

最佳答案

我用objective-c包装的apple脚本实现了这一点:

 NSString *comment = @"hi boys";

NSOpenPanel *op = [NSOpenPanel new];
NSInteger answer = [op runModal];
if (answer == NSOKButton) {
    NSURL *url = [op URL];

    NSMutableString *appleScriptString = [NSMutableString new];
    [appleScriptString appendString:@"TELL APPLICATION \"FINDER\"\n"];

    NSString *setPath = [NSString stringWithFormat:@"SET filePath TO \"%@\" AS POSIX FILE \n", [url absoluteString]];
    [appleScriptString appendString:setPath];

    NSString *setComment = [NSString stringWithFormat:@"SET COMMENT OF (filePath AS ALIAS) TO \"%@\" \n", comment];
    [appleScriptString appendString:setComment];

    [appleScriptString appendString:@"END TELL"];

    NSAppleScript *commentorScript = [[NSAppleScript alloc] initWithSource:appleScriptString];
    NSDictionary *dictErr;
    [commentorScript executeAndReturnError:&dictErr];
    NSLog(@"Dict error = %@", dictErr);
}

关于objective-c - 如何以编程方式在文件和/或目录中添加注释?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24548752/

10-10 01:46