Question

I'm trying to get spotlight comments with python. All I need now is the ability for popen to return whatever the shell would normally output if running the same thing. once i have a string in python then I can filter it properly.

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
Was it helpful?

Solution

I don't know what the equivalent of this would be in Python, but you can use xattr to 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])

I forgot about mdls -n kMDItemFinderComment. Finder doesn't always store the comments in extended attributes anyway.

OTHER TIPS

I know this question was asked several months ago, but this is how I get to spotlight comments via Python & popen.

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

print out

So it's using mdls as opposed to xattr if that matters to you, but it seems like you could continue using the xattr command string you built, if you just tell Popen where to send STDOUT and then use .communicate().

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top