Mod: new playlist
  • Copy the tracks you just added to a new playlist.

I want the option of being able to copy the new tracks to a new playlist. This first routine does the asking and the one following deals with the playlist creation and track copying.

Declare a variable playlistName as a global. Modify the iTunes routine that loops with each selected track with the new playlist query:

tell application "iTunes"
	
	-- add to a playlist?
	set playlistName to ""
	set defAns to (t1's artist & " - " & t1's album) as text
	
	set ddResult to (display dialog "Put new tracks in playlist named:" default answer defAns ¬
		buttons {"OK", "Skip"} default button 2)
	if text returned of ddResult is not "" and button returned of ddResult is "OK" then
		set playlistName to text returned of ddResult
	end if
	
	log playlistName -- observation/debugging
	
	repeat with t in sel
		try
			my copy_this_file_by_pid(get t's persistent ID)
		end try
	end repeat
	
end tell


The playlistName variable will both 1) serve as a flag that we want to create a new playlist as well as 2) actually contain the string to be used as the new playlist's name.

You can set the display dialog's default answer parameter to a suggested playlist name using the artist and album of the first selected track. Or make it blank, "".

Later, when the tracks are being added to iTunes, insert the new code right after the add command:

		try
			set aliasToAdd to (POSIX file (locLoc)) as text
			with timeout of (1 * days) seconds
				tell application "iTunes"
					-- add the file
					set newTrack to add alias aliasToAdd
					-- copy to playlist?
					if playlistName is not "" then
						if (not (exists playlist playlistName)) then
							make user playlist with properties {name:playlistName}
						end if
						duplicate newTrack to playlist playlistName
						-- optional; select the playlist to watch progress
						set view of front browser window to playlist playlistName
					end if
				end tell
			end timeout
		on error m number n
			log m
			log n
		end try


If playlistName is not an empty string (which it would be if we declined to create a new playlist) then create a new playlist if it doesn't exist and duplicate the newTrack to it. And set the view to watch all the action while you're at it.

"But what about play count and ratings tags?"

These tags (among others) don't travel with a file. In order to copy tags correctly they would have to be retrieved from the remote iTunes during the osascript section and applied to each corresponding track as it is added to the local iTunes. To keep things simple I didn't include anything like that in this tutorial. Wouldn't it be normal for individual users to change ratings and increase play counts as a matter of course anyway? So why bother.

Site contents © 2001 - 2014 Doug Adams and weblished by Doug Adams, dougscripts AT mac DOT com. About.
All rights reserved. Legal.
AppleScript, iTunes, iPod, iPad, and iPhone are registered trademarks of Apple Inc.