Double variables and trouble with a repeat

Hi, I’m currently working on this Applescript to create an XML file from 21 separate iTunes playlists for an online music library. I’ve run into a couple of stumbling blocks and was hoping someone might have an answer.

  1. I would like the main section to repeat. Is there a way to enclose both the “getting info from iTunes part” and the “write to a text file part” in a repeat statement? I have to have an “end tell” before the “on write_to_file” so enclosing everything in a repeat statement doesn’t work.

  2. As the script repeats, when it gets to the last track of the first playlist (“subgenre_1”), it will move on to the next playlist (“subgenre_2”). I’d like to define a vairable called “current_plist” that will change through the different playlist names I defined (e.g. “01. Test Name 1”, “02. Test Name 2”, etc.) so that the line:

    set track_name to (get name of file track Plist_count of user playlist subgenre_1)

would be:

set track_name to (get name of file track Plist_count of user playlist current_plist)

I tried:

set current_plist to (“subgenre_” & Plist_count)

but that returns a literal value of “subgenre_1”. I’d like it to be seen as the VARIABLE “subgenre_1” which would then return a literal value of “01. Test Name 1”.

I hope that makes some kind of sense.

Thanks.

--set these variables
set subgenre_1 to "01. Test Name 1"
set subgenre_2 to "02. Test Name 2"
set subgenre_3 to "03. Test Name 3"
set subgenre_4 to "04. Test Name 4"
set subgenre_5 to "05. Test Name 5"
set subgenre_6 to "06. Test Name 6"
set subgenre_7 to "07. Test Name 7"
set subgenre_8 to "08. Test Name 8"
set subgenre_9 to "09. Test Name 9"
set subgenre_11 to "10. Test Name 10"
set subgenre_12 to "11. Test Name 11"
set subgenre_13 to "12. Test Name 12"
set subgenre_14 to "13. Test Name 13"
set subgenre_15 to "14. Test Name 14"
set subgenre_16 to "15. Test Name 15"
set subgenre_17 to "16. Test Name 16"
set subgenre_18 to "17. Test Name 17"
set subgenre_19 to "18. Test Name 18"
set subgenre_20 to "19. Test Name 19"
set subgenre_21 to "20. Test Name 20"

set Plist_num to 21
--end set variables

--begin script
set Plist_count to 1

tell application "iTunes"
	get count of file tracks of user playlist subgenre_1
	set track_name to (get name of file track Plist_count of user playlist subgenre_1)
	set track_genre to (get grouping of file track Plist_count of user playlist subgenre_1)
	set track_subgenre to (get genre of file track Plist_count of user playlist subgenre_1)
	
	--write to TEXT file
	set this_data to "<genre>" & track_genre & "</genre>" & return & "<subgenre>" & track_subgenre & "</subgenre>" & return & "<track>" & track_name & "</track>"
	set this_file to (((path to desktop folder) as text) & "Library.xml")
	my write_to_file(this_data, this_file, true)
end tell

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

Try something like this:

set thesePlaylists to {"01. Test Name 1", "02. Test Name 2", "03. Test Name 3", "04. Test Name 4", "05. Test Name 5", "06. Test Name 6", "07. Test Name 7", "08. Test Name 8", "09. Test Name 9", "10. Test Name 10", "11. Test Name 11", "12. Test Name 12", "13. Test Name 13", "14. Test Name 14", "15. Test Name 15", "16. Test Name 16", "17. Test Name 17", "18. Test Name 18", "19. Test Name 19", "20. Test Name 20"}

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 grouping of thisTrack
			set track_subgenre to genre of thisTrack
			
			--write to TEXT file
			set this_data to "<genre>" & track_genre & "</genre>" & return & "<subgenre>" & track_subgenre & "</subgenre>" & return & "<track>" & track_name & "</track>"
			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

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

Thanks alot, Bruce. That works great. I added a bit of code so that the genre and sub-genre only get written when they change so thought I’d post it (this is just the middle section.)

Thanks, Michael


set new_track_genre to “”
set new_track_subgenre to “”

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 "<track>" & track_name & "</track>" & return
			else
				set this_data to "<subgenre>" & track_subgenre & "</subgenre>" & return & "<track>" & track_name & "</track>" & return
			end if
		else
			set this_data to "<genre>" & new_track_genre & "</genre>" & return & "<subgenre>" & new_track_subgenre & "</subgenre>" & return & "<track>" & track_name & "</track>" & return
		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