You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何编写AppleScript从Tardy's Music按文件名生成iTunes播放列表?

没问题,这完全可以用AppleScript搞定!我帮你整理了一套完整的方案,包括准备工作、脚本代码和详细说明,一步步来就行:

准备工作
  • 先把你的文件名列表整理成纯文本文件:每行放一个完整文件名(比如01 Song Title.mp3),不要有空行或者多余符号。把这个文件存到容易找到的地方,比如桌面,命名为tracks_to_add.txt
  • 确保iTunes已经打开,并且提前创建好名为Best Of的播放列表(如果没创建也没关系,脚本会自动帮你生成)
AppleScript 脚本代码

打开「脚本编辑器」(在应用程序/实用工具文件夹里),粘贴下面的代码:

-- 设置关键路径和名称,根据你的实际情况修改
set musicFolderPath to (path to music folder as text) & "Tardy's Music:"
set trackListPath to (path to desktop as text) & "tracks_to_add.txt"
set targetPlaylistName to "Best Of"

-- 读取文件名列表,处理读取错误
try
    set trackListFile to open for access file trackListPath
    set trackNames to paragraphs of (read trackListFile)
    close access trackListFile
on error
    try
        close access file trackListPath
    end try
    display dialog "无法读取文件名列表文件,请检查文件路径是否正确!" buttons {"确定"} default button 1
    return
end try

-- 连接iTunes并处理播放列表
tell application "iTunes"
    if not running then activate
    
    -- 检查目标播放列表是否存在,不存在则自动创建
    set targetPlaylist to null
    try
        set targetPlaylist to playlist targetPlaylistName
    on error
        set targetPlaylist to make new playlist with properties {name:targetPlaylistName}
        display dialog "已自动创建播放列表 '" & targetPlaylistName & "'" buttons {"确定"} default button 1
    end try
    
    -- 遍历每个文件名,搜索并添加到播放列表
    set addedCount to 0
    set notFoundCount to 0
    set notFoundTracks to {}
    
    repeat with trackName in trackNames
        -- 跳过空行
        if trackName is not "" then
            -- 精准搜索:只在Tardy's Music文件夹下匹配同名文件
            set foundTracks to (file tracks whose location starts with musicFolderPath and name is trackName)
            if length of foundTracks > 0 then
                -- 添加第一个匹配的曲目(避免同文件名多文件的冲突)
                add item 1 of foundTracks to targetPlaylist
                set addedCount to addedCount + 1
            else
                set notFoundCount to notFoundCount + 1
                set end of notFoundTracks to trackName
            end if
        end if
    end repeat
    
    -- 弹出结果统计,方便排查问题
    set resultMessage to "操作完成!" & linefeed & "成功添加 " & addedCount & " 首曲目"
    if notFoundCount > 0 then
        set resultMessage to resultMessage & linefeed & "未找到 " & notFoundCount & " 首曲目:" & linefeed & (join notFoundTracks with linefeed)
    end if
    display dialog resultMessage buttons {"确定"} default button 1
end tell
关键细节说明
  • 路径修改:如果你的文件名列表不在桌面,或者Tardy's Music文件夹的位置不是默认音乐文件夹下,记得修改开头的musicFolderPath和trackListPath变量
  • 精准匹配:脚本里用location starts with musicFolderPath限定了搜索范围,只会在Tardy's Music及其子文件夹里找文件,不会误匹配其他位置的同名歌曲
  • 错误处理:脚本会处理文件读取失败、播放列表不存在等情况,还会在最后告诉你哪些文件没找到,方便你核对文件名是否正确
  • 重复处理:如果同一个文件名在文件夹里有多个副本,脚本只会添加第一个匹配到的,避免重复添加
使用步骤
  1. 按照准备工作的要求整理好你的文件名文本文件
  2. 打开脚本编辑器,粘贴代码并检查路径是否正确
  3. 点击脚本编辑器顶部的运行按钮(▶️),跟着弹窗提示操作就行

内容的提问来源于stack exchange,提问作者tardy pigeon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.20 08:56:57