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

求AppleScript Droplet实现:按文件名批量修改MP3的ID3标签(附报错代码)

AppleScript批量修改MP3 ID3标签报错排查

需求

创建基于AppleScript的Droplet应用,将批量MP3文件拖入后,按照「TITLE - ARTIST - YEAR」的文件名格式提取信息,修改对应ID3标签。

尝试的代码

tell application "Music"
    try
        -- 获取"Filename Retag"播放列表
        set playlistName to playlist "Filename Retag"
        set filenameRetagPlaylist to playlistName
        
        -- 处理播放列表中的每首曲目
        repeat with aTrack in file tracks of filenameRetagPlaylist
            set filePath to location of aTrack
            set fileName to name of (info for filePath)
            
            -- 从文件名提取信息
            set textDelimiters to " - "
            set textItems to text items of fileName
            if (count of textItems) = 3 then
                set title to text 1 thru -2 of (item 1 of textItems)
                set artist to text 1 thru -2 of (item 2 of textItems)
                set year to item 1 of textItems
                
                -- 更新曲目元数据
                set name of aTrack to title
                set artist of aTrack to artist
                set year of aTrack to year
            else
                do shell script "echo \"Invalid filename format: " & fileName & "\" >> /var/log/system.log"
            end if
        end repeat
        
        do shell script "echo \"Metadata updated for tracks in playlist \\\"" & playlistName & "\\\"\" >> /var/log/system.log"
        
    on error
        do shell script "echo \"Playlist \\\"" & playlistName & "\\\" not found in iTunes.\" >> /var/log/system.log"
    end try
end tell

报错信息

error "无法将应用程序"Music"的«class cSrc» id 64的«class cUsP» id 2444转换为Unicode文本。" number -1700 from «class cUsP» id 2444 of «class cSrc» id 64 to Unicode text

问题分析与修复方案

核心错误原因

  1. set playlistName to playlist "Filename Retag"直接将播放列表对象赋值给变量,后续日志输出时尝试将对象转文本,触发类型不匹配报错。
  2. 文件名提取逻辑错误:未去除文件后缀,且年份提取时索引错误(原代码取item 1,实际应为item 3)。
  3. 未处理AppleScript文本分隔符的全局影响,可能干扰后续代码。
  4. 原代码基于播放列表处理,未实现Droplet拖入文件的核心需求。

修复后的Droplet代码

-- 保存默认文本分隔符
set oldDelimiters to AppleScript's text item delimiters

on open droppedFiles
    tell application "Music"
        activate
        repeat with aFile in droppedFiles
            try
                -- 获取文件名(不含后缀)
                set fileInfo to info for aFile
                set fileName to name of fileInfo
                set fileExtension to name extension of fileInfo
                set fileNameNoExt to text 1 thru ((length of fileName) - (length of fileExtension) - 1) of fileName
                
                -- 从文件名提取信息
                set AppleScript's text item delimiters to " - "
                set textItems to text items of fileNameNoExt
                if (count of textItems) = 3 then
                    set trackTitle to item 1 of textItems
                    set trackArtist to item 2 of textItems
                    set trackYear to item 3 of textItems
                    
                    -- 将文件导入Music并获取曲目对象
                    add aFile to library playlist
                    delay 1 -- 等待导入完成
                    set aTrack to first track whose location is aFile
                    
                    -- 更新元数据
                    set name of aTrack to trackTitle
                    set artist of aTrack to trackArtist
                    set year of aTrack to trackYear
                else
                    do shell script "echo \"无效文件名格式: " & quoted form of fileName & "\" >> ~/Library/Logs/MP3Retag.log"
                end if
            on error errMsg
                do shell script "echo \"处理文件" & quoted form of (name of fileInfo) & "时出错: " & quoted form of errMsg & "\" >> ~/Library/Logs/MP3Retag.log"
            end try
        end repeat
    end tell
    -- 恢复默认文本分隔符
    set AppleScript's text item delimiters to oldDelimiters
end open

关键改进点

  • 使用on open结构实现Droplet拖入文件的处理逻辑。
  • 正确提取不含后缀的文件名,修正年份索引错误。
  • 保存并还原默认文本分隔符,避免全局干扰。
  • 将日志输出到用户目录下的专属日志文件,便于查看。
  • 添加导入延迟,确保Music完成文件加载后再修改元数据。

内容的提问来源于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 01:25:55