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

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

我查看了一些告诉我如何从文件夹中获取随机文件的答案,以及一些可以处理 iTunes 播放列表的答案。没能把它们放在一起。

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

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

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

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

我确实想知道是否有某种方法可以按百分比获取文件数(Folk 中文件的 20%),但这并不是真正的交易破坏者!

在此先感谢任何可以帮助我的人!

迟到

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

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

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

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 首歌曲,...等等。您可以更改这些属性,只要流派列表的项目数与数字流派列表的项目数相同即可。

遗憾的是,自iTunes 11起,shuffle属性无法通过脚本设置,必须在iTunes中手动设置随机播放列表(可一次设置)