从文件夹中随机获取一个文件,并将其添加到iTunes播放列表中



我看过一些答案,这些答案告诉我如何从文件夹中获取随机文件,还有一些可以处理iTunes播放列表。还没能把这些放在一起。

我正在寻找一种方法(我在AppleScript中思考),将200首歌曲放在硬盘上的Folk播放列表文件夹中,随机选择其中20首,然后将它们添加到iTunes播放列表中。

我知道智能播放列表可以做到这一点,但我想尽可能多地在iTunes之外做,因为我的很多音乐都在文件夹中,而不是iTunes本身。

如果有任何帮助,我将不胜感激:

  1. 从文件夹中随机获取20个文件而且
  2. 然后把它们推到播放列表中

我确实想知道是否有什么方法可以让我获得%(Folk中20%的文件)的文件数量,但这并不是真正的交易破坏者!

提前感谢任何能帮助我的人!

Tardy

这是您要查找的脚本。我留下第一个答案是因为它对其他人也有用。

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

我已经发表了许多评论来表明这一点(我希望)。在这个例子中,我有4个流派,我会得到3首第一流派的歌曲,2首第二流派的歌曲。。。等等。你可以更改这些属性,只要流派列表的项数与numper流派列表的项数相同。

不幸的是,由于iTunes 11无法通过脚本设置shuffle属性,您必须在iTunes中手动设置它才能随机播放列表(可以一次性设置)

如Vadian所说,要播放曲目,必须首先将其导入iTunes。最好在播放列表中导入它们(之后更容易删除)。下面的脚本是这样做的:

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

最新更新