• Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

5. AppleScript for iTunes > 77. Get a Track’s Running Time and File Size

Get a Track’s Running Time and File Size

Get the total time and total size of selected tracks.

It’s easy to get the total running time and size of a playlist in iTunes; it appears right along the bottom of the main window, beneath the tracks. If you only want to know about a selection of tracks, though, you’d better break out the calculator—or use this simple script that will display the total time and combined size of just the selected or enabled (checkmarked) tracks.

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

PREVIEW

                                                                          

Not a subscriber?

Start A Free Trial


  
  • Creative Edge
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint