Itunes list of Artist, Album AND Genre

I got this list off of the awesome Dougscripts page. It creates a text file from itunes of just the Artist and Album.

I would also like it to include Genre. Can someone add that for me?

I am obsessive about giving all my songs genres then creating genre playlists. I am hoping to create a database off all my music and sort it by genre, artist then album.

I have assigned more than one genre to some songs so I may end up with a crazy mess but I am willing to take a chance :slight_smile:

thanks in advance for any help or advice you can give.

cindy in indy

global new_contents, file_name, useC, allAlbumTracks
tell application "iTunes"
	set theLibrary to a reference to (get view of front window)
	set numberOfLibTracks to (count of every track in theLibrary)
	set allAlbums to {}
	set new_contents to {}
	set track_separator to ", "
	
	set progressFactor to 10
	
	--Q & A
	set opt1a to "Album-Artist"
	set opt1b to "Artist-Album"
	set theOrder to true
	
	if the button returned of (display dialog "Selected: " & (name of theLibrary as string) & return & return & ¬
		"Arrange by:" buttons {"Cancel", opt1a, opt1b} giving up after 300 with icon 1) is opt1b then set theOrder to false
	
	set useC to button returned of (display dialog "Place track contents of each album in third tabbed column?" buttons {"Cancel", "No", "Yes"} giving up after 300 with icon 1) as string
	my get_the_file_name()
	
	repeat with i from 1 to numberOfLibTracks
		try
			set aTrack to (a reference to track i of theLibrary)
			copy (aTrack's album as string) to aTracksAlbum
			if aTracksAlbum is not "" then
				if allAlbums does not contain aTracksAlbum then
					copy aTracksAlbum to end of allAlbums
					
					if (aTrack's artist as string) is not "" then
						set aTracksArtist to (aTrack's artist as string)
					else
						set aTracksArtist to ("*unknown artist*")
					end if
					copy aTracksArtist to ArtistName
					copy aTracksAlbum to AlbumTitle
					
					if theOrder then
						my add_to_contents(aTracksAlbum & tab & aTracksArtist)
					else
						my add_to_contents(aTracksArtist & tab & aTracksAlbum)
					end if
					if useC is "Yes" then
						copy (name of every track of theLibrary whose album is AlbumTitle and artist is ArtistName) to allAlbumTracks
						copy (my ListToText(allAlbumTracks, track_separator)) to allAlbumTracks
						-- now write this
						my add_to_contents(tab & allAlbumTracks)
						if frontmost and (length of allAlbums) mod progressFactor = 0 then display dialog ((length of allAlbums) as string) & " albums looked at..." buttons {"¢"} giving up after 1
						
					end if
					my add_to_contents(return)
					
				end if
			end if
		end try
	end repeat
	my Make_The_File(file_name as string, new_contents as string)
end tell
display dialog "Done!" buttons {"Thanks"} default button 1 with icon 1 giving up after 300

-- create file in Finder
to Make_The_File(theTextFile, xx)
	--	tell application "Finder"
	try
		copy (open for access file theTextFile with write permission) to fileRef
		write xx to fileRef
		close access fileRef
	on error errx number errNum
		close access fileRef
		display dialog "There has been an error creating the file:" & return & return & errx & return & "error number: " & errNum buttons {"Cancel"}
	end try
	--	end tell
end Make_The_File

-- choose file
to get_the_file_name()
	set file_name to (choose file name with prompt ¬
		"Enter a name for the exported file" default name "")
	if file_name is false then error number -128 -- cancel
	return file_name
end get_the_file_name

on ListToText(theList, theDelimiter)
	set saveDelim to AppleScript's text item delimiters
	try
		set AppleScript's text item delimiters to {theDelimiter}
		set theText to theList as text
	on error errStr number errNum
		set AppleScript's text item delimiters to saveDelim
		error errStr number errNum
	end try
	set AppleScript's text item delimiters to saveDelim
	return (theText)
end ListToText

-- add text to eventual contents
to add_to_contents(x)
	copy x to end of new_contents
end add_to_contents

A very quick edit, Should work.
I took some liberties with also changing the layout.

You can change the:
set LineStart to “< ¢ "
set LineEnd to " ¢ >”
set fieldSep to " ¢ "

to what ever you want.
EDIT found a misspell that would cause an error. FIXED

global new_contents, file_name, useC, allAlbumTracks
tell application "iTunes"
	set theLibrary to a reference to (get view of front window)
	set numberOfLibTracks to (count of every track in theLibrary)
	set allAlbums to {}
	set new_contents to {}
	set track_separator to ","
	set LineStart to "< ¢  "
	set LineEnd to "  ¢ >"
	set fieldSep to " ¢ " 
	set progressFactor to 10
	
	--Q & A
	set opt1a to "Album-Genre-Artist"
	set opt1b to "Artist-Album-Genre"
	set theOrder to true
	
	if the button returned of (display dialog "Selected: " & (name of theLibrary as string) & return & return & ¬
		"Arrange by:" buttons {"Cancel", opt1a, opt1b} giving up after 300 with icon 1) is opt1b then set theOrder to false
	
	set useC to button returned of (display dialog "Place track contents of each album in third tabbed column?" buttons {"Cancel", "No", "Yes"} giving up after 300 with icon 1) as string
	my get_the_file_name()
	
	repeat with i from 1 to numberOfLibTracks
		try
			set aTrack to (a reference to track i of theLibrary)
			copy (aTrack's album as string) to aTracksAlbum
			copy (aTrack's genre as string) to aTracksgenre
			log
			if aTracksAlbum is not "" then
				if allAlbums does not contain aTracksAlbum then
					copy aTracksAlbum to end of allAlbums
					
					if (aTrack's artist as string) is not "" then
						set aTracksArtist to (aTrack's artist as string)
					else
						set aTracksArtist to ("*unknown artist*")
					end if
					copy aTracksArtist to ArtistName
					copy aTracksAlbum to AlbumTitle
					copy aTracksgenre to Albumgenre
					if theOrder then
						my add_to_contents(LineStart & aTracksAlbum & fieldSep & Albumgenre & fieldSep & aTracksArtist)
					else
						my add_to_contents(LineStart & aTracksArtist & fieldSep & aTracksAlbum & fieldSep & Albumgenre)
					end if
					if useC is "Yes" then
						copy (name of every track of theLibrary whose album is AlbumTitle and artist is ArtistName) to allAlbumTracks
						copy (my ListToText(allAlbumTracks, track_separator)) to allAlbumTracks
						-- now write this
						my add_to_contents(tab & allAlbumTracks)
						if frontmost and (length of allAlbums) mod progressFactor = 0 then display dialog ((length of allAlbums) as string) & " albums looked at..." buttons {"¢"} giving up after 1
						
					end if
					my add_to_contents(LineEnd & return)
					
				end if
			end if
		end try
	end repeat
	my Make_The_File(file_name as string, new_contents as string)
end tell
display dialog "Done!" buttons {"Thanks"} default button 1 with icon 1 giving up after 300

-- create file in Finder
to Make_The_File(theTextFile, xx)
	--    tell application "Finder"
	try
		copy (open for access file theTextFile with write permission) to fileRef
		write xx to fileRef
		close access fileRef
	on error errx number errNum
		close access fileRef
		display dialog "There has been an error creating the file:" & return & return & errx & return & "error number: " & errNum buttons {"Cancel"}
	end try
	--    end tell
end Make_The_File

-- choose file
to get_the_file_name()
	set file_name to (choose file name with prompt ¬
		"Enter a name for the exported file" default name "")
	if file_name is false then error number -128 -- cancel
	return file_name
end get_the_file_name

on ListToText(theList, theDelimiter)
	set saveDelim to AppleScript's text item delimiters
	try
		set AppleScript's text item delimiters to {theDelimiter}
		set theText to theList as text
	on error errStr number errNum
		set AppleScript's text item delimiters to saveDelim
		error errStr number errNum
	end try
	set AppleScript's text item delimiters to saveDelim
	return (theText)
end ListToText

-- add text to eventual contents
to add_to_contents(x)
	copy x to end of new_contents
end add_to_contents

Changed the Layout if the album contents are included.

Again you can set the
set TracksIncloseStart to " ¢ [ "
set TracksIncloseEnd to " ]"
to whatever you want.

global new_contents, file_name, useC, allAlbumTracks
tell application "iTunes"
	set theLibrary to a reference to (get view of front window)
	set numberOfLibTracks to (count of every track in theLibrary)
	set allAlbums to {}
	set new_contents to {}
	set track_separator to ","
	set LineStart to "< ¢  "
	set LineEnd to "  ¢ >"
	set fieldSep to " ¢ " --as string
	set TracksIncloseStart to " ¢ [ "
	set TracksIncloseEnd to " ]"
	set progressFactor to 10
	
	
	--Q & A
	set opt1a to "Album-Genre-Artist"
	set opt1b to "Artist-Album-Genre"
	set theOrder to true
	
	if the button returned of (display dialog "Selected: " & (name of theLibrary as string) & return & return & ¬
		"Arrange by:" buttons {"Cancel", opt1a, opt1b} giving up after 300 with icon 1) is opt1b then set theOrder to false
	
	set useC to button returned of (display dialog "Place track contents of each album in third tabbed column?" buttons {"Cancel", "No", "Yes"} giving up after 300 with icon 1) as string
	my get_the_file_name()
	
	repeat with i from 1 to numberOfLibTracks
		--try
		set aTrack to (a reference to track i of theLibrary)
		copy (aTrack's album as string) to aTracksAlbum
		copy (aTrack's genre as string) to aTracksgenre
		if aTracksgenre is "" then set aTracksgenre to ("*No Genre*")
		
		if aTracksAlbum is not "" then
			if allAlbums does not contain aTracksAlbum then
				copy aTracksAlbum to end of allAlbums
				
				if (aTrack's artist as string) is not "" then
					set aTracksArtist to (aTrack's artist as string)
				else
					set aTracksArtist to ("*unknown artist*")
				end if
				copy aTracksArtist to ArtistName
				copy aTracksAlbum to AlbumTitle
				copy aTracksgenre to Albumgenre
				log theOrder
				if theOrder then
					
					my add_to_contents(LineStart & aTracksAlbum & fieldSep & Albumgenre & fieldSep & aTracksArtist)
					
				else
					my add_to_contents(LineStart & aTracksArtist & fieldSep & aTracksAlbum & fieldSep & Albumgenre)
					
				end if
				if useC is "Yes" then
					copy (name of every track of theLibrary whose album is AlbumTitle and artist is ArtistName) to allAlbumTracks
					set allAlbumTracksSep to TracksIncloseStart & allAlbumTracks & TracksIncloseEnd
					copy (my ListToText(allAlbumTracksSep, track_separator)) to allAlbumTracks
					
					-- now write this
					my add_to_contents(allAlbumTracksSep)
					if frontmost and (length of allAlbums) mod progressFactor = 0 then display dialog ((length of allAlbums) as string) & " albums looked at..." buttons {"¢"} giving up after 1
					
				end if
				my add_to_contents(LineEnd & return)
				
			end if
		end if
		--end try
	end repeat
	my Make_The_File(file_name as string, new_contents as string)
end tell
display dialog "Done!" buttons {"Thanks"} default button 1 with icon 1 giving up after 300

-- create file in Finder
to Make_The_File(theTextFile, xx)
	--    tell application "Finder"
	try
		copy (open for access file theTextFile with write permission) to fileRef
		write xx to fileRef
		close access fileRef
	on error errx number errNum
		close access fileRef
		display dialog "There has been an error creating the file:" & return & return & errx & return & "error number: " & errNum buttons {"Cancel"}
	end try
	--    end tell
end Make_The_File

-- choose file
to get_the_file_name()
	set file_name to (choose file name with prompt ¬
		"Enter a name for the exported file" default name "")
	if file_name is false then error number -128 -- cancel
	return file_name
end get_the_file_name

on ListToText(theList, theDelimiter)
	set saveDelim to AppleScript's text item delimiters
	try
		set AppleScript's text item delimiters to {theDelimiter}
		set theText to theList as text
		
	on error errStr number errNum
		set AppleScript's text item delimiters to saveDelim
		error errStr number errNum
	end try
	set AppleScript's text item delimiters to saveDelim
	return (theText)
end ListToText

-- add text to eventual contents
to add_to_contents(x)
	copy x to end of new_contents
end add_to_contents