如何以编程方式在文件和/或目录中添加注释? [英] How to add comments in a file and/or directory programmatically?

查看:194
本文介绍了如何以编程方式在文件和/或目录中添加注释?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图以编程方式在文件和目录中添加评论。

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

可以吗?如果是,请帮助我实现这一点。

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

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

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

推荐答案

我通过包装在Objective-C中的Apple Script实现了这一点:

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

这篇关于如何以编程方式在文件和/或目录中添加注释?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆