Sync your mp3 Player with iTunes

This script is intented for those of us with no such luck to have an iPod (I had one and it broke… :frowning: ) and are using another… thing…

It works pretty nice… but has a few bugs and a missing part I thought I had added but just realized I didn’t hehe…

Bugs: sometimes it copies a file and then crashes when trying to copy it again (which if you take a look should never happen).
Missing part: (depending on player) It should check if the file is really a .mp3 file… and not a .aac or something else, but as said… that depends if your player supports other media types

It would be great if someone could take a look at it and find out why it doesn’t work ok.

set the VolumeDir to "/Volumes/" as POSIX file as alias
set the MountedVolumes to list folder VolumeDir without invisibles
set PathToMusicFiles to "Untitled:TUNER:" -- or whatever the path to where you have to put the files...
set iTunesSyncPlaylist to "Mp3 Playlist" -- or whatever you called it...

if "Untitled" is in MountedVolumes then -- or whatever your mp3 reports it self to be.
	set PlaylistFiles to {}
	set PlaylistFilePaths to {}
	set DeletedFiles to 0
	set AddedFiles to 0
	
	tell application "iTunes"
		if size of user playlist iTunesSyncPlaylist is greater than 239.9 * 1024 * 1024 then return "Playlist too big"
		-- set this value according to the players limit.
		set PlaylistCount to count of tracks of user playlist iTunesSyncPlaylist
		repeat with i from 1 to PlaylistCount
			set ThisTrack to get location of track i of user playlist iTunesSyncPlaylist -- returns an alias.
			set the end of PlaylistFilePaths to ThisTrack
			tell application "Finder" to set the end of PlaylistFiles to displayed name of ThisTrack
		end repeat
	end tell
	
	tell application "Finder"
		set ExistingPlayerFiles to get every file in (PathToMusicFiles as alias)
		repeat with i from 1 to count of ExistingPlayerFiles
			set ThisFile to displayed name of (item i of ExistingPlayerFiles)
			if ThisFile is not in PlaylistFiles then
				delete alias (PathToMusicFiles & ThisFile)
				set DeletedFiles to DeletedFiles + 1
			end if
		end repeat
		empty trash -- bueno, esto es un poco jugado porque borraría toda la trash...
		-- not completely good since it emptys everything else...
		
		repeat with i from 1 to count of PlaylistFilePaths
			set ThisTracksName to displayed name of (item i of PlaylistFilePaths)
			try
				exists displayed name of (PathToMusicFiles & ThisTracksName) as alias
			on error
				try
					move (item i of PlaylistFilePaths) to PathToMusicFiles
					set AddedFiles to AddedFiles + 1
				on error ElError
					activate me
					display dialog "Error con: " & (item i of PlaylistFilePaths) & return & ElError buttons {"Ok"} default button 1 giving up after 10
				end try
			end try
		end repeat
	end tell
	
	activate me
	display dialog "Added Files: " & AddedFiles & ". Deleted Files: " & DeletedFiles buttons {"Ok"} default button 1 giving up after 10
	return {"Added Files: " & AddedFiles, "Deleted Files: " & DeletedFiles}
end if