有什么办法通过Cocoa API或通过shell命令向文件添加标签?
解决方法 对不起,添加另一个答案,但与设置标签颜色相关的一个很长已经。这里是我用来设置用户标签的python脚本的摘录。它似乎工作,使事情可搜索,但不确定标签是否会正确显示。用法基本上是:tagfile.py "Tag name" fileOrFoldername
代码如下。
#! /usr/bin/env python# -*- Coding: utf-8 -*-""" Write Tags to fileUsage: tagfile.py "Tagname" filename1 filename2 You can use wildcards for the file name. Use quotes if spaces in Tags. To check if it worked,use xattr -l filename"""import sysimport subprocessdef writexattrs(F,TagList): """ writexattrs(F,TagList): writes the List of Tags to three xattr fIElds on a file-by file basis: "kMDItemFinderComment","_kMDItemUserTags","kMDItemOMUserTags Uses subprocess instead of xattr module. Slower but no dependencIEs""" Result = "" pListFront = '<!DOCTYPE pList PUBliC "-//Apple//DTD PList 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><pList version="1.0"><array>' pListEnd = '</array></pList>' pListTagstring = '' for Tag in TagList: pListTagstring = pListTagstring + '<string>{}</string>'.format(Tag.replace("'","-")) TagText = pListFront + pListTagstring + pListEnd OptionalTag = "com.apple.Metadata:" XattrList = ["kMDItemFinderComment","kMDItemOMUserTags"] for FIEld in XattrList: XattrCommand = 'xattr -w {0} \'{1}\' "{2}"'.format(OptionalTag + FIEld,TagText.encode("utf8"),F) if DEBUG: sys.stderr.write("XATTR: {}\n".format(XattrCommand)) ProcString = subprocess.check_output(XattrCommand,stderr=subprocess.STDOUT,shell=True) Result += ProcString return ResultDEBUG = Falseif __name__ == "__main__": if len(sys.argv) < 3: print __doc__ else: TagList = [ sys.argv[1] ] # print TagList # Or you can harDWire your Tags here # TagList = ['Orange','Green'] fileList = sys.argv[2:] for filename in fileList: writexattrs(filename,TagList)总结
以上是内存溢出为你收集整理的osx – 我如何以编程方式添加OS X“标签”到文件?全部内容,希望文章能够帮你解决osx – 我如何以编程方式添加OS X“标签”到文件?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)