Export paths of selected songs from iTunes to clipboard applescript

Alec Jacobson

October 07, 2009

weblog/

Here's an applescript to find all the (unix) paths of selected songs (in a playlist or library) in iTunes and copy them to the clipboard (with newlines in between).
(*
Extract the location paths for all songs selected in iTunes and 
copy the list (newline separated to the clipboard)

Adapted from:
 Export Selected Song List v1.1 AppleSript for iTunes written by Doug Adams dougadams@mac.com
 *)
-------------------------------------

-- check if iTunes is running 
tell application "System Events"
  if (get name of every process) contains "iTunes" then set okflag to true
end tell
if okflag then
  
  tell application "iTunes"
    
    if selection is {} then -- no selection
      display dialog "Select some tracks first..." buttons {"OK"} default button 1
    else
      set selected_songs to ""
      repeat with this_track in selection
        if selected_songs is not equal to "" then
          set selected_songs to selected_songs & "
"
        end if
        try
          set selected_songs to selected_songs & (quoted form of POSIX path of (get this_track's location))
        on error
          display dialog "Could not get location for " & (get this_track's name) buttons {"OK"} default button 1
          
        end try
      end repeat
      if selected_songs is "" then
        display dialog "None of the selected tracks had valid locations. Nothing copied..." buttons {"OK"} default button 1
      else
        set the clipboard to (selected_songs as string)
        display dialog "Paths to locations of selected songs copied to the clipboard." buttons {"OK"} default button 1
      end if
    end if
  end tell
end if