Writing a script to help me keep my playlists synchronised between to computers.
I figured I’d do it via applescript.
The first half is the exporting to m3u, which is what I’m getting stuck with.
The code is:
property delimiter_character : " - "
tell application "iTunes"
set this_playlist to playlist "Alternative Mixtape"
set this_name to (the name of this_playlist) as string
set the playlist_count to the count of tracks of this_playlist
set playlist_data to {}
tell this_playlist
repeat with i from 1 to the count of tracks
tell track i
set the end of the playlist_data to {name, delimiter_character, artist, return, location, return}
end tell
end repeat
end tell
end tell
set FileName to "Path:To:File.m3u"
set theFile to open for access FileName with write permission
write playlist_data to theFile
close access theFile
Problem is that I get all sorts of “garbled” output:
listlistutxt Hips Of The Yearutxt - utxtMistutxt
alisvvHDD…ÏXËH+Ï›Hips Of The Year.mp3χ»g∏mMp3 hookˇˇˇˇ Bye Bye…Ï<»»gúMϛϋ’.HDD:Music:Mist:Bye Bye:Hips Of The Year.mp3*Hips Of The Year.mp3HDD(/Music/Mist/Bye Bye/Hips Of The Year.mp3
I’ve tried to convert the clipboard to plain text, but I keep getting an error when trying to copy as class UTF8 or as record.
m3u is a text file. Your issue is in your code the playlist_data is created as a list. It’s actually a list-of-lists which is even more complicated. So you are writing a list to a file as text… which is how it gets messed up. Try this code. It creates playlist_data as text instead of a list so it writes to the file properly. I made a couple other optimizations too. I hope it helps.
NOTE: you will have to change the playlistName and filePath to your values.
One final thing to note. You can write the playlist_data list to a file too. You would have to tell the write statement to write the data as a list in this line “write playlist_data to theFile as list”. You haven’t specified anything in the “as” part of that statement and therefore it does the default behavior of writing the file as text. But you can specify “list” if you wanted. You will notice that you won’t be able to read the file with a text editor if you do this, however the advantage is that you can later read that file “as list” back into an applescript and get the data back in list format. This is not appropriate for your task of writing an m3u file though.