我正在研究一个Python脚本,该脚本基于OS X Yosemite中的各种输入来创建大量图像文件。我正在尝试在创建每个文件时将用于创建每个文件的输入写为“查找器注释”,这样,如果该输出看起来很有趣,我就可以查看生成该文件的特定输入值。我已经证实可以使用Apple脚本轻松完成此操作。

tell application "Finder" to set comment of (POSIX file "/Users/mgarito/Desktop/Random_Pixel_Color/2015-01-03_14.04.21.png" as alias) to {Val1, Val2, Val3} as Unicode text

之后,在选择文件并显示其信息(cmd + i)时,Finder注释将清晰显示所需的文本“ Val1,Val2,Val2”。

通过在使用小程序之前和之后运行mdls [文件/路径/名称],可以进一步确认这一点,该程序清楚地表明已正确添加了预期的文本。

问题是我不知道如何将其合并到我的python脚本中以保存自己。

我印象中,解决方案应*具有以下效果:VarList = [Var1, Var2, Var3]Fiele = [File/Path/Name]file.os.system.add(kMDItemFinderComment, VarList)

作为旁注,我还查看了xattr -w [Attribute_Name] [Attribute_Value] [File / Path / Name],但发现尽管此属性将存储属性,但未将其存储在所需的位置。相反,它最终出现在关联的pList中,这不是我想要的。

最佳答案

经过更多的挖掘,我能够找到一个python applescript软件包:https://pypi.python.org/pypi/py-applescript

这使我得到了一个可行的答案,尽管如果有人有更好的选择,我仍然愿意在python中本地执行此操作?



import applescript

NewFile = '[File/Path/Name]' <br>
Comment = "Almost there.."

AddComment = applescript.AppleScript('''

    on run {arg1, arg2}
       tell application "Finder" to set comment of (POSIX file arg1 as alias) to arg2 as Unicode text
       return
    end run

''')

print(AddComment.run(NewFile, Comment))

print("Done")

10-05 18:10
查看更多