Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Using a Smart Playlist to sync small iPods' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Using a Smart Playlist to sync small iPods
Authored by: sjonke on Feb 20, '04 12:23:13PM

I don't have my iPod mini yet, but I wrote an applescript that uses one smart playlist containing randomly selected songs limited to 1500 MB to generate another playlist that has full albums in it. It first clears out both lists which causes the smart playlist to reselect random songs for a fresh mix that is worthy on its own. Then it walks through those songs, pulls a random number out of a hat and if the number is less than some specifiable percent (currently 25% but it may prove necessary to tweak that number) it adds all the tracks from that album to the second playlist. The net results is a lengthy random track list and a random selection of the full albums those tracks are on, all fitting in somewhat less than 4 GB of space. Both playlists will be sync'd to the iPod. I'm anxious to put it in use, but my just-ordered-last-night green iPod mini is showing a 1 to 3 week wait. Sigh....

---
--- What?



[ Reply to This | # ]
Using a Smart Playlist to sync small iPods
Authored by: filmsmith on Feb 20, '04 12:37:24PM
Really? I just (nearly) finished a script that will copy all my songs from my 'New Releases' smart playlist to a playlist called 'Pack the 'Pod' and then proceed to fill up the rest of the user specified (defaults to 15 gigs) space with albums at random. My script is posted below for anyone who's interested. Also, if you would post your script, I'd love to compare notes!
property default_capacity : 15
property default_playlist : "Pack the 'Pod"
global the_capacity
global album_list

set album_list to {""}
tell application "iTunes"
	delete every track of playlist default_playlist
	with timeout of 360 seconds
		set default_capacity to text returned of (display dialog "How full shall I pack'r, Sir? (Please give your answer in Gigs)" default answer default_capacity giving up after 300) as integer
	end timeout
	set the_capacity to (default_capacity * 1.0E+9) as real
	my smart_playlist()
	my library_pack()
	repeat with i from 1 to (count of sources)
		if kind of source i = iPod then
			set the_pod to name of source i as text
			exit repeat
		end if
	end repeat
	try
		update the_pod
	end try
end tell

on smart_playlist()
	tell application "iTunes"
		set random_library to playlist "New Releases"
		set shuffle of random_library to false
		set track_counter to 1
		repeat while (size of playlist default_playlist) < (size of random_library)
			try
				set album_name to album of track track_counter of random_library
				if album_name is not in album_list and album_name is not "" then
					duplicate (get a reference to (every track in random_library whose album = album_name)) to playlist default_playlist
					set end of album_list to album_name
				else if album_name is "" then
					duplicate track track_counter to playlist default_playlist
				end if
				repeat with x from 1 to count of tracks of random_library
					if album of track (track_counter + x) of random_library is not album_name then
						set track_counter to (track_counter + x)
						exit repeat
					end if
				end repeat
			on error
				exit repeat
			end try
		end repeat
	end tell
end smart_playlist


on library_pack()
	tell application "iTunes"
		set random_library to playlist "Library" of source "Library"
		set shuffle of random_library to true
		set shuffle of random_library to false
		set shuffle of random_library to true
		set track_counter to 1
		repeat while (size of playlist default_playlist as real) < the_capacity
			if enabled of (track track_counter of random_library) is true then
				set album_name to album of track track_counter of random_library
				if album_name is not in album_list and album_name is not "" then
					duplicate (get a reference to (every track in random_library whose album = album_name)) to playlist default_playlist
					set end of album_list to album_name
				else if album_name is "" then
					duplicate track track_counter in random_library to playlist default_playlist
				end if
			end if
			set track_counter to track_counter + 1
		end repeat
	end tell
end library_pack

p.s. If anyone can find a way to optimize this as it takes about 15 minutes to complete, I'm very interested to hear it. I posted this to macscripter.net, but found little help.

[ Reply to This | # ]
Using a Smart Playlist to sync small iPods
Authored by: sjonke on Feb 20, '04 01:27:21PM
Mine's pretty simple. The trackPlaylist should be a smart playlist that selects tracks at random (or however) and is limited to some size (I have mine set to 1500 MB) and the albumPlaylist should be a regular playlist:

property trackPlaylist : "iPod Tracks"
property albumPlaylist : "iPod Albums"
property albumsPercent : 0.25

tell application "iTunes"
  delete every track of playlist trackPlaylist
  delete every track of playlist albumPlaylist
  delay 2
  
  if exists (window 1) then set view of window 1 to playlist albumPlaylist
  
  set theAlbums to {}
  repeat with x in every track of playlist trackPlaylist
    set currAlbum to album of x
    if theAlbums does not contain currAlbum then
      set theAlbums to theAlbums & currAlbum
      if (random number) < albumsPercent then
        duplicate (every track of playlist "Library" whose album is currAlbum) to playlist albumPlaylist
      end if
    end if
  end repeat
end tell
NOTE: if you put this in iTunes script menu then be sure to save it as an application and not a compiled script. When I run it as a compiled script from there, the "delay 2" seems to block iTunes and as a result the trackPlaylist doesn't update before the script starts iterating over it. It works fine as an application and it works fine as a compiled script from the shareware "FastScripts" menu. It takes about a minute to run on my 400 MHz iMac G3, but that is just an estimate - I haven't timed it.

---
--- What?

[ Reply to This | # ]

Using a Smart Playlist to sync small iPods
Authored by: filmsmith on Feb 20, '04 01:37:52PM

Dang. I never thought I'd have to say this but... yours is so much smaller and more efficient than mine.

Having compared the two, I see how to trim the bloat in mine, though I can only trim some. Mostly, I have extra lines to compensate for iTunes seemingly crappy 'Shuffle' system. However, a few lines in mine that I prefer are those that pass over tracks that aren't enabled and also includes tracks whose album fields are empty. I also threw in some stuff to update my iPod as soon as it's finished. Basically, I set this script to run when I wake up monday morning and by the time I'm ready to go, I have a newly randomized iPod with all the New Releases included.

I think I see why yours is faster than mine. Mine uses a brute force to try and find albums that haven't been chosen, so odds are good that, as the script nears completion, it will take longer to find unused albums. Yours seems to solve this with "if (random number) < albumsPercent then" though I'll need to test it this eve.

Thanks for hook-up. I'm always curious to see how other people tackle scrips.



[ Reply to This | # ]
Using a Smart Playlist to sync small iPods
Authored by: sjonke on Feb 20, '04 01:56:17PM

Regarding passing over tracks that aren't enabled, you get that for free by setting the smart playlist to only include enabled tracks! Use the smart playlist to do as much of the work as possible.

---
--- What?



[ Reply to This | # ]
Never mind! Here's the solution.
Authored by: sjonke on Feb 20, '04 01:59:27PM
Oh, never mind - I see what you mean. Good point. I changed the duplicate line to the following to resolve that:

duplicate (every track of playlist "Library" whose album is currAlbum and enabled is true) to playlist albumPlaylist

---
--- What?

[ Reply to This | # ]

Never mind! Here's the solution.
Authored by: filmsmith on Feb 20, '04 02:01:13PM

Look at that! Had no idea I could do it that way.

Thanks again!



[ Reply to This | # ]
Never mind! Here's the solution.
Authored by: sjonke on Feb 20, '04 02:12:23PM

There are some powerful constructs in Applescript, they're just poorly documented and inconsistently implemented. An application's dictionary only tells you half the story.

My script presumes you don't have two or more albums with the same name, though that is certainly possible. You could include the artist name in there (... and artist is someName) - I didn't put that in because it was easier/faster to just save the album name in a list and it's not an issue for my library.

---
--- What?



[ Reply to This | # ]
Using a Smart Playlist to sync small iPods
Authored by: RAMd®d on Feb 22, '04 09:59:54PM

Ok, I'm way out of my element here, but I want to do what you're about to do, once you get your Mini.

Two things-
*Now that the dust has settled, what does your script look like?
*Will the random selection be totally/randomly different each time you sync? Or will it be slightly different, based on the additions of new songs to iTunes?

What I'd really like is to be able to create playlists for my favorite groupings, and then randomly populate them *each* time I sync.

Thanks for the great info!



[ Reply to This | # ]