1

I'm trying to write an AppleScript to do bulk find-and-replace operations on iTunes track names. Currently, this is my code:

tell application "iTunes"
    set sel to selection of front browser window
    if sel is {} then
        try
            display dialog "Nothing is selected…" buttons {"Quit"} with icon 0
        end try
        return
    end if

    set c to (count of sel)

    set theSearchString to display dialog "Find:" default answer "" --prompt for input to search for

    set theReplacementString to display dialog "Replace with:" default answer "" --prompt for input to search for

    repeat with i from 1 to c --set the movement numbers
        set thisTrack to item i of sel
        set songName to (get name of thisTrack)
        set name of thisTrack to (findAndReplaceInText(songName, text returned of theSearchString, text returned of theReplacementString))

    end repeat

end tell

on findAndReplaceInText(theText, theSearchString, theReplacementString)
    set AppleScript's text item delimiters to theSearchString
    set theTextItems to every text item of theText
    set AppleScript's text item delimiters to theReplacementString
    set theText to theTextItems as string
    set AppleScript's text item delimiters to ""
    return theText
end findAndReplaceInText

Currently, the findAndReplaceInText() function returns error 1708. What did I do wrong? The find-and-replace function is coming from Apple: Mac Automation Scripting Guide- Manipulating Text

1

Technically all you need to do is put my in front of findAndReplaceInText, however this version of the code is IMO a better way to write it.

tell application "iTunes"

    set sel to selection of front browser window
    if sel is {} then
        display dialog "Nothing is selected…" buttons {"Quit"} with icon 0
        return
    end if

    set theSearchString to text returned of (display dialog "Find:" default answer "")
    set theReplacementString to text returned of (display dialog "Replace with:" default answer "")

    repeat with i from 1 to (count of sel)
        set thisTrack to item i of sel
        set name of thisTrack to my findAndReplaceInText((name of thisTrack), theSearchString, theReplacementString)
    end repeat

end tell

on findAndReplaceInText(theText, theSearchString, theReplacementString)
    set AppleScript's text item delimiters to theSearchString
    set theTextItems to every text item of theText
    set AppleScript's text item delimiters to theReplacementString
    set theText to theTextItems as string
    set AppleScript's text item delimiters to ""
    return theText
end findAndReplaceInText

Also note that while your script changes the displayed name of the song, it does not change it as the filesystem level, just the meta-data stored in iTunes.

  • Works like a charm, thanks! I added a selection dialog to do find-and-replace in any metadata field. I already use it tons! – willem.hill Nov 2 '17 at 3:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .