Remove extra spaces from artists' names in iTunes

Mar 31, '03 09:50:00AM

Contributed by: Anonymous

If you have an iPod you probably noticed that artists with a trailing space or leading space in their name is considered a separate artist from those without such extra spaces. This makes playing all the songs by one artist rather difficult. After being annoyed by this I came up with the following Applescript to strip out leading and trailing spaces from both song names and artist names.

[robg adds: Read the rest of the hint for the script, which I have not tested, beyond checking to insure that it compiles correctly.]

tell application "iTunes"
  repeat with i from 1 to the count of tracks of library playlists
  
    set songname to the name of (track i of library playlists)
    set change_made to false as boolean
  
    -- check leading spaces
    if (length of artistname > 0) then
      repeat
        if not ((ASCII number of character 1 of songname) = 32) then
          -- if first character isn't a space then done
          exit repeat
        end if
    
        -- else remove character
        set songname to (characters 2 through end of songname) as string
        set change_made to true as boolean
      end repeat
       
      -- check trailing spaces
      repeat
        if not ((ASCII number of the last character of songname) = 32) then
          -- if first character isn't a space then done
          exit repeat
        end if
    
        -- else remove character
        set songname to characters 1 through ((length of songname) - 1) of  ¬
          songname as string
        set change_made to true as boolean
      end repeat
    end if
  
    if (change_made = true) then
      set the name of track i of library to songname
    end if
  
    set artistname to the artist of (track i of library playlists) as string
    set change_made to false as boolean
  
    if (length of artistname > 0) then
      -- check leading spaces
      repeat
        if not ((ASCII number of character 1 of artistname) = 32) then
          -- if first character isn't a space then done
          exit repeat
        end if
    
        -- else remove character
        set artistname to (characters 2 through end of artistname) as string
        set change_made to true as boolean
      end repeat
       
      -- check trailing spaces
      repeat
        if not ((ASCII number of the last character of artistname) = 32) then
          -- if first character isn't a space then done
          exit repeat
        end if
    
        -- else remove character
        set artistname to characters 1 through ((length of artistname) - 1) of  ¬
          artistname as string
        set change_made to true as boolean
           end repeat
          
      if (change_made = true) then
        set the name of track i of library to artistname
      end if
    end if
  
  end repeat -- songs
   
end tell

Comments (4)


Mac OS X Hints
http://hints.macworld.com/article.php?story=20030329042654161