-- Adapted from Bery Rinaldo's "Export Playlist to M3U" script. The original copyright-- information for this script is as follows:
-- Export Playlist to M3U
--
-- This AppleScript is used to output the current iTunes playlist into an M3U format file suitable for use with the AudioTron.
-- Copyright (C) 2002 Bery Rinaldo
--
-- This program is free software; you can redistribute it and/or modify it under the terms
-- of the GNU General Public License as published by the Free Software Foundation; either
-- version 2 of the License, or (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
-- without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
-- See the GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License along with this program; if not,
-- write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-- Script written by Bery Rinaldo beryrinaldo@mac.com on 18-March-2002
-- Script modified by Jan Philipp Köhler pk2061@gmx.de in October 2014
--
property |linefeed| : ASCII character 10
try
with timeout of 1800 seconds
tell application "iTunes"
set playlist_names to name of user playlists
-- set the playlist_text to "#CURTRACK 0" & |linefeed|
set the playlist_text to "#EXTM3U" & |linefeed|
set the playlist_name to item 1 of (choose from list playlist_names)
-- set the export_folder to POSIX path of (choose folder)
set the export_folder to "/Volumes/music/playlists/"
set the target_file to export_folder & "temp.m3u" as Unicode text
-- tell application "System Events" to set fileExists to exists disk item (my POSIX file target_file as string)
-- if the result then
-- display dialog "There is already a file named " & playlist_name & ".m3u. Do you want to replace the file?" buttons {"Cancel", "Proceed"} cancel button 1 with icon caution
-- end if
tell playlist playlist_name
set the track_count to the count of tracks
end tell
repeat with z from 1 to the track_count
tell track z of playlist playlist_name
copy {location} to {loc}
end tell
set name_z to name of (track z of playlist playlist_name)
set time_z to duration of (track z of playlist playlist_name)
set time_z to round (time_z) rounding down
set the posixloc to POSIX path of loc as text
set new_posixloc to my replace_chars(posixloc, "/Volumes/music", "..")
set encoded_posixloc to my encode_text(new_posixloc, true, false)
set the playlist_text to the playlist_text & "#EXTURL:file://" & encoded_posixloc & |linefeed|
set the playlist_text to the playlist_text & "#EXTINF:" & time_z & "," & name_z & |linefeed|
set the playlist_text to the playlist_text & new_posixloc & |linefeed|
end repeat
end tell
set this_result to my write_to_file(playlist_text, POSIX file target_file)
if this_result is not true then
error this_result -- There was a problem writing the playlist file.
end if
-- Do real UTF-8 Conversion:
do shell script "iconv -f UTF8-MAC -t UTF8 \"" & target_file & "\" > \"" & export_folder & playlist_name & ".m3u\""
do shell script "rm \"" & target_file & "\""
end timeout
on error error_message number error_number
tell application "iTunes"
if the error_number is not -128 then
beep
display dialog error_message buttons {"Cancel"} default button 1
end if
end tell
end try
on write_to_file(this_data, target_file)
try
set the target_file to the target_file as Unicode text
set the open_target_file to open for access file target_file with write permission
set eof open_target_file to 0
--set BOM_UTF8 to ((ASCII character 239) & (ASCII character 187) & (ASCII character 191))
--write BOM_UTF8 to open_target_file
write this_data to the open_target_file as «class utf8»
close access the open_target_file
return true
on error error_message
try
close access file target_file
end try
return error_message
end try
end write_to_file
on replace_chars(this_text, search_string, replacement_string)
set AppleScript's text item delimiters to the search_string
set the item_list to every text item of this_text
set AppleScript's text item delimiters to the replacement_string
set this_text to the item_list as string
set AppleScript's text item delimiters to ""
return this_text
end replace_chars
-- this sub-routine is used to encode text
on encode_text(this_text, encode_URL_A, encode_URL_B)
set the standard_characters to "abcdefghijklmnopqrstuvwxyz0123456789"
set the URL_A_chars to "$+!'?;&@=#%><{}[]\"~`^\\|*"
set the URL_B_chars to ".-_:/()"
set the acceptable_characters to the standard_characters
if encode_URL_A is false then set the acceptable_characters to the acceptable_characters & the URL_A_chars
if encode_URL_B is false then set the acceptable_characters to the acceptable_characters & the URL_B_chars
set the encoded_text to ""
repeat with this_char in this_text
if this_char is in the acceptable_characters then
set the encoded_text to (the encoded_text & this_char)
else
set the encoded_text to (the encoded_text & encode_char(this_char)) as string
end if
end repeat
return the encoded_text
end encode_text
on encode_char(this_char)
set the ASCII_num to (the ASCII number this_char)
set the hex_list to {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}
set x to item ((ASCII_num div 16) + 1) of the hex_list
set y to item ((ASCII_num mod 16) + 1) of the hex_list
return ("%" & x & y) as string
end encode_char
try
set fileobj to open for access filepath with write permission
set eof of fileobj to 0
set BOM_UTF8 to ((ASCII character 239) & (ASCII character 187) & (ASCII character 191))
write BOM_UTF8 to fileobj
write filecont to fileobj as «class utf8»
close access fileobj
on error errmsg
try
close access fileobj
end try
end try