playlist text export - weird characters in text file

Hi, I am trying to export all of the track names of a given playlist to a text file.

It results in my text file starting with “listutxt” (and some funky characters) and each additional entry with “utxt” and I don’t know why this is happening… I tried pasting the output of my text file in here both with quote and without, both result in this message board freaking out and erasing my message after “listutxt”… What is the deal?!

– Anyway, I also was originally trying to have this script export the tracks of the currently selected playlist, but I could not get current playlist to work… Can anyone advise on this?

set playlistInfo to {}

tell application "iTunes"
	activate
	set x to text returned of (display dialog "enter playlist's name" default answer "")
	set myTracks to playlist x's tracks
	repeat with theTrack in the myTracks
		set myName to theTrack's name
		set myArtist to theTrack's artist
		set myAlbum to theTrack's album
		set info to "Song: " & myName & ", Artist: " & myArtist & " (from the album: " & myAlbum & ")" & return & return
		
		copy info to the end of playlistInfo
	end repeat
	set folderchoice to choose folder default location (path to home folder) with prompt "Select folder where playlist contents are to be saved" without invisibles
	set folderchoice to (folderchoice & "itunes_playlist_export2.txt") as string
	set docName to folderchoice as list
	set docPointer to (open for access docName with write permission)
	set eof of docPointer to 0
	write playlistInfo to docPointer
	close access docPointer
end tell

Hi Patrick,

you’re writing a list to the text file, not plain text.
To avoid the “listutxt” (and some funky characters) flatten the list
I took the liberty to optimize the code a bit


set playlistInfo to {}

tell application "iTunes"
	activate
	set x to choose from list (get name of playlists) with prompt "choose playlist"
	tell result
		if it is false then return
		set x to item 1 of x
	end tell
	repeat with theTrack in playlist x's tracks
		tell contents of theTrack
			copy "Song: " & name & ", Artist: " & artist & " (from the album: " & album & ")" & return to the end of playlistInfo
		end tell
	end repeat
end tell
set {TID, text item delimiters} to {text item delimiters, return}
set playlistInfo to playlistInfo as text
set text item delimiters to TID

set folderchoice to choose folder default location (path to home folder) with prompt "Select folder where playlist contents are to be saved" without invisible
set docName to (folderchoice as text) & "itunes_playlist_export2.txt"
set docPointer to (open for access docName with write permission)
set eof of docPointer to 0
write playlistInfo to docPointer
close access docPointer

ahhh! Ok thank you very much!

-patrick