Controlling iTunes with Ruby

In working on Ruby Scripting Leopard, I’ve been helped by searching for AppleScript examples, such as those at Doug’s AppleScripts for iTunes. To pay it forward, I offer an example to some future searcher after the jump. Since WordPress (or something) is turning quotes into “smart” quotes, follow this link if you want plain text.

This should work on stock Leopard.

I run it via launchd, the replacement for cron(1). Lingon is a convenient way to create a launchd configuration file.

#!/usr/bin/env ruby

# Run to create a playlist named "today’s tunes". The playlist is 
# populated with tracks selected because the first character of their
# names begins with a randomly selected letter or digit. For example, 
# my list today begins like this:
#  Galang
#  Gamma Ray
#  Gary Stomp
#  Gear

require osx/cocoa
include OSX
OSX.require_framework ScriptingBridge

PLAYLIST_NAME = "today’s tunes"

def main
  todays_tunes = make_empty_playlist
  tracks = tracks_beginning_with(some_random_character, all_songs)
  tracks.each do | track |
    track.duplicateTo(todays_tunes)
  end
end

def make_empty_playlist
  ensure_no_playlist(PLAYLIST_NAME)
  p = ITunesPlaylist.alloc.initWithProperties({’name => PLAYLIST_NAME})
  # TODO: why does following fail?
  # LIBRARY.userPlaylists.addObject(p)
  LIBRARY.userPlaylists.insertObject_atIndex(p,0)
  p.shuffle = true
  p
end

def tracks_beginning_with(letter, tracks)
  tracks.find_all do | track |
    name = if starts_with_qualifier?(track.name)
             phrase_tail(track.name)
           else
             track.name
           end
    /^#{letter}/i =~ name
  end
end

def ensure_no_playlist(name)
  p = LIBRARY.playlists.objectWithName(name)
  p.delete if p.exists == 1
end

def all_songs
  LIBRARY.playlists.objectWithName(’Library‘).tracks
end

def some_random_character
  possibilities = (’a‘..’z‘).to_a + (’0‘..’9‘).to_a
  possibilities[rand(possibilities.length)]
end

# Works with either a NSCFString or a string.
# TODO: Change tests to indicate that?
def starts_with_qualifier?(name)
  name =~ /^thes/i || name =~ /^ans/i || name =~ /^as/i
end

def phrase_tail(name)
  name.gsub(/^w+s+/, ‘)
end

def bapp(bundle_name)
  SBApplication.applicationWithBundleIdentifier(bundle_name)
end

ITUNES = bapp(’com.apple.iTunes‘)
LIBRARY = ITUNES.sources.objectWithName(’Library‘)

if $0 == __FILE__
  main
end

One Response to “Controlling iTunes with Ruby”

  1. Philip Woeber Says:

    FYI: I have done a lot of ruby scripting on microsoft with Watir for testing Time Warner Telecom webapps. Before I retired from TWTC, I converted three of my fellow testers to Ruby from Perl. Now I do iTunes for me and Excel for my CPA daughter’s business. I want to use rb-appscript for iTunes and Excel. I copied your text version (see it below), saved it as createPlaylist.rb, ran it to create a playlist named “today’s tunes” from textmate (command-R) and got results below. I still have a lot to learn about my MacPro. Am also delving into RubyCocoa. I will keep on hacking. I love Ruby. I am 68 now. I am a refugee from COBOL, FORTRAN and Sperry aka Unisys mainframes. I have an O’Reilly Safari subscription and bought your RubyCocoa book and roughcut. I also have hard copy of Everyday Scripting that I am working through now. Very instructive. I didn’t know how to use “collect”.

    RubyMate
    Ruby
    Theme:
    copy output
    RubyMate r8136 running Ruby r1.8.6 (/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby)
    >>> createPlaylist.rb

    ruby: unknown type name “tdta”.
    /Users/pwoeber/ruby/rb-appscripts/createPlaylist.rb:64:in `starts_with_qualifier?’: ‘NSString#=~’ doesn’t work correctly. Because it returns byte indexes. Please use ‘String#=~’ instead.
    /Users/pwoeber/ruby/rb-appscripts/createPlaylist.rb:64:in `starts_with_qualifier?’: ‘NSString#=~’ doesn’t work correctly. Because it returns byte indexes. Please use ‘String#=~’ instead.
    /Users/pwoeber/ruby/rb-appscripts/createPlaylist.rb:64:in `starts_with_qualifier?’: ‘NSString#=~’ doesn’t work correctly. Because it returns byte indexes. Please use ‘String#=~’ instead.
    /Users/pwoeber/ruby/rb-appscripts/createPlaylist.rb:64:in `starts_with_qualifier?’: ‘NSString#=~’ doesn’t work correctly. Because it returns byte indexes. Please use ‘String#=~’ instead.
    /Users/pwoeber/ruby/rb-appscripts/createPlaylist.rb:64:in `starts_with_qualifier?’: ‘NSString#=~’ doesn’t work correctly. Because it returns byte indexes. Please use ‘String#=~’ instead.
    /Users/pwoeber/ruby/rb-appscripts/createPlaylist.rb:64:in `starts_with_qualifier?’: ‘NSString#=~’ doesn’t work correctly. Because it returns byte indexes. Please use ‘String#=~’ instead.
    /Users/pwoeber/ruby/rb-appscripts/createPlaylist.rb:64:in `starts_with_qualifier?’: ‘NSString#=~’ doesn’t work correctly. Because it returns byte indexes. Please use ‘String#=~’ instead.
    /Users/pwoeber/ruby/rb-appscripts/createPlaylist.rb:64:in `starts_with_qualifier?’: ‘NSString#=~’ doesn’t work correctly. Because it returns byte indexes. Please use ‘String#=~’ instead.
    /Users/pwoeber/ruby/rb-appscripts/createPlaylist.rb:64:in `starts_with_qualifier?’: ‘NSString#=~’ doesn’t work correctly. Because it returns byte indexes. Please use ‘String#=~’ instead.
    /Users/pwoeber/ruby/rb-appscripts/createPlaylist.rb:64:in `starts_with_qualifier?’: ‘NSString#=~’ doesn’t work correctly. Because it returns byte indexes. Please use ‘String#=~’ instead.
    /Users/pwoeber/ruby/rb-appscripts/createPlaylist.rb:64:in `starts_with_qualifier?’: ‘NSString#=~’ doesn’t work correctly. Because it returns byte indexes. Please use ‘String#=~’ instead.
    /Users/pwoeber/ruby/rb-appscripts/createPlaylist.rb:64:in `starts_with_qualifier?’: ‘NSString#=~’ doesn’t work correctly. Because it returns byte indexes. Please use ‘String#=~’ instead.
    /Users/pwoeber/ruby/rb-appscripts/createPlaylist.rb:64:in `starts_with_qualifier?’: ‘NSString#=~’ doesn’t work correctly. Because it returns byte indexes. Please use ‘String#=~’ instead.
    /Users/pwoeber/ruby/rb-appscripts/createPlaylist.rb:64:in `starts_with_qualifier?’: ‘NSString#=~’ doesn’t work correctly. Because it returns byte indexes. Please use ‘String#=~’ instead.
    /Users/pwoeber/ruby/rb-appscripts/createPlaylist.rb:64:in `starts_with_qualifier?’: ‘NSString#=~’ doesn’t work correctly. Because it returns byte indexes. Please use ‘String#=~’ instead.
    /Users/pwoeber/ruby/rb-appscripts/createPlaylist.rb:64:in `starts_with_qualifier?’: ‘NSString#=~’ doesn’t work correctly. Because it returns byte indexes. Please use ‘String#=~’ instead.
    /Users/pwoeber/ruby/rb-appscripts/createPlaylist.rb:64:in `starts_with_qualifier?’: ‘NSString#=~’ doesn’t work correctly. Because it returns byte indexes. Please use ‘String#=~’ instead.
    /Users/pwoeber/ruby/rb-appscripts/createPlaylist.rb:64:in `starts_with_qualifier?’: ‘NSString#=~’ doesn’t work correctly. Because it returns byte indexes. Please use ‘String#=~’ instead.
    /Users/pwoeber/ruby/rb-appscripts/createPlaylist.rb:64:in `starts_with_qualifier?’: ‘NSString#=~’ doesn’t work correctly. Because it returns byte indexes. Please use ‘String#=~’ instead.
    /Users/pwoeber/ruby/rb-appscripts/createPlaylist.rb:64:in `starts_with_qualifier?’: ‘NSString#=~’ doesn’t work correctly. Because it returns byte indexes. Please use ‘String#=~’ instead.
    /Users/pwoeber/ruby/rb-appscripts/createPlaylist.rb:64:in `starts_with_qualifier?’: ‘NSString#=~’ doesn’t work correctly. Because it returns byte indexes. Please use ‘String#=~’ instead.
    /Users/pwoeber/ruby/rb-appscripts/createPlaylist.rb:64:in `starts_with_qualifier?’: ‘NSString#=~’ doesn’t work correctly. Because it returns byte indexes. Please use ‘String#=~’ instead.
    /Users/pwoeber/ruby/rb-appscripts/createPlaylist.rb:64:in `starts_with_qualifier?’: ‘NSString#=~’ doesn’t work correctly. Because it returns byte indexes. Please use ‘String#=~’ instead.
    /Users/pwoeber/ruby/rb-appscripts/createPlaylist.rb:64:in `starts_with_qualifier?’: ‘NSString#=~’ doesn’t work correctly. Because it returns byte indexes. Please use ‘String#=~’ instead.
    /Users/pwoeber/ruby/rb-appscripts/createPlaylist.rb:64:in `starts_with_qualifier?’: ‘NSString#=~’ doesn’t work correctly. Because it returns byte indexes. Please use ‘String#=~’ instead.
    /Users/pwoeber/ruby/rb-appscripts/createPlaylist.rb:64:in `starts_with_qualifier?’: ‘NSString#=~’ doesn’t work correctly. Because it returns byte indexes. Please use ‘String#=~’ instead.
    /Users/pwoeber/ruby/rb-appscripts/createPlaylist.rb:64:in `starts_with_qualifier?’: ‘NSString#=~’ doesn’t work correctly. Because it returns byte indexes. Please use ‘String#=~’ instead.
    /Users/pwoeber/ruby/rb-appscripts/createPlaylist.rb:64:in `starts_with_qualifier?’: ‘NSString#=~’ doesn’t work correctly. Because it returns byte indexes. Please use ‘String#=~’ instead.
    /Users/pwoeber/ruby/rb-appscripts/createPlaylist.rb:64:in `starts_with_qualifier?’: ‘NSString#=~’ doesn’t work correctly. Because it returns byte indexes. Please use ‘String#=~’ instead.
    /Users/pwoeber/ruby/rb-appscripts/createPlaylist.rb:64:in `starts_with_qualifier?’: ‘NSString#=~’ doesn’t work correctly. Because it returns byte indexes. Please use ‘String#=~’ instead.
    /Users/pwoeber/ruby/rb-appscripts/createPlaylist.rb:64:in `starts_with_qualifier?’: ‘NSString#=~’ doesn’t work correctly. Because it returns byte indexes. Please use ‘String#=~’ instead.
    /Users/pwoeber/ruby/rb-appscripts/createPlaylist.rb:64:in `starts_with_qualifier?’: ‘NSString#=~’ doesn’t work correctly. Because it returns byte indexes. Please use ‘String#=~’ instead.
    /Users/pwoeber/ruby/rb-appscripts/createPlaylist.rb:64:in `starts_with_qualifier?’: ‘NSString#=~’ doesn’t work correctly. Because it returns byte indexes. Please use ‘String#=~’ instead.
    /Users/pwoeber/ruby/rb-appscripts/createPlaylist.rb:64:in `starts_with_qualifier?’: ‘NSString#=~’ doesn’t work correctly. Because it returns byte indexes. Please use ‘String#=~’ instead.
    /Users/pwoeber/ruby/rb-appscripts/createPlaylist.rb:64:in `starts_with_qualifier?’: ‘NSString#=~’ doesn’t work correctly. Because it returns byte indexes. Please use ‘String#=~’ instead.
    /Users/pwoeber/ruby/rb-appscripts/createPlaylist.rb:64:in `starts_with_qualifier?’: ‘NSString#=~’ doesn’t work correctly. Because it returns byte indexes. Please use ‘String#=~’ instead.
    /Users/pwoeber/ruby/rb-appscripts/createPlaylist.rb:64:in `starts_with_qualifier?’: ‘NSString#=~’ doesn’t work correctly. Because it returns byte indexes. Please use ‘String#=~’ instead.
    /Users/pwoeber/ruby/rb-appscripts/createPlaylist.rb:64:in `starts_with_qualifier?’: ‘NSString#=~’ doesn’t work correctly. Because it returns byte indexes. Please use ‘String#=~’ instead.
    Program exited.

    Here is the script:
    #!/usr/bin/env ruby

    # Run to create a playlist named “today’s tunes”. The playlist is
    # populated with tracks selected because the first character of their
    # names begins with a randomly selected letter or digit. For example,
    # my list today begins like this:
    # Galang
    # Gamma Ray
    # Gary Stomp
    # Gear

    require ‘osx/cocoa’
    include OSX
    OSX.require_framework ‘ScriptingBridge’

    PLAYLIST_NAME = “today’s tunes”

    def main
    todays_tunes = make_empty_playlist
    tracks = tracks_beginning_with(some_random_character, all_songs)
    tracks.each do | track |
    track.duplicateTo(todays_tunes)
    end
    end

    def make_empty_playlist
    ensure_no_playlist(PLAYLIST_NAME)
    p = ITunesPlaylist.alloc.initWithProperties({’name’ => PLAYLIST_NAME})
    # TODO: why does following fail?
    # LIBRARY.userPlaylists.addObject(p)
    LIBRARY.userPlaylists.insertObject_atIndex(p,0)
    p.shuffle = true
    p
    end

    def tracks_beginning_with(letter, tracks)
    tracks.find_all do | track |
    name = if starts_with_qualifier?(track.name)
    phrase_tail(track.name)
    else
    track.name
    end
    /^#{letter}/i =~ name
    end
    end

    def ensure_no_playlist(name)
    p = LIBRARY.playlists.objectWithName(name)
    p.delete if p.exists == 1
    end

    def all_songs
    LIBRARY.playlists.objectWithName(’Library’).tracks
    end

    def some_random_character
    possibilities = (’a’..’z').to_a + (’0′..’9′).to_a
    possibilities[rand(possibilities.length)]
    end

    # Works with either a NSCFString or a string.
    # TODO: Change tests to indicate that?
    def starts_with_qualifier?(name)
    name =~ /^the\s/i || name =~ /^an\s/i || name =~ /^a\s/i
    end

    def phrase_tail(name)
    name.gsub(/^\w+\s+/, ”)
    end

    def bapp(bundle_name)
    SBApplication.applicationWithBundleIdentifier(bundle_name)
    end

    ITUNES = bapp(’com.apple.iTunes’)
    LIBRARY = ITUNES.sources.objectWithName(’Library’)

    if $0 == __FILE__
    main
    end

Leave a Reply

You must be logged in to post a comment.


  • Buy Cheapest fluoxetine cod Online Best Online. Buy Medications Online.
  • Buy Cheapest diazepam generics Now Cheap Online Pharmacy. WorldWide Shipping.
  • Buy Cheapest trazodone for sleep Online Best Drugstore. Free Viagra Pills!
  • Buy Cheapest does cialis work Online Best Internet. Top Online Pharmacy.
  • Buy Cheapest viagra generic Online Cheap Prescription Drugs. Best Internet.
  • Buy Cheap clomid get pregnant Now Cheap Pharmacy Online. Cheap Online Pharmacy.
  • Buy Cheapest diet pills for weight loss Now No Prescription Needed. Best Drugstore.
  • Buy Cheapest codeine buy Online Order Cheap Meds Without Rx. Low Prices.
  • Buy Cheap purchase xenical Online Discount Pharmacy Online. Best Prices.
  • Buy Cheapest viagra in mexico Online Drugs, Health And Beauty. Best Internet.
  • Buy Cheap buy cheap generic viagra Now Free Viagra Pills! Guaranteed Shipping.
  • Buy Cheapest discount weight loss suppliments Now Best Prices. No Prescription Needed.
  • Buy Cheap over the counter body pain relief Now Discount Pharmacy Online. Best Drugstore.
  • Buy Cheap levitra levitria Online Cheap Pharmacy Online. Guaranteed Shipping.
  • Buy Cheap natural viagra alternatives Now Pharmacy Store. Order Cheap Meds Without Rx.
  • Buy Cheapest very cheap phentermine Now WorldWide Shipping. Cheap Pharmacy Online.
  • Buy Cheap generics for valium Now Guaranteed Shipping. Discount Online Pharmacy.
  • Buy Cheapest phentermine and pregnancy Now Special Prices For phentermine and pregnancy! Pharmacy Store.
  • Buy Cheap clomid and multiple births Now 100% Satisfaction Guaranteed. Low Prices.
  • Buy Cheap tramadol recreational Now 100% Satisfaction Guaranteed. Low Prices.
  • Buy Cheap levitra facts Online 100% Satisfaction Guaranteed. Best Online.
  • Buy Cheap ativan information Online Cheap Pharmacy Online. Guaranteed Shipping.
  • Buy Cheap cialis mail order medication Online Free Viagra Pills! WorldWide Shipping.
  • Buy Cheapest buy hair loss medicine stop Now Best Prices. Order Cheap Meds Without Rx.
  • Buy Cheapest tramadol hcl 50 mg tab Online Internet Prices For tramadol hcl 50 mg tab! Low Prices.
  • Buy Cheapest acne medication pills Now Discount Online Pharmacy. Low Prices.
  • Buy Cheapest cheapest ultram Now Online Medical Shop. Cheap Pharmacy Online.
  • Buy Cheap buy phentermine diet pill Now Drugs, Health And Beauty. Online Medical Shop.
  • Buying Cheap real phentermine. Offshore Pharmacy, Good Prices. Best Internet.
  • Buy Cheap viagra buy india Online Cheap Prescription Drugs. Best Internet.
  • Buy Cheap pain medicine online Now Order Cheap Meds Without Rx. Best Prices.
  • Buy Cheap drugs on line Now All Medications Are Certificated! Best Prices.
  • Buy Cheap viagra super active Online Cheap Online Pharmacy. Pharmacy Store.
  • Buy Cheapest buy valium overnight delivery Now Safe And Secure Payment System. Low Prices.
  • Buy Cheapest cialis 30 oral Online Best Drugstore. Free Viagra Pills!
  • Buy Cheapest nonprescription pain killer Now Best Prices. Buy Medications Online.
  • Buy Cheapest weight loss probiotics Now Online Medical Shop. WorldWide Shipping.
  • Buy Cheap cialis levitra viagra Now 24/Internet)(safe Pharmacy. Best Drugstore.
  • Buy Cheap fluconazole 150mg Now Best Drugstore. Internet Prices For fluconazole 150mg!
  • Buy Cheapest phentermine online directory Online Best Internet. Top Online Pharmacy.
  • Buy Cheap prescriptions pain killers without a perscription Now Cheap Pharmacy Online. 24/Online Pharmacy.
  • Buy Cheapest xanax generics Online Cheap Prescription Drugs. Best Internet.
  • Buy Cheap fat weight loss products Online Low Prices. Order Cheap Meds Without Rx.
  • xanax usa Online Without Prescription Best Prices. Best Internet.
  • Buy Cheapest multiple prescriptions of xanax Now Best Drugstore. No Prescription Needed.
  • Buy Cheap where to buy valium Now The Largest Internet Pharmacy. Best Prices.
  • Buy diflucan pill Without Prescription Doctor. Best Prices. Best Internet.
  • Buy Cheap anti depression tablets Online No Prescription Needed. Best Drugstore.
  • Buy Cheapest order amoxicillin Now Cheap Pharmacy Online. WorldWide Shipping.
  • Buy Cheap 100mg tramadol Now Cheap Prescription Drugs. Best Drugstore.
  • Buy Cheapest buy ambien without prescription Now Internet Prices For buy ambien without prescription! Best Online.
  • Buy Cheapest does buspar work Now Pharmacy Store. Online Medical Shop.
  • Buy Cheap impotence levitra Online WorldWide Shipping. Guaranteed Shipping.
  • Buy Cheapest ambien long term effects Now No Prescription Needed. WorldWide Shipping.
  • Buy buy codeine no prescription Without Prescription Doctor. Pharmacy Store. Low Prices.
  • Buy Cheapest tramadol info Online Cheap Online Pharmacy. Best Online.
  • Buy Cheapest cheap buspar Now Best Prices. The Largest Internet Pharmacy.
  • Buy Cheap phentermine online Online Best Prices. Online Prices For phentermine online!
  • Buy Cheap buy aspirin witn codeine from canada Now 24/Online Pharmacy. Cheap Pharmacy Online.
  • Buy Cheapest nutritional diet vitamin supplements Online Pharmacy Store. Buy Medications Online.
  • Buy Cheapest cheap generic cialis Now Pharmacy Store. Discount Pharmacy Online.
  • Buy Cheapest order spironolactone Online Best Internet. Cheap Online Pharmacy.
  • Buy Cheap long term anxiety treatment Now Order Cheap Meds Without Rx. Best Prices.
  • Buy Cheapest overnight tramadol cheap Online Special Prices For overnight tramadol cheap! Best Prices.
  • Buy Cheapest buy diet pills in uk Now Pharmacy At The Best Price! Best Drugstore.
  • Buy Cheapest order plavix Online Top Online Pharmacy. Best Internet.
  • zoloft lexapro Online Without Prescription Best Online. Free Viagra Pills!
  • Buy Cheap clonazepam drug Online Pharmacy Store. Drugs, Health And Beauty.
  • Buy Cheapest online medicine Online Discount Pharmacy Online. Best Internet.
  • Buy Cheap pharmacy anxiety Online The Largest Internet Pharmacy. Best Online.
  • Buy Cheapest codeine solution Now Best Online. Top Online Pharmacy Supplier.
  • low cost xenical Online Without Prescription Low Prices. Pharmacy Store.
  • Buy Cheap effects of lasix Now Best Online. Order Cheap Meds Without Rx.
  • Buy Cheap overnight delivery ativan Online Best Online. 24/Internet)(safe Pharmacy.
  • Buy Cheapest viagra buy Online Online Prices For viagra buy! Low Prices.
  • Buy Cheap how long does levitra last Online Top Online Pharmacy. Online Medical Shop.
  • Buy Cheap vitamin supplement Online Pharmacy At The Best Price! Low Prices.
  • Buy Cheap pain meds without prescriptions Now Cheap Pharmacy Online. 24/Online Pharmacy.
  • Buy Cheap on line prescriptions for cialis Now No Prescription Needed For Drugs. Best Online.
  • Buy Cheapest cialis line order Now Best Drugstore. Buy Medications Online.
  • Buy Cheapest tadalafil professional Online Cheap Prescription Drugs. Best Prices.
  • Buy order appetite suppressants online Online Without Prescription. Internet Prices For order appetite suppressants online!
  • Buy Cheap valium about Now Best Internet. Pharmacy At The Best Price!
  • Buy overnight shipping of cialis Without Prescription Doctor. Best Prices. Best Online.
  • Buy Cheapest valium no rx Now Best Drugstore. Buy Medications Online.
  • Buy Cheap cheapest place to buy viagra online Now 100% Satisfaction Guaranteed. Best Drugstore.
  • Buy Cheap compare viagra cialis levitra Now WorldWide Shipping. Cheap Pharmacy Online.
  • Buy Cheap pain medication without a prescription Now Free Viagra Pills! Internet Prices For pain medication without a prescription!
  • Buy Cheap wholesale medications Now Buy Medications Online. 24/Online Pharmacy.
  • Buy Cheap overnight shipping of cialis Now Cheap Pharmacy Online. Online Medical Shop.
  • Buy Cheap cialis impotence drug eli lilly co Now Best Prices. Internet Prices For cialis impotence drug eli lilly co!
  • Buy Cheap online diet meds Now Drugs, Health And Beauty. Guaranteed Shipping.
  • Buy Cheap levaquin 500 mg Now Free Viagra Pills! Special Prices For levaquin 500 mg!
  • Buy Cheapest diet patch in canada Online Best Drugstore. Guaranteed Shipping.
  • Buy Cheapest do diet pills really work Online No Prescription Needed. Best Online.
  • Buy Cheapest antidepressant pills Now Low Prices. Safe And Secure Payment System.
  • Buy Cheap reliable online pharmacies Online Best Drugstore. Pharmacy At The Best Price!
  • Buy Cheap buying pain meds without a prescription Online Internet Prices For buying pain meds without a prescription! Best Online.
  • Buying Cheapest xanax and weight loss. Mexican Rx, Best Prices. Free Viagra Pills!
  • Buy Cheapest buy valium cheap Now Low Prices. Drugs, Health And Beauty.