O'Reilly Hacks
oreilly.comO'Reilly NetworkSafari BookshelfConferences Sign In/My Account | View Cart   
Book List Learning Lab PDFs O'Reilly Gear Newsletters Press Room Jobs  


 
Buy the book!
iPod and iTunes Hacks
By Hadley Stern
October 2004
More Info

HACK
#77
Get a Track's Running Time and File Size
Get the total time and total size of selected tracks.
The Code
[Discuss (0) | Link to this hack]

The Code

This script sums the running time and size of either the selected tracks or the enabled tracks and displays the results in a dialog box:

   tell application "iTunes"

     set lib to view of front window
     set total_size to 0

     set total_duration to 0
     set dd_buttons to {"Cancel", "Enabled", "Selected"}

     -- target  selected or enabled tracks?
     if button returned of 
       (display dialog "Show total time and space of " & 
         "Selected or Enabled tracks in playlist \"" & (name of lib) & 
         "\":" buttons dd_buttons default button 2) is "Enabled" then
       set trackList to every track in lib whose enabled is true
     else
       set trackList to selection
     end if

     -- conglomerate each track's size and duration
     repeat with aTrack in trackList
       try
         set total_size to total_size + (get aTrack's size)
         set total_duration to total_duration + (get aTrack's duration)
       end try
     end repeat

     -- convert total bytes to megabytes
     set dd_size to (round total_size / 1024 / 1024 * 100 rounding up) / 100

     -- convert total seconds to hours, minutes, seconds
     set t_hour to 
       (round total_duration / 3600 rounding down)
     set t_min to 
       (round (total_duration / 60 - t_hour * 60) rounding down)
     set t_sec to 
       (round total_duration mod 60)
     set dd_time to t_hour & "h " & t_min & "m " & t_sec & "s"

     -- show totals
     display dialog "Total tracks: " & (count of trackList) & 
       return & return & "Total time: " & dd_time & 
       return & return & "Total size: " & dd_size & 
       "MB" buttons {"Thanks"} default button 1

   end tell

First, the script initializes a few variables, which include a list of strings used as button names in the upcoming display dialog and a reference to the selected playlist.

Next, the script puts up a dialog box asking whether you want to target the selected tracks or the enabled tracks (the ones that are checkmarked) in the selected playlist. If you select Enabled, the variable trackListwill be set to a list of every checkmarked track (every track whose enabledproperty is set to truehas its checkbox checked). Otherwise, trackListwill be set to a list of references to each selected track.

A repeatblock then sets the variable aTrackto each track reference in trackListon each loop. On each loop, the durationproperty (a track's time in seconds) and the sizeproperty (a track's size in bytes) of aTrackare successively added to the variables total_durationand total_size,respectively. When the repeat loop has finished, these two variables contain the total time in seconds and total size in bytes of all the tracks. Then the script converts the seconds to hours, minutes, and seconds and the bytes to megabytes.

Finally, the display dialog shows the results.


O'Reilly Home | Privacy Policy

© 2007 O'Reilly Media, Inc.
Website: | Customer Service: | Book issues:

All trademarks and registered trademarks appearing on oreilly.com are the property of their respective owners.