有谁能够编写一个 Applescript 作为创建 Droplet 应用程序的基础,当我将一堆 mp3 放到它上面时,它会使用文件名模式“标题 - 艺术家 - 年份”来相应地更改每个文件的 id3 标签?
我有一个我试图做的示例,但不断出现错误:
tell application "Music"
try
-- Get the "Filename Retag" playlist
set playlistName to playlist "Filename Retag"
set filenameRetagPlaylist to playlistName
-- Process each track in the playlist
repeat with aTrack in file tracks of filenameRetagPlaylist
set filePath to location of aTrack
set fileName to name of (info for filePath)
-- Extract data from filename
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
-- Update track metadata
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
这给了我错误:错误“无法将应用程序“音乐”的“类 cSrc” id 64 的“类 cUsP” id 2444 转换为 Unicode 文本类型。”数字 -1700 从 «class cUsP» id 2444 «class cSrc» id 64 到 Unicode 文本
我添加此代码是因为我可能非常接近解决方案,但有人可以看到我做错了什么!
对于 Droplet,需要将项目添加到音乐库中,除非它们已经添加到“文件名重新标记”播放列表中。请注意,文件的名称path还包含扩展名,因此如果您要走这条路线,则在使用名称模式时需要删除扩展名。
playlistName
变量设置为播放列表,
但 filenameRetagPlaylist
也设置为 playlistName
,所以它
看起来这两个陈述混淆了,应该是:
set playlistName to "Filename Retag"
set filenameRetagPlaylist to playlist playlistName
info for
,因为轨道具有 name
属性;textDelimiters
变量,你应该设置
AppleScript’s text item delimiters
属性;name
、artist
、year
等。
在这种情况下,它们都可以用一条语句来设置。您发布的脚本经过一些清理并进行了更多错误处理,看起来像:
tell application "Music"
try
-- Get the "Filename Retag" playlist
set playlistName to "Filename Retag"
set filenameRetagPlaylist to playlist playlistName
-- Process each track in the playlist
set processed to 0 -- this will be the number of successfully processed items
set theTracks to file tracks of filenameRetagPlaylist
repeat with aTrack in theTracks
set fileName to name of aTrack
-- Extract data from filename
set tempTID to AppleScript's text item delimiters -- stash current delimiters
set AppleScript's text item delimiters to " - "
set textItems to text items of fileName
set AppleScript's text item delimiters to tempTID -- restore previous delimiters
if (count textItems) = 3 then
try
if item 3 of textItems as integer < 1700 or item 3 of textItems as integer > 2100 then error "Invalid year - value is out of range." -- check the text for a year
tell aTrack to set {its name, its artist, its year} to textItems -- Update track metadata
set processed to processed + 1
on error errmess -- year is not a number, etc
log errmess
do shell script "echo 'Error with track \"" & fileName & "\": " & errmess & "' >> /var/log/system.log"
end try
else
do shell script "echo 'Invalid filename format: \"" & fileName & "\"' >> /var/log/system.log"
end if
end repeat
do shell script "echo 'Metadata updated for " & processed & " of " & (count theTracks) & " tracks in playlist \"" & playlistName & "\".' >> /var/log/system.log"
on error errmess -- no playlist, etc
log errmess
do shell script "echo 'Script error: " & errmess & "' >> /var/log/system.log"
end try
end tell