Skip to content

Instantly share code, notes, and snippets.

@mblsha
Created September 22, 2011 15:23
Show Gist options
  • Save mblsha/1235052 to your computer and use it in GitHub Desktop.
Save mblsha/1235052 to your computer and use it in GitHub Desktop.
Copies all files from an iTunes playlist to new folder
#!/usr/bin/env ruby -wKU
# PREREQUISITES: sudo gem install rb-appscript
require 'rubygems'
require 'appscript'
require 'fileutils'
require 'pathname'
if ARGV.length != 1
playlists = Appscript.app('iTunes').playlists.get.map { |p| "\"#{p.name.get}\"" }
puts "USAGE: extract-itunes-playlist.rb PLAYLIST_NAME"
puts "Available playlists: #{playlists.join(', ')}"
exit 0
end
selected_playlist = ARGV[0]
itunes = Appscript.app('iTunes')
itunes.playlists.get.each do |playlist|
next if playlist.name.get != selected_playlist
FileUtils::Verbose::mkdir_p(selected_playlist)
count = 1
playlist.tracks.get.each do |track|
orig_filename = track.location.get.to_s
pathname = Pathname.new(orig_filename)
dest_filename = sprintf("%04d#{pathname.extname}", count)
FileUtils::Verbose::cp(orig_filename, "#{selected_playlist}/#{dest_filename}")
count += 1
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment