Search code examples
pythonstringapplescriptitunes

Using Regex (or simmilar) to change song name (or other tag) in itunes via script


So I have a scripting issue (and cannot find a specific app to do this in conjunction with itunes).

I can do something like this to any script in Python, Say I have as the song title:

Beat of my heart - original mix

and I want to change it to beat of my heart (original mix)

in Python I would:

string = 'Beat of my heart - original mix'
location = string.index(' - ')
fixed_string = string[,location] + '(' + string[location+3,] + ')'

simple right? But I want to batch this in itunes (on a mac) on the tracks I have labeled as such

Any suggestions?

Thx


Solution

  • With Applescript, you can use Text Item Delimiters:

    set ASTID to AppleScript's text item delimiters
    tell application "iTunes"
        repeat with song in (selection of browser window 1)
            set the_name to name of song
            if the_name contains " - " then
                set AppleScript's text item delimiters to ("- ")
                set the_name to text items of the_name
                set AppleScript's text item delimiters to ("(")
                set the_name to (the_name as string) & ")"
                set name of song to the_name
            end if
        end repeat
        set AppleScript's text item delimiters to ASTID
    end tell