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

如何用AppleScript自动将列表歌曲导入Mac的Music应用?

AppleScript 批量导入指定歌曲到Music播放列表

脚本功能

  • 读取制表符分隔的SL文本文件,每一行包含「歌曲名[TAB]歌手[TAB]年份」结构
  • 在外接硬盘的General Music文件夹中搜索匹配的音频文件
  • 将找到的歌曲添加到Music应用的SL playlist播放列表
  • 自动忽略搜索失败、多文件匹配的情况,继续处理下一首

完整脚本代码

-- 配置参数,根据实际情况修改
set slFilePath to (path to desktop folder as text) & "SL.txt" -- SL文件默认路径为桌面,可按需修改
set musicRootFolder to "/Volumes/[你的外接硬盘名称]/General Music" -- 替换为实际外接硬盘名称
set targetPlaylistName to "SL playlist"

-- 初始化Music应用,确保目标播放列表存在
tell application "Music"
    if not (exists playlist targetPlaylistName) then
        make new playlist with properties {name:targetPlaylistName}
    end if
end tell

-- 读取SL文件内容
try
    set fileContents to read file slFilePath as «class utf8»
on error errMsg
    display dialog "读取SL文件失败:" & errMsg buttons {"确定"} default button 1
    return
end try

-- 按行拆分文件内容
set lineList to paragraphs of fileContents

-- 逐行处理每首歌曲
repeat with currentLine in lineList
    if currentLine is not "" then
        -- 按制表符拆分当前行的歌曲信息
        set AppleScript's text item delimiters to tab
        set songData to text items of currentLine
        set AppleScript's text item delimiters to ""
        
        if length of songData ≥ 3 then
            set songName to item 1 of songData
            set artistName to item 2 of songData
            set releaseYear to item 3 of songData
            
            -- 构造系统搜索查询条件,兼容特殊字符
            set searchQuery to "name:\"" & songName & "\" kind:music artist:\"" & artistName & "\" year:\"" & releaseYear & "\""
            set searchPath to quoted form of musicRootFolder
            
            -- 使用mdfind进行原生文件搜索
            try
                set foundFiles to do shell script "mdfind -onlyin " & searchPath & " " & quoted form of searchQuery
                if foundFiles is not "" then
                    set filePaths to paragraphs of foundFiles
                    
                    -- 将找到的文件添加到目标播放列表
                    tell application "Music"
                        repeat with filePath in filePaths
                            try
                                add POSIX file filePath to playlist targetPlaylistName
                            on error
                                -- 忽略单个文件添加失败的情况
                            end try
                        end repeat
                    end tell
                end if
            on error
                -- 忽略搜索失败的情况
            end try
        end if
    end if
end repeat

display dialog "批量处理完成!" buttons {"确定"} default button 1

使用步骤

  1. 修改配置参数:

    • 将脚本中的[你的外接硬盘名称]替换为实际的外接硬盘名称(比如硬盘叫“移动硬盘”,就改成"/Volumes/移动硬盘/General Music")
    • 如果SL.txt不在桌面,修改slFilePath为文件实际路径
  2. 前置准备:

    • 确保外接硬盘已挂载,General Music文件夹存在
    • 可提前在Music应用创建SL playlist播放列表,脚本也会自动检测并创建
  3. 运行脚本:

    • 打开「脚本编辑器」(启动台→其他文件夹内)
    • 粘贴上述代码,点击运行按钮
    • 首次运行会弹出权限请求,点击「允许」授予脚本文件访问和Music应用控制权限

注意事项

  • 兼容Music支持的所有音频格式(MP3、FLAC、AAC等)
  • 特殊字符(如å、冒号)通过系统原生搜索自动处理,无需额外转义
  • 脚本会自动跳过空行、格式错误的行,不中断整体处理流程

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 08:45:14