Setting the order of tracks on the ipod shuffle

Hi! I’m new to Applescript and have managed to write something to load my iPod Shuffle with music/podcasts (heavily borrowed from here), but I would ideally like to use set the order of tracks on the shuffle once they’ve been transferred. Currently my code deletes tracks/podcasts which have been listened to and then transfers new music/podcasts, but this results in the new music/podcasts being at placed at the end of the stuff already on the shuffle, which makes it impossible to find my new podcasts which I like to listen to first every day. Instead I’d like the podcasts to go to the top of the shuffle track order. The only ways I can figure out how to do this is to either make the code delete everything from the shuffle and then reload in the order I want it - but this seems a bit inefficient, or manually drag and drop the podcasts to the top once transferred, which defies the reason for using the applescript to automate things!

Hope that makes sense! Thanks in advance for any tips…
Kate

PS here is my script


tell application "iTunes"
	repeat with thisSource in sources
		if the name of thisSource = "kate" then set myIpod to thisSource
	end repeat
	
	updateAllPodcasts
	
	-- delete all the podcasts on the ipod shuffle which have been listened to
	set destinationPlaylist to the first playlist in myIpod
	set sel to every track in destinationPlaylist whose podcast is true and played count is greater than 0
	repeat with atrack in sel
		delete atrack
	end repeat
	
	-- "Shuffle -> podcasts" is a smart playlist that contains new podcasts to be added
	set sourcePlaylist to playlist "Shuffle -> podcasts"
	
	-- if statement to only do this if there are new podcasts to add
	if the number of tracks in sourcePlaylist is greater than 0 then
		set shufflable of (every track of sourcePlaylist) to true
		
		-- if statement to only do this if there are already tracks on the ipod shuffle
		if the number of tracks in destinationPlaylist is greater than 0 then
			set shufflefilenames to (name of file tracks in destinationPlaylist whose podcast is true)
			set newsel to (every track in sourcePlaylist)
			
			-- check each track on the shuffle and see if it has the same name as a podcast about to be uploaded.  If it does, change a boolean variable called "donotdup" to true so this track WON'T be deleted
			repeat with atrack in newsel
				if name of atrack is in shufflefilenames then
					set donotdup to true
				else
					set donotdup to false
				end if
				if donotdup is false then
					duplicate atrack to destinationPlaylist
				end if
			end repeat
		else
			duplicate every track in sourcePlaylist to destinationPlaylist
		end if
		
	end if
	
	display dialog "all podcasts have been updated and old ones have been deleted"
	
	-- delete all the tracks on the ipod shuffle which have been listened to in the last 7 days (smart playlist selects songs not listened to in the last 2 weeks so these must have been listened to recently on the shuffle)
	set destinationPlaylist to the first playlist in myIpod
	set sel to every track in destinationPlaylist whose podcast is false and played date is greater than ((get current date) - 7 * days)
	repeat with atrack in sel
		delete atrack
	end repeat
	
	-- "Shuffle -> music" is a smart playlist that contains new music to be added
	set sourcePlaylist to playlist "Shuffle -> music"
	
	-- if statement to only do this if there are new tracks to add
	if the number of tracks in sourcePlaylist is greater than 0 then
		set shufflable of (every track of sourcePlaylist) to true
		
		-- if statement to only do this if there are already tracks on the ipod shuffle
		if the number of tracks in destinationPlaylist is greater than 0 then
			set shufflefilenames to (name of file tracks in destinationPlaylist)
			set newsel to (every track in sourcePlaylist)
			
			-- check each track on the shuffle and see if it has the same name as a podcast about to be uploaded.  If it does, change a boolean variable called "donotdup" to true so this track WON'T be deleted
			repeat with atrack in newsel
				if name of atrack is in shufflefilenames then
					set donotdup to true
				else
					set donotdup to false
				end if
				if donotdup is false then
					duplicate atrack to destinationPlaylist
				end if
			end repeat
		else
			duplicate every track in sourcePlaylist to destinationPlaylist
		end if
	end if
end tell



I’ve done a lot of iTunes scripting with a first gen Nano and a 3rd gen Nano, but never with a shuffle. Is yours a current shuffle or an older one? I think the newer ones are a bit different than the old ones.

That being said, it should be possible to script this or write a smart playlist that will accomplish what you want. One way is to create a smart playlist that lists the current unplayed podcasts. Another could list the least recently played music tracks. Then you could write a script that creates a manual playlist (or guts an existing one) and restocks it the way you want things done. You would duplicate the podcast tracks first, then the music tracks until the playlist reaches a given size (the size of your shuffle or less).

Another method would be to create a smart playlist with the following rules:

Match any of the following rules
Playlist is Music
Genre contains podcast
limit to x GB selected by least often played

This should list unplayed podcasts and music that has been listened to least (or not at all) and list them with the unplayed items first. Not quite what you want, but close.

Hi Kevin

Thanks for your reply! It’s a second generation shuffle… I’m already using smart playlists to generate the content for the shuffle, and if the shuffle is empty everything is copied in the correct order with no problems… It’s just when I want to delete the listened-to items and add more, without completely emptying the shuffle first, that I’m having problems with the ordering. Your first suggestion is essentially what I am currently doing, and I think your second suggestion would have the same limitations because the smart playlist would be added to the shuffle at the end of the already-loaded (ie unlistened to) tracks.

Thanks anyway though! I’m beginning to think there’s no way to order the tracks that are already on the shuffle without deleting and re-adding…
kate

Sorry I didn’t have better suggestions, Kate. If you check out our tutorials in Unscripted, I’ve written a couple of articles on iTunes scripting. I don’t know if there is anything in them that might help. The first is Advanced iTunes Scripting with Libraries and the second is Improve your Applescripts: Return to iTunes Scripting.

My problem was that while iTunes’ smart playlists could choose tracks that hadn’t been played lately, it wouldn’t do the same for entire albums, which I like to listen to all at once.

Thanks Kevin, I’ll take a look at those… the album problem sounds annoying!