iTunes: Total Size of Current Playlist

itunesThis script, erm, duplicates the functionality of simply looking at the bottom of the iTunes window, where you’ll find the total size of the current playlist smugly peering up at you. So why did I write this? Your guess is as good as mine. However, in the name of providing examples for would-be iTunes scripters, I present it here in all its redundant glory.

Download Total-size-of-current-playlist.rb.zip.

The source code is cowering sheepishly around the corner.

#!/usr/bin/env ruby -w

# rb-appscript code by Ian Haigh : http://ianhaigh.com/
# Fri Dec  4 11:53:38 EST 2009

require 'rubygems'
require 'appscript'

class Numeric
  def to_human
    units = %w{B KB MB GB TB}
    e = (Math.log(self)/Math.log(1024)).floor
    s = "%.3f " % (to_f / 1024**e)
    s.sub(/\.?0*$/, units[e])
  end
end

app = Appscript.app("iTunes.app")

total = app.browser_windows.view.tracks.get[0].inject(0) { |total, t| total + t.size.get }

puts "#{total.to_human}"
This entry was posted in itunes. Bookmark the permalink. Trackbacks are closed, but you can post a comment.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

  • What’s this all about then?

    rb-appscript is a powerful way to automate your Mac. It's similar to Applescript but it's built in Ruby, which means you get to leverage that language's powerful syntax and libraries. Giving you more time to make toasted sandwiches. You want long-winded explanations? We got those.

    Please note that these scripts are by no means bulletproof, and are intended to provide examples of application scripting. I take no resposibility for any loss of data or mishaps; you download and run everything at your own risk. That said, I use all of these scripts in a production environment every day (well – more accurately, I use some of these scripts in a production environment most days).