Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Remove extra spaces from artists' names in iTunes Apps
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
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
 
[15,627 views]  

Remove extra spaces from artists' names in iTunes | 4 comments | Create New Account
Click here to return to the 'Remove extra spaces from artists' names in iTunes' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Remove extra spaces from artists' names in iTunes
Authored by: DougAdams on Mar 31, '03 12:02:16PM
Great script.

For more examples of AppleScripts that work with track names, check out our Managing Track Info page.

Doug
Doug's AppleScripts for iTunes

---



[ Reply to This | # ]

Remove extra spaces from artists' names in iTunes
Authored by: ClarkGoble on Mar 31, '03 02:41:55PM

I just noticed a small typo at the top:

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

That should be

if (length of songname >0 ) then


The point of all that is to skip those tracks that don't have either a song name or artist name. Lacking song names isn't likely, so the bug probably won't affect anything.



[ Reply to This | # ]
Remove extra spaces from artists' names in iTunes
Authored by: DougAdams on Mar 31, '03 06:06:45PM

Script Editor would have fixed that before attempting to compile.





---



[ Reply to This | # ]
Improved script: Remove extra spaces from artists' names in iTunes
Authored by: kopf on Apr 01, '03 01:36:05AM
Thanks for posting this script! It didn't work on my system (OX 10.2.4 with iTunes 3.0.1). The different issues are described below for anyone interested. Here is a modified script that worked for me:
set n to 0
set myNames to ""

tell application "iTunes" to set trackList to tracks of library playlists

repeat with allTracks in trackList
	repeat with myTrack in allTracks
		
		tell application "iTunes" to set songname to the name of myTrack
		set change_made to false
		
		-- check leading spaces
		if (length of songname > 0) then
			repeat
				if text 1 through 1 of songname is not " " then
					-- if first character isn't a space then done
					exit repeat
				end if
				
				-- else remove character
				set songname to text 2 through -1 of songname
				set change_made to true
			end repeat
			
			-- check trailing spaces
			repeat
				if text -1 through -1 of songname is not " " then
					-- if last character isn't a space then done
					exit repeat
				end if
				
				-- else remove character
				set songname to characters 1 through -2 of songname as string
				set change_made to true
			end repeat
			
			if change_made then
				tell application "iTunes" to set the name of myTrack to songname
				set myNames to myNames & return & "Title: " & songname
				beep
				set n to n + 1
			end if
		end if
		
		tell application "iTunes" to set artistname to the artist of myTrack
		set change_made to false
		
		if (length of artistname > 0) then
			-- check leading spaces
			repeat
				if text 1 through 1 of artistname is not " " then
					-- if first character isn't a space then done
					exit repeat
				end if
				
				-- else remove character
				set artistname to text 2 through -1 of artistname
				set change_made to true
			end repeat
			
			-- check trailing spaces
			repeat
				if text -1 through -1 of artistname is not " " then
					-- if last character isn't a space then done
					exit repeat
				end if
				
				-- else remove character
				set artistname to text 1 through -2 of artistname
				set change_made to true
			end repeat
			
			if change_made then
				tell application "iTunes" to set the artist of myTrack to artistname
				set myNames to myNames & return & "Artist: " & artistname
				beep
				set n to n + 1
			end if
		end if
		
	end repeat -- tracks
end repeat -- track lists

if n > 0 then tell me to display dialog "" & n & " items were changed." & myNames
The details:

First, the original script ran for about a second and did not do anything, not even to test tracks with altered names with leading and trailing spaces. When I tested the main loop it did not produce a running list of track names. Did this work for anyone?
tracks of library playlists
produces a list with one item which again is a list with references to the individual tracks. I needed a double imbedded loop like so
tell application "iTunes" to set trackList to tracks of library playlists
repeat with allTracks in trackList
	repeat with myTrack in allTracks
...
The script also suffers from some semantic flaws that often slow down scripts by an order of magnitude.
tell app "iTunes"
should only bracket statements that need to talk to iTunes, i.e. for getting or setting track info. All the other statements should be outside these tell blocks in order to avoid unnecessary AppleEvents being sent around the system. The construct
set songname to (characters 2 through end of songname) as string
first converts the song name string to a list of individual characters and then converts it back to a string (hence the need for the "as string" coercion). The shorter (and much faster executing) syntax for removing the first character is
set songname to text 2 through -1 of songname
and similarly to remove the last character
set songname to text 1 through -2 of songname
This may not make a noticeable difference on all your 1GHz+ G4's but made things at least a little faster on my '97 vintage G3. Finally I put in some beeps so I know that the script is doing something and a final dialog telling me what was fixed. (That dialog will not work if there are too many items ...) Structured programming buffs probably would have made the space stripping code into a handler, but I had to stop somewhere ;-)

Hope this helps,

Klaus

[ Reply to This | # ]