使用 AppleScript 重命名 iTunes 文件

Rename iTunes files with AppleScript

我在 iTunes 中安装了这个漂亮的 AppleScript,它可以将选定的文件重新定位到我神圣的音乐文件夹中。但是,我还想将文件重命名为 {Artist} - {Trackname}.extension(99% 的时间扩展名为 mp3)。

虽然艺术家和曲目名称在脚本的前面是已知的,但我不知道如何在 move_the_files-函数中也使它们可用。

希望各位AppleScript(自虐)爱好者能帮帮我。 这是脚本:

property my_title : "Re-Locate Selected"
global selectedTracks, originalLocs
global newDirectory

tell application "iTunes"
    ok_v(get version) of me

    set selectedTracks to selection
    if selectedTracks is {} then return
    if (get class of (item 1 of selectedTracks)) is not file track then return

    set originalLocs to {}
    repeat with i from 1 to (length of selectedTracks)
        tell item i of selectedTracks to set {nom, loc, artist} to {get name, get location, get artist}
        # log ("artis" & artist)
        if loc is missing value then
            display dialog "The file for \"" & nom & "\" appears to be missing. ..." buttons {"Cancel", "OK, Skip It"} default button 1 with icon 2 giving up after 30 with title "Need a decision..."
        end if
        set end of originalLocs to (loc as text)
    end repeat

    # OKAY to proceed

    display dialog "This script will re-locate the files of the selected tracks to a new folder. All meta-data will be preserved." buttons {"Cancel", "Execute"} default button 2 with title my_title
    # set newDirectory to (choose folder with prompt "Move files of selected tracks to:")
    set newDirectory to "/Volumes/Daten/Musik/Tracks/2014/12"

    #   log ("rez: " & newDirectory)

    my move_the_files()

    tell application "iTunes" to if (get frontmost) then display dialog "Done, handsome <3!" buttons {"Cool"} default button 1 with title my_title giving up after 15

end tell

to move_the_files()
    repeat with t from 1 to (length of selectedTracks)
        try
            with timeout of 300 seconds
                set f to (item t of originalLocs) as text
                do shell script "cp -p " & quofo(f) & space & quofo(newDirectory)
                relocate((item t of selectedTracks), fig_new_loc(f))
                tell application "Finder" to delete f
            end timeout
        on error m
            logger(m)
        end try
    end repeat
end move_the_files

to fig_new_loc(f)
    return (newDirectory & (last item of text_to_list(f, ":"))) as text
end fig_new_loc

on quofo(t)
    return (quoted form of POSIX path of (t as text))
end quofo

on ok_v(v)
    if (do shell script ("echo " & quoted form of (v as text) & "|cut -d . -f 1")) as integer < 10 then
        display dialog return & "This script requires iTunes v10.0 or better..." buttons {"Cancel"} default button 1 with icon 0 giving up after 15 --with title my_title
    end if
end ok_v

to relocate(t, nf)
    tell application "iTunes" to set location of t to (nf as alias)
end relocate

on replace_chars(txt, srch, repl)
    set text item delimiters to srch
    set item_list to every text item of txt
    set text item delimiters to repl
    set txt to item_list as string
    set text item delimiters to ""
    return txt
end replace_chars

on text_to_list(txt, delim)
    set saveD to text item delimiters
    try
        set text item delimiters to {delim}
        set theList to every text item of txt
    on error errStr number errNum
        set text item delimiters to saveD
        error errStr number errNum
    end try
    set text item delimiters to saveD
    return (theList)
end text_to_list

on list_to_text(theList, delim)
    set saveD to text item delimiters
    try
        set text item delimiters to {delim}
        set txt to theList as text
    on error errStr number errNum
        set text item delimiters to saveD
        error errStr number errNum
    end try
    set text item delimiters to saveD
    return (txt)
end list_to_text

to logger(t)
    log t
    try
        do shell script "logger -t" & space & quoted form of my_title & space & quoted form of (t as text)
    end try
end logger

使用cp unix命令,可以同时复制和重命名。 我稍微更改了您的脚本以减少它,并在主循环中设置了 copy/rename 。删除 "set location" 和执行 shell 脚本 "rm" 之前的“--”。我离开它们是为了更安全地进行调试。

在第一行,我为我的测试分配了 Newdirectory 到我的桌面。你必须调整它。

set NewDirectory to (path to desktop folder from user domain) as string -- my desktop for example

tell application "iTunes"
    ok_v(get version) of me

    set selectedTracks to selection
    if selectedTracks is {} then return
    if (get class of (item 1 of selectedTracks)) is not file track then return

    set originalLocs to {}
    repeat with i from 1 to (count of selectedTracks)
        tell item i of selectedTracks to set {TName, Tloc, Tartist} to {get name, get location, get artist}
        if Tloc is missing value then
            display dialog "The file for \"" & TName & "\" appears to be missing. ..." buttons {"Cancel", "OK, Skip It"} default button 1 with icon 2 giving up after 30 with title "Need a decision..."
        end if
        tell application "Finder" to set FExt to name extension of Tloc
        set NewName to NewDirectory & Tartist & "_" & TName & "." & FExt
        do shell script "cp " & quoted form of (POSIX path of Tloc) & " " & quoted form of (POSIX path of NewName)
        --set location of (item i of selectedTracks) to NewName
        --do shell script "rm " & quoted form of (posix path of Tloc)
    end repeat
end tell

on ok_v(v)
if (do shell script ("echo " & quoted form of (v as text) & "|cut -d . -f 1")) as integer < 10 then
    display dialog return & "This script requires iTunes v10.0 or better..." buttons {"Cancel"} default button 1 with icon 0 giving up after 15 --with title my_title
end if
end ok_v