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!