Get a random file from a folder and add it to an iTunes playlist

283 Views Asked by At

I have looked at some answers which tell me how to get random files from folders, and some which can deal with iTunes playlists. Haven't been able to put these together.

What I'm looking for is a way (I was thinking in AppleScript) of getting the 200 songs in my Folk playlist folder on my hard drive, randomly selecting 20 of those songs, and then adding them to an iTunes playlist.

I am aware smart playlists can kind of do this, but I want to do it as much outside of iTunes as possible, because a lot of my music is in folders and not on iTunes per se.

I'd be really grateful of any help with:

  1. Getting 20 random files from a folder And
  2. Then pushing them into a playlist.

I did wonder if there was some way I could get the number of files in per cent (20% of the files in Folk), but it's not really a deal breaker!

Thanks in advance to anyone who can help me!

Tardy

2

There are 2 best solutions below

3
pbell On BEST ANSWER

Here is the script you're looking for. I leave the first answer because it could also be useful for others.

property RList : "my random list" -- name of the random list
property ListGenres : {"Rock", "Pop", "Soundtrack", "Jazz"} -- your list of genres
property NumPerGenre : {3, 2, 5, 4} -- the number of songs per genre

tell application "iTunes"
if exists user playlist RList then -- check if the playlsit exists or not
    delete tracks of user playlist RList -- delete all current tracks of the play list
    repeat while (number of tracks of playlist RList) > 0
        delay 0.1 -- wait for the library to clear out, because iTunes is asynchronous !
    end repeat
else -- creation of the play list
    set MyPlayList to make new user playlist with properties {name:RList}
end if

repeat with I from 1 to (count of ListGenres) -- loop per genre
    set ListTracks to (tracks whose genre is (item I of ListGenres))
    repeat with J from 1 to (item I of NumPerGenre) -- loop to add x tracks per genre
        set TheTrack to item (random number from 1 to (count of ListTracks)) of ListTracks
        duplicate TheTrack to playlist RList
    end repeat -- loop for all tracks per genre
end repeat -- loop by Genre
play playlist RList -- start to play !  
end tell

I've put many comments to make it clear (I hope). In this example, I have 4 genres, and I will get 3 songs of the first genre, 2 songs of the second genre, ...and so on. you can change these properties, as long as the list of genre count same number of items as list of numpergenre.

Unfortunately, since iTunes 11, the shuffle attribute can't be set via script, you must set it manually in iTunes to play the list randomly (it can be set once for all)

12
pbell On

to play tracks, you must first import them in iTunes as Vadian said. better to import them in play list (easier to delete after). the script bellow does that :

set MyRatio to 0.2 -- the % of files randomly selected over the total file of the selected folder
set MyFolder to choose folder "select folder with your musics"
tell application "Finder" to set MyList to every file of MyFolder

-- build the random list
set SongList to {}
set MaxCount to (MyRatio * (count of MyList)) as integer
set MyCount to 0
repeat until MyCount = MaxCount
set MyItem to random number from 1 to (count of MyList)
set NewFile to (item MyItem of MyList) as string
if NewFile is not in SongList then
    copy NewFile to the end of SongList
    set MyCount to MyCount + 1
end if
end repeat

-- add the files to iTunes new playlist
tell application "iTunes"
set MyPlayList to make new user playlist with properties {name:"my Import"}
repeat with I from 1 to count of SongList
    add ((item I of SongList) as alias) to MyPlayList
end repeat
play MyPlayList -- start to play the play list
end tell