title: using mdls command to generate a metadata report
body: <p>All,<br>
  I have shell script which goes through all the media (audio,video) files in a user-supplied folder and creates a nice text based metadata report. I'm on a <strong>Mac OS X</strong> and I'm using Spotlight's <code>mdls</code> command to get all the relevant metadata. The issue is that sometimes I see "null" results in my report for all the media files. If I run the script again it seems to work. I'm confused why this is happening. This is part of the script that outputs the metadata report: </p>
  
  <pre><code>cd "path_to_folder"
  while IFS= read -r -d $'\0' file; do
    duration=`mdls -name kMDItemDurationSeconds "$file" | cut -d "=" -f 2 `
    duration=`printf "%.2f" $duration;`
    pixel_height=`mdls -name kMDItemPixelHeight "$file" | cut -d "=" -f 2`
    pixel_width=`mdls -name kMDItemPixelWidth "$file" | cut -d "=" -f 2`
    codec=`mdls -name kMDItemCodecs "$file" | cut -d "=" -f 2`
    temp="$i) [$file]\n- Duration: $duration\n- Dimensions: $pixel_width X $pixel_height pixels\n- Codec: $codec\n"
    metaDataOutput=$metaDataOutput"\n"$temp
    i=$((i + 1))
  done &lt; &lt;(find .  \( -iname \*.m4v -o -iname \*.mov -o -iname \*.mp3 -o -iname \*.m4r -o -iname \*.m4a \)  -print0 )
  
  echo -e  "\n[Report]\n"$metaDataOutput  
  </code></pre>
  
  <p>The expected output is like this:</p>
  
  <p>1) <strong>[./test1.mov]</strong><br>
  - Duration: 22.03<br>
  - Dimensions: 480 X 640 pixels<br>
  - Codec: ( "H.264" )  </p>
  
  <p>2) <strong>[./test2.mov]</strong><br>
  - Duration: 25.03<br>
  - Dimensions: 480 X 640 pixels<br>
  - Codec: ( "H.264" )   </p>
  
  <p>But sometimes the output is null for all the media files:</p>
  
  <p>1) <strong>[./test1.mov]</strong><br>
  - Duration: null<br>
  - Dimensions: null X null pixels<br>
  - Codec: ( null ) </p>
  
  <p>2) <strong>[./test2.mov]</strong><br>
  - Duration: null<br>
  - Dimensions: null X null pixels<br>
  - Codec: ( null ) </p>
  
  <p>Am I missing a trick here? Why does the script work sometimes and sometimes outputs null?</p>