Find files from iTunes playlist and copy to folder

Hello all.

I’m trying to write a script that finds the files for the selected tracks in iTunes and copies them to a folder on the Desktop. This is because I buy new music on my laptop but I have no good way to sync this with my iMac at home. This way I could take all track in a playlist “new music” and have the script locate then and copy the files to the folder “To iMac” on the desktop, for easy transfer to the iMac.

This is what I have come up with so far: (It only works for one selected track at a time)

tell application "iTunes"
	set selec to tracks of current playlist
	repeat with the_track in selec
		set track_name to name of the_track
		tell application "System Events"
			activate application "iTunes"
			keystroke "r" using {command down}
		end tell
		tell application "Finder"
			if selection is not {} then
				set s to selection
				duplicate s to folder "To iMac" with replacing
			else
				display dialog "Finder encountered an error while trying to copy the track " & track_name & " to ~/Desktop/To iMac." with title "iTunes" with icon 1 buttons {"Cancel"} default button 1
			end if
		end tell
	end repeat
end tell

Hope there’s someone out there that can help me with this.
Thanks in advance,
regards Cesar

Hi Cesar,

it can be a bit easier without “Show in Finder”.
Sometimes it’s confusing with “selection” properties in different applications

tell application "iTunes"
	repeat with i in selection
		set s to location of i
		tell application "Finder" to duplicate s to folder "To iMac" with replacing
	end repeat
end tell

Thank you Stefan.

Your script work exactly like I wanted it to. “set s to location of” helps a lot, instead going by “System Events”.

Thanks again,
regards Cesar

tell application "iTunes" to get location of selection
tell application "Finder" to duplicate result to folder "To iMac" with replacing

:slight_smile:

Hehe… Now, who can do it on one line?

Seriously, thanks for the help, I’m sure I’ll be back with more questions.
Cesar