【问题标题】:Get a random file from a folder and add it to an iTunes playlist从文件夹中获取随机文件并将其添加到 iTunes 播放列表
【发布时间】:2015-11-14 09:12:33
【问题描述】:

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

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

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

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

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

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

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

迟到

【问题讨论】:

  • 据我所知,您无法将文件添加到不在 iTunes 数据库中的 iTunes 播放列表中,我的意思是您无论如何都必须将文件导入 iTunes
  • Vadian,我正在考虑使用自动操作将文件导入 iTunes。

标签: file random applescript itunes playlists


【解决方案1】:

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

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

我已经放了很多 cmets 来说明清楚(我希望)。在这个例子中,我有 4 个流派,我会得到第一个流派的 3 首歌曲,第二个流派的 2 首歌曲,……以此类推。您可以更改这些属性,只要流派列表的项目数与 numpergenre 列表相同。

很遗憾,从iTunes 11开始,shuffle属性无法通过脚本设置,必须在iTunes中手动设置才能随机播放列表(可以设置一次)

【讨论】:

  • 太棒了。谢谢你。请问你是怎么学的问那些变量名和命令?我尝试过使用 AppleScript 字典,但想知道是否有一本我可以与所有这些人一起获得的书,而不是使用搜索引擎在这里找到一个位,在那里找到另一个位等?
  • 只是在 applescript 中运行它(而不是作为应用程序),似乎完成了这项工作,但让 Applescript 旋转并且不得不强制退出。我应该在某处写一个“放弃之后”的行吗?
  • 这可能是由于曲目数量过多。我的 iTunes 数只有 7000 左右,还可以。我从未用 20000 首曲目测试过(也不能)。如果您从编辑器运行脚本,则在窗口底部显示事件/结果并在不同位置添加一些日志指令行以查看问题所在。日志指令类似于日志“跟踪步骤 1”。查看脚本编辑器的底部窗口,您将一一看到所有痕迹,直到它阻塞为止。这样做,你会看到它挂在哪里。
【解决方案2】:

要播放曲目,您必须先将它们导入 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

【讨论】:

  • 太棒了,pbell。我非常感谢您付出的努力 - 我相信我可以通过一些调整来使用该代码!
  • 我唯一不确定的是:它在文件夹中是递归的,还是只是在顶层?真的很想深入挖掘。
  • 通过添加“整个内容”来更改第 3 行:告诉应用程序“Finder”将 MyList 设置为 MyFolder 的全部内容的每个文件,其扩展名为 {"mp3", "aif"}。您必须使用可能的文件扩展名完成列表。
  • 非常感谢!您所有的 cmets 都表示赞赏。绝对解决了我的问题。
  • 我意识到我很痛苦,但我试图摆弄代码,以便将其变成一个应用程序,该应用程序遍历大量文件夹并从每个文件夹中获取不同的数字,但我摔倒了在第一关。我更改了代码,使其启动:告诉应用程序“Finder”将 MyList 设置为别名“Macintosh HD:General Music:02 Rock:02E Rock 2010-2020:”的全部内容的每个文件,其扩展名在 {“mp3” ,“m4a”,“aif”,“mp4”}。在进入随机列表阶段之前似乎已经超时。知道如何从我的 Rock 2010-2020 文件夹中获取 15 首曲目,然后从另一个文件夹中获取 5 首曲目吗?
猜你喜欢
  • 1970-01-01
  • 2016-10-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-10-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多