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


Click here to return to the 'Improved script: 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.
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 | # ]