【问题标题】:How to write OS X Finder Comments from python?如何从 python 编写 OS X Finder 评论?
【发布时间】:2015-01-03 20:27:38
【问题描述】:

我正在编写一个 python 脚本,该脚本根据 OS X Yosemite 中的各种输入创建大量图像文件。我试图在创建每个文件时将用于创建每个文件的输入写为“Finder cmets”,以便如果输出在视觉上很有趣,我可以查看生成文件的特定输入值。我已经验证这可以通过苹果脚本轻松完成。

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 cmets 会清楚地显示预期的文本“Val1、Val2、Val2”。

在使用苹果脚本之前和之后运行 mdls [File/Path/Name] 进一步证实了这一点,这清楚地表明预期的文本已被正确添加。

问题是我不知道如何将其合并到我的 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


    【解决方案1】:

    这是我的方法。

    首先你需要使用pip install applescript 命令安装applescript 包。

    这是一个将 cmets 添加到文件的函数:

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

    然后我只是这样使用它:

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

    【讨论】:

      【解决方案2】:

      经过更多挖掘,我找到了一个 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")
      

      【讨论】:

      • 比我做的好,就是用commandsosascript
      • 这行得通。您必须先安装 pyobjc(使用 pip3)并手动安装 applescript。不要在文件路径中包含方括号。
      【解决方案3】:

      这是获取文件评论的功能。

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

      【讨论】:

        【解决方案4】:

        另一种方法是使用appscript,这是一个高级 Apple 事件桥,遗憾的是,它不再受到官方支持但仍然有效(并且看到了 updated release in Jan. 2021)。以下是读取和设置文件注释的示例:

        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)
        
        

        尽管 appscript 的作者建议不要在新项目中使用它,但我最近使用它创建了一个名为 Urial 的命令行实用程序,专门用于在 Finder cmets 中编写和更新 URI。也许它的代码可以作为使用 appscript 操作 Finder cmets 的另一个示例。

        【讨论】:

          猜你喜欢
          • 2010-11-20
          • 1970-01-01
          • 2014-04-13
          • 1970-01-01
          • 2010-12-17
          • 1970-01-01
          • 1970-01-01
          • 2020-03-03
          • 2012-03-25
          相关资源
          最近更新 更多