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

Ano*_*dya 5 macos cocoa finder objective-c

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

在此处输入图片说明

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

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

Ano*_*dya 6

我通过封装在 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);
}
Run Code Online (Sandbox Code Playgroud)