简体   繁体   English

如何从python编写OS X Finder评论?

[英]How to write OS X Finder Comments from python?

I'm working on a python script that creates numerous images files based on a variety of inputs in OS X Yosemite.我正在开发一个 python 脚本,该脚本基于 OS X Yosemite 中的各种输入创建大量图像文件。 I am trying to write the inputs used to create each file as 'Finder comments' as each file is created so that IF the the output is visually interesting I can look at the specific input values that generated the file.我试图在创建每个文件时将用于创建每个文件的输入编写为“Finder 注释”,以便如果输出在视觉上很有趣,我可以查看生成文件的特定输入值。 I've verified that this can be done easily with apple script.我已经证实这可以通过苹果脚本轻松完成。

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

Afterward, upon selecting the file and showing its info (cmd+i) the Finder comments clearly display the expected text 'Val1, Val2, Val2'.之后,在选择文件并显示其信息 (cmd+i) 后,Finder 注释会清楚地显示预期的文本“Val1、Val2、Val2”。

This is further confirmed by running mdls [File/Path/Name] before and after the applescript is used which clearly shows the expected text has been properly added.通过在使用 applescript 之前和之后运行 mdls [File/Path/Name] 进一步证实了这一点,这清楚地显示了预期的文本已被正确添加。

The problem is I can't figure out how to incorporate this into my python script to save myself.问题是我不知道如何将它合并到我的 python 脚本中来拯救自己。

Im under the impression the solution should* be something to the effect of: VarList = [Var1, Var2, Var3] Fiele = [File/Path/Name] file.os.system.add(kMDItemFinderComment, VarList)我的印象是,解决方案应该* 达到以下效果: VarList = [Var1, Var2, Var3] Fiele = [File/Path/Name] file.os.system.add(kMDItemFinderComment, VarList)

As a side note I've also look at xattr -w [Attribute_Name] [Attribute_Value] [File/Path/Name] but found that though this will store the attribute, it is not stored in the desired location.作为旁注,我还查看了 xattr -w [Attribute_Name] [Attribute_Value] [File/Path/Name] 但发现虽然这将存储属性,但它并未存储在所需的位置。 Instead it ends up in an affiliated pList which is not what I'm after.相反,它最终出现在一个附属的 pList 中,这不是我所追求的。

After more digging, I was able to locate a python applescript bundle: https://pypi.python.org/pypi/py-applescript 经过更多挖掘之后,我能够找到一个python applescript包: https ://pypi.python.org/pypi/py-applescript

This got me to a workable answer, though I'd still prefer to do this natively in python if anyone has a better option? 这使我得到了一个可行的答案,尽管如果有人有更好的选择,我仍然愿意在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")

Here is my way to do that. 这是我的方法。

First you need to install applesctipt package using pip install applescript command. 首先,您需要使用pip install applescript applesctipt命令安装applesctipt软件包。

Here is the function that comments any file: 这是注释任何文件的功能:

def set_comment(file_path, comment_text):
    import applescript
    applescript.tell.app("Finder", f'set comment of (POSIX file "{file_path}" as alias) to "{comment_text}" as Unicode text')

and then I'm just using it like this: 然后我就这样使用它:

set_comment('/Users/UserAccountName/Pictures/IMG_6860.MOV', 'my comment')

This is the function to get comment of a file.这是获取文件注释的函数。

def get_comment(file_path):
    import applescript
    return applescript.tell.app("Finder", f'get comment of (POSIX file "{file_path}" as alias)').out
print(get_comment('Your Path'))

Another approach is to use appscript , a high-level Apple event bridge that is sadly no longer officially supported but still works (and saw an updated release in Jan. 2021 ).另一种方法是使用appscript ,这是一个高级 Apple 事件桥,遗憾的是不再受到官方支持但仍然有效(并且在 2021 年 1 月看到了更新版本)。 Here is an example of reading and setting the comment on a file:以下是读取和设置文件注释的示例:

import appscript
import mactypes

# Get a handle on the Finder.
finder = appscript.app('Finder')

# Tell Finder to select the file.
file = finder.items[mactypes.Alias("/path/to/a/file")]

# Print the current comment
comment = file.comment()
print("Current comment: " + comment)

# Set a new comment.
file.comment.set("New comment")

# Print the current comment again to verify.
comment = file.comment()
print("Current comment: " + comment)

Despite that the author of appscript recommends against using it in new projects, I used it recently to create a command-line utility called Urial for the specialized purpose of writing and updating URIs in Finder comments.尽管 appscript 的作者建议不要在新项目中使用它,但我最近使用它创建了一个名为Urial的命令行实用程序,专门用于在 Finder 注释中编写和更新 URI。 Perhaps its code can serve as an an additional example of using appscript to manipulate Finder comments.也许它的代码可以作为使用 appscript 操作 Finder 注释的附加示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM