How to add comments in a file and/or directory programmatically?

萝らか妹 提交于 2019-12-01 07:12:29

问题


I am trying to add comment in files and directory programmatically.

Is it possible to do? If yes, please help me to achieve this.

I looked for a direct API from Cocoa framework but no success, I will be happy to do this by any way (cocoa, shell or any scripts).


回答1:


I achieved this by Apple Script wrapped in Objective-C as :

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



回答2:


AFAIK there is no public API, if you google for Spotlight comments you will see various solutions which all are based on AppleEvents, Applescript or xattr.. like

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



来源:https://stackoverflow.com/questions/24548752/how-to-add-comments-in-a-file-and-or-directory-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!