2015-08-21 74 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