Search code examples
applescriptitunes

Rename iTunes files with AppleScript


I've got this beautiful AppleScript inside iTunes which relocates selected files to my holy music folder. However I'd also like to rename the file to {Artist} - {Trackname}.extension (extension is mp3 99% of the time).

While artist and trackname are known earlier in the script I've got no clue how to also make them available in the move_the_files-function.

I hope any AppleScript (self-flagellating) enthusiasts can help me. This is the script:

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

Solution

  • Using cp unix command, you can copy and rename at same time. I changed a bit your script to reduce it and I set the copy/rename inside the main loop. remove the "--" before the "set location" and the do shell script "rm". I leave them to be more safe for debugging.

    On first row, I assigned, for my tests, the Newdirectory to my desktop. you must adjust this.

    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