One of the nice things in OS X since the introducion of Spotlight is that we now have
some command line tools to look at metadata. The command mdls will return all the
metadata for a file and we can use it in various ways.
Let's have a look at a PNG file.
(If you want to follow along and run the commands go to the Cell menu above and select All Output > Clear.)
In [1]:
mdData = !mdls images/dashboard_files_tab.png
In [2]:
mdData
Out[2]:
We can also use straight Python to do the same thing, though it looks a little more complex.
In [10]:
import subprocess
p = subprocess.Popen(['mdls', 'images/dashboard_files_tab.png'], stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
mdString, err = p.communicate()
mdData = mdString.split('\n')
In [11]:
mdData
Out[11]:
The first thing you will notice is that everything starts kMDItem and after that
is what you might call the "real name". Notice that what we have is a list of strings so we can easily extract one.
In [12]:
match = [s for s in mdData if 'PixelHeight' in s]
In [13]:
match
Out[13]:
This makes match a list of strings, in this case only one. Notice that match[0] is
actually valid Python code to declare the variable kMDItemPixelHeight so we can
just run the string and end up with a usable variable.
In [14]:
exec match[0]
In [15]:
kMDItemPixelHeight
Out[15]:
Now do the same for the width and name.
In [17]:
match = [s for s in mdData if 'PixelWidth' in s]
exec match[0]
match = [s for s in mdData if 'FSName' in s]
exec match[0]
And we can print a nice string with the name and size of the picture.
In [18]:
print kMDItemFSName + '\t' + str(kMDItemPixelHeight) +'x' + str(kMDItemPixelWidth)
We could easily turn this into a function. If we turn it into a function we could also add some error checking.
In [27]:
import subprocess
def printSize(fname):
# run mdls
p = subprocess.Popen(['mdls', fname], stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
mdString, err = p.communicate()
if err:
return "Could not open " + fname
# the above returns the output as a single string with '\n'
# between output lines. Let's make it a list of lines
mdData = mdString.split('\n')
# All the above is the equivalent of the IPython `mData = !mdls fileName`
# get the height
match = [s for s in mdData if 'PixelHeight' in s]
if not match:
return fname + " Is Not Picture"
exec match[0]
# get the width
match = [s for s in mdData if 'PixelWidth' in s]
if not match:
return fname + " Is Not Picture"
exec match[0]
# get the name
match = [s for s in mdData if 'FSName' in s]
if not match:
return fname + " File Name Error"
exec match[0]
return kMDItemFSName + ' ' +str(kMDItemPixelHeight) +'x' + str(kMDItemPixelWidth)
In [28]:
printSize('images/dashboard_running_tab.png')
Out[28]:
Now let's try some errors.
In [29]:
printSize('Index.ipynb')
Out[29]:
In [33]:
printSize('I_dont_exist.txt')
Out[33]:
In [36]:
book = !mdls photoshop1pdf.pdf
In [37]:
book
Out[37]:
In [38]:
other = !mdls On_Basilisk_Station.mobi
In [39]:
other
Out[39]:
In [40]:
candid = !mdls GoingCandid.pdf
In [41]:
candid
Out[41]: