【问题标题】:Get spotlight comments from file using python使用python从文件中获取聚光灯评论
【发布时间】:2012-09-15 09:56:01
【问题描述】:

我正在尝试使用 python 获取聚光灯 cmets。我现在需要的只是让 popen 能够返回运行相同东西时 shell 通常会输出的任何内容。一旦我在 python 中有一个字符串,我就可以正确过滤它。

import sys, os, glob

paths = glob.glob("*.wav")
print paths

for soundFile in paths:
    #soundFile = os.path.abspath(soundFile)
    result = os.popen("xattr -p com.apple.metadata:kMDItemFinderComment "+soundFile+" | xxd -r -p |plutil -convert xml1 -o - -")
    print result

【问题讨论】:

    标签: python comments applescript spotlight


    【解决方案1】:

    我不知道在 Python 中相当于什么,但您可以使用 xattr 到 print the extended attribute as an XML property list

    #!/usr/bin/env ruby -KU
    
    require 'cgi'
    
    plist = `xattr -p com.apple.metadata:kMDItemFinderComment test.txt |
    xxd -r -p | plutil -convert xml1 -o - -`
    puts CGI.unescapeHTML(plist.scan(/<string>(.*?)<\/string>/m)[0][0])
    

    我忘了mdls -n kMDItemFinderComment。 Finder doesn't always store the comments in extended attributes 反正。

    【讨论】:

    • 这让我更接近,因为我可以将 xattr 放入 popen。但我不知道如何让 popen 返回 shell 在这种情况下通常会返回的内容。
    【解决方案2】:

    我知道几个月前有人问过这个问题,但这就是我如何通过 Python 和 popen 来关注 cmets。

    cmd = subprocess.Popen(['mdls', '-name', 'kMDItemFinderComment',pathtofile], stderr=subprocess.STDOUT,stdout = subprocess.PIPE )
    out,err = cmd.communicate()
    
    print out
    

    所以如果这对你很重要,它使用 mdls 而不是 xattr,但如果你告诉 Popen 将STDOUT 发送到哪里,然后使用.communicate(),你似乎可以继续使用你构建的 xattr 命令字符串。

    【讨论】:

    • 抱歉,文本似乎没有正确粘贴到答案中。在“subprocess.PIPE)”和“out,err = ...”之后应该有一个换行符
    猜你喜欢
    • 2015-06-09
    • 1970-01-01
    • 1970-01-01
    • 2018-04-10
    • 2017-03-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-09
    相关资源
    最近更新 更多