#!/usr/bin/ruby require "appscript" require "osax" include Appscript puts "running" # set up appscript connection itunes = app('iTunes'); sa = OSAX::ScriptingAddition.new("StandardAdditions") sel = itunes.selection.get xml = "FrontRowExportPlaylist.xml"; ids = [] puts "got itunes connection" # get IDs of selected tracks if they're shared for track in sel con = track.container.container.get kind = con.kind.get if kind == :'shared_library' sharename = con.name.get.to_s id = track.persistent_ID.get.hex # change in 7.2? # puts id.class, id if (id < 0) # puts "correcting id" # puts id id = 2**64+id # else # puts "no need to invert" end ids.push(id.to_s(base=16).upcase) end end if ids.length > 0 # puts ids else puts "No shared tracks" exit end itunes = nil # remove iTunes connection puts "got tracks" # open temporary file and set up XML xml_alias = sa.path_to('temp') # implicit "system domain" here, might be nice to change xml_file = xml_alias.to_s + xml playlist_start = < Playlists Name Remote Playlist Playlist Items EOF playlist_end = < Tracks EOF # open and write file playlist = File.new(xml_file, "w") playlist.puts playlist_start ids.each { |id| playlist.puts " " playlist.puts " Persistent ID" playlist.puts " " + id + "" playlist.puts " " } playlist.puts playlist_end puts "written xml to "+xml_file # now tell AppleScript to add the playlist # I'd like to do this with pure Ruby too, but it doesn't work. Any # clues? # xml_alias = MacTypes::Alias.path(xml_file) # get alias once file is created # shares = itunes.sources.get # shares.each { # |share| # name = share.name.get # if name.to_s == sharename # puts "executing share add" # share.add(xml_alias) # end # } # so instead... script = <