如何以编程方式在文件和/或目录中添加注释?

时间:2014-07-03 08:33:15

标签: objective-c macos cocoa finder

我正在尝试以编程方式在文件和目录中添加注释。

enter image description here

有可能吗?如果是,请帮助我实现这一目标。

我从Cocoa框架中寻找了一个直接的API,但没有成功,我很乐意以任何方式(可可,shell或任何脚本)这样做。

2 个答案:

答案 0 :(得分:4)

我用Objective-C包装的Apple Script实现了这个目标:

 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);
}

答案 1 :(得分:1)

AFAIK没有公共API,如果你谷歌搜索Spotlight评论,你会看到各种解决方案,所有解决方案都基于AppleEvents,Applescript或xattr ..喜欢

http://www.cocoabuilder.com/archive/cocoa/178663-writing-spotlight-comments.html http://www.cocoabuilder.com/archive/cocoa/305493-adding-spotlight-comment-data-to-folder-file.html http://cocoadev.com/SpotlightAndTagging