2015-08-21 71 views
0

我已经有了这个美丽的AppleScript里面的iTunes,将选定的文件重定位到我的圣乐音乐文件夹。不过,我也想重命名文件为{Artist} - {Trackname}.extension(扩展名是99%的时间)。用AppleScript重命名iTunes文件

尽管艺术家和曲目名称在脚本的早些时候已知,但我不知道如何在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

用cp unix命令,你可以复制,并在同一时间重新命名。 我改变了一下你的脚本以减少它,并且我在主循环中设置了复制/重命名。删除“设置位置”和do 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