Having trouble incorporating handler into repeat loop

Hi. I’m unclear how to incorporate a handler into my script. Not quite sure how add it within a repeat loop. I’ve currently got the following script which goes through a bunch of iTunes playlists and create an XML document. It is"


set thesePlaylists to {"Playlist 1", "Playlist 2", "Playlist 3", "Playlist 4", "Playlist 5"}
set lo_res_folder to "mp3_lo/"
set hi_res_folder to "mp3_hi/"
set aif_folder to "aif/"
set this_data to (("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" & return as string) & return as string)

set new_track_genre to ""
set new_track_subgenre to ""

set this_file to ((path to desktop folder as Unicode text) & "Library.xml")
my write_to_file(this_data, this_file, true)

repeat with thisPlaylist in thesePlaylists
	tell application "iTunes"
		file tracks of user playlist thisPlaylist
		
		repeat with thisTrack in result
			set track_name to name of thisTrack
			set track_genre to new_track_genre
			set new_track_genre to grouping of thisTrack
			set track_subgenre to new_track_subgenre
			set new_track_subgenre to genre of thisTrack
			
			--write to TEXT file
			if new_track_genre = track_genre then
				if new_track_subgenre = track_subgenre then
					set this_data to tab & tab & "<track lo_res=\"" & lo_res_folder & track_name & "_lo.mp3\"" & " hi_res=\"" & hi_res_folder & track_name & ".mp3\"" & " aiff=\"" & aif_folder & track_name & ".aif\"" & ">" & track_name & "</track>" & return as string
				else
					set this_data to tab & "</subgenre>" & return & tab & "<subgenre title=\"" & new_track_subgenre & "\">" & return & tab & tab & "<track lo_res=\"" & lo_res_folder & track_name & "_lo.mp3\"" & " hi_res=\"" & hi_res_folder & track_name & ".mp3\"" & " aiff=\"" & aif_folder & track_name & ".aif\"" & ">" & track_name & "</track>" & return as string
				end if
			else
				if track_genre = "" then
					set this_data to "<genre title=\"" & new_track_genre & "\">" & return & tab & "<subgenre title=\"" & new_track_subgenre & "\">" & return & tab & tab & "<track lo_res=\"" & lo_res_folder & track_name & "_lo.mp3\"" & " hi_res=\"" & hi_res_folder & track_name & ".mp3\"" & " aiff=\"" & aif_folder & track_name & ".aif\"" & ">" & track_name & "</track>" & return as string
				else
					set this_data to tab & "</subgenre>" & return & "</genre>" & return & return & "<genre title=\"" & new_track_genre & "\">" & return & tab & "<subgenre title=\"" & new_track_subgenre & "\">" & return & tab & tab & "<track lo_res=\"" & lo_res_folder & track_name & "_lo.mp3\"" & " hi_res=\"" & hi_res_folder & track_name & ".mp3\"" & " aiff=\"" & aif_folder & track_name & ".aif\"" & ">" & track_name & "</track>" & return as string
				end if
			end if
			set this_file to ((path to desktop folder as Unicode text) & "Library.xml")
			my write_to_file(this_data, this_file, true)
		end repeat
	end tell
end repeat
set this_data to tab & "</subgenre>" & return & "</genre>" as string
set this_file to ((path to desktop folder as Unicode text) & "Library.xml")
my write_to_file(this_data, this_file, true)


on write_to_file(this_data, target_file, append_data)
	try
		set the target_file to the target_file as text
		set the open_target_file to ¬
			open for access file target_file with write permission
		if append_data is false then ¬
			set eof of the open_target_file to 0
		write this_data to the open_target_file starting at eof
		close access the open_target_file
		return true
	on error
		try
			close access file target_file
		end try
		return false
	end try
end write_to_file

display dialog "Finished!" buttons {"Okay"} default button 1 with icon 1

I would like to replace ellipses and spaces in the track, genre and subgenre names with “& #133;” (without the space) and “_” respectively. I saw Kai’s post on find & replace and would like to incorporate his ideas but am having trouble. So:

(* add this *)
property searchList : {“.”, " “}
property replaceList : {”& #133;", “_”}

set thesePlaylists to etc. etc…

repeat with thisPlaylist in thesePlaylists
tell application “iTunes”
file tracks of user playlist thisPlaylist

	repeat with thisTrack in result
		set track_name to name of thisTrack

(* replace any spaces from track_name with “" and any elipses with “& #133;” )
set track_genre to new_track_genre
set new_track_genre to grouping of thisTrack
(
replace any spaces from new_track_genre with "
” and any elipses with “& #133;” )
set track_subgenre to new_track_subgenre
set new_track_subgenre to genre of thisTrack
(
replace any spaces from new_track_subgenre with “_” and any elipses with “& #133;” *)

etc. etc. etc.

Kai’s example seems like it would work, but I’m not sure how to incorporate within a repeat loop and also not sure how to succinctly apply it to three different variables. For reference Kai’s script is:

to switchText of t from s to r
    set text item delimiters to s
    set t to t's text items
    set text item delimiters to r
    tell t to set t to beginning & ({""} & rest)
    t
end switchText

to convertText(t)
    set d to text item delimiters
    considering case
        repeat with n from 1 to count searchList
            set t to switchText of t from my searchList's item n to my replaceList's item n
        end repeat
    end considering
    set text item delimiters to d
    t
end convertText

Thanks for any help!

Hi,

kai’s subroutines are perfect for this.
Try this

set track_name to convertText(name of thisTrack)
            set track_genre to new_track_genre
            set new_track_genre to convertText(grouping of thisTrack)
            set track_subgenre to new_track_subgenre
            set new_track_subgenre to convertText(genre of thisTrack)

2 Notes: You should remove the space character in “& #133;” to “…”
and you should save the xml-File in UTF-8 encoding, as specified in the header.
The normal write routine saves in MacRoman

.
 write this_data to the open_target_file starting at eof as «class utf8»
.

Thanks Stefan. Everything is making sense to me except for where in my script this goes:

to switchText of t from s to r
set text item delimiters to s
set t to t's text items
set text item delimiters to r
tell t to set t to beginning & ({""} & rest)
t
end switchText

to convertText(t)
set d to text item delimiters
considering case
repeat with n from 1 to count searchList
set t to switchText of t from my searchList's item n to my replaceList's item n
end repeat
end considering
set text item delimiters to d
t
end convertText

If I place it before the repeat loops I get the following error: “iTunes got an error: Can’t continue convertText.” If I place it within the repeat loop, the script won’t compile since it’s looking for an “end” before the “to”.

to switchText. and to convertText(t) are subroutines.
They are normally placed either at the beginning or at the end of the main script.
Within an application’s tell block a subroutine must be called with my handler() ,
because handlers belong to Applescript itself.

For your example


.
set track_name to my convertText(name of thisTrack)
set track_genre to new_track_genre
set new_track_genre to my convertText(grouping of thisTrack)
set track_subgenre to new_track_subgenre
set new_track_subgenre to my convertText(genre of thisTrack)
.

Thanks, Stefan. Works great! And thanks for noticing the need for “as «class utf8»”.

Michael