Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

enter image description here

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).

like image 766
Anoop Vaidya Avatar asked Jul 03 '14 08:07

Anoop Vaidya


1 Answers

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);
}
like image 93
Anoop Vaidya Avatar answered Nov 02 '22 06:11

Anoop Vaidya