使用AppleScript重命名iTunes文件

时间:2015-08-21 20:25:03

标签: applescript itunes

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

虽然脚本中早先知道了艺术家和曲目名称,但我不知道如何在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

1 个答案:

答案 0 :(得分:1)

使用cp unix命令,您可以同时复制和重命名。 我改变了一下你的脚本以减少它,我在主循环中设置了复制/重命名。删除&#34; - &#34;在&#34;设置位置&#34;之前和do shell脚本&#34; rm&#34;。我让他们更安全地进行调试。

在第一行,我为我的测试分配了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