Just started playing around with Apple Script and something I set out to do was to create an extended M3U playlist file of a folder of audio files so that I can play these via VLC instead of having to use iTunes, which I have grown tired of. Just want something simple to play tunes while I putter about on the Mac
I got to this solution with a lot of trial and error (and a great deal of Google searching). I have checking if the m3u file already exists in the folder, but I have not been able to solve how to check this file for any code inside, create any missing links and skip any files already linked within the m3u file? No idea if this is something that can be done within Apple Script? Currently it will just add the new code to the end of the m3u that is already there.
If anyone else wants to use the script I have attached it and if any script masters can add to or simplify this script Iâm more than welcome to hear their comments and suggestions.
Thanks.
-- Scripted created 13th Sept 2001 by Alan Robertson.
-- Script to create an Extended M3U playlist file out of any mp3, m4a or flac files inside the front opened Finder window and then copy the m3u files at end to Music/Playlists folder on local drive for easy access to all playlists. File will be called the name of the original folder.
property type_list : {"mp3", "MP3", "FLAC", "flac", "m4a", "M4A"} -- sets file types to look for
tell application "Finder"
set theFolder to window 1
set thePath to (POSIX path of (target of theFolder as alias))
set isFile to the name of theFolder & ".m3u"
set isThere to POSIX path of thePath & isFile as POSIX file
if exists isThere then
tell application "TextEdit"
activate
open isThere
end tell
else
set theFile to make new file in theFolder
set fileName to the name of theFolder & ".m3u"
set the name of theFile to fileName
set FinalFile to thePath & fileName
tell application "TextEdit"
activate
open FinalFile
end tell
end if
tell application "TextEdit"
activate
write ("#EXTM3U" & return) to isThere starting at eof
set mp3Files to (every file in theFolder whose name extension is in the type_list)
set theCount to number of items in mp3Files
repeat with i from 1 to theCount
set oneFile to item i of mp3Files
set AppleScript's text item delimiters to ":"
set this_parent_dir to (text items 1 through -2 of (oneFile as string)) as string
set this_name to (text item -1 of (oneFile as string)) as string
set this_ext to ""
if this_name contains "." then
set AppleScript's text item delimiters to "."
set this_ext to the last text item of this_name
set final_name to (first text item of this_name) as string
set final_dir to POSIX path of this_parent_dir
end if
write "#EXTINF:0," & (final_name) & return to isThere starting at eof
write (final_dir) & "/" & (this_name) & return to isThere starting at eof
end repeat
tell application "Finder"
copy isThere to playlist
end tell
quit
end tell
end tell
tell application "Finder"
set Folder2 to window 1
set ParentFolder to container of item 1 of Folder2
set playlist to "Macintosh HD:Users:*user name*:Music:Playlists:" -- change to your own folder path
set copyFiles to (every file in ParentFolder whose name extension is "m3u")
move copyFiles to folder playlist
end tell
nice script.
TextEdit is not needed to write out a plain text file.
This version creates the files directly in the Playlists folder
and adds a bit error handling in case there is no open Finder window,
no matching files or an error occurs during the writing process.
The script assumes that the folder Playlists exists in the folder Music of the current user.
By setting the property useFinderWindow to false you can choose the source folder
without using an opened Finder window
property type_list : {"mp3", "MP3", "FLAC", "flac", "m4a", "M4A"} -- sets file types to look for
property useFinderWindow : true -- set to false for choosing the folder via Open dialog box
set playListsFolder to ((path to music folder as text) & "Playlists:") -- assumes that the folder exists !
if not useFinderWindow then set theFolder to choose folder
tell application "Finder"
activate
try
if useFinderWindow then set theFolder to target of window 1
set folderName to name of theFolder
set mp3Files to (every file in theFolder whose name extension is in the type_list)
if (count mp3Files) is 0 then display dialog "no matching music files" buttons {"Cancel"} default button 1
on error
display dialog "no open window" buttons {"Cancel"} default button 1
end try
end tell
set playListFile to playListsFolder & folderName & ".m3u"
try
set fRef to open for access file playListFile with write permission
write ("#EXTM3U" & return) to fRef starting at eof
repeat with i from 1 to (count mp3Files)
set oneFile to item i of mp3Files
set {name:Nm, name extension:Ex} to oneFile
set baseName to text 1 thru ((get offset of "." & Ex in Nm) - 1) of Nm
write "#EXTINF:0," & baseName & return & (POSIX path of (oneFile as text)) & return to fRef starting at eof
end repeat
close access fRef
on error e
try
close access file playListFile
end try
display dialog "an error occured" & return & e buttons {"Cancel"} default button 1
end try
Might take me a while to figure out everything your version does, but it does a better job than mine.
How would I go about adding in a check to see if the m3u file for the folder hasnât already been created and saved in my Playlist folder? That, I think, would make this process complete for my use.
this checks if the file exists.
You can choose to abort the script, overwrite or append the text
property type_list : {"mp3", "MP3", "FLAC", "flac", "m4a", "M4A"} -- sets file types to look for
property useFinderWindow : true -- set to false for choosing the folder via Open dialog box
set playListsFolder to ((path to music folder as text) & "Playlists:") -- assumes that the folder exists !
if not useFinderWindow then set theFolder to choose folder
tell application "Finder"
activate
try
if useFinderWindow then set theFolder to target of window 1
set folderName to name of theFolder
set mp3Files to (every file in theFolder whose name extension is in the type_list)
if (count mp3Files) is 0 then display dialog "no matching music files" buttons {"Cancel"} default button 1
on error number n
if n = -128 then return
display dialog "no open window" buttons {"Cancel"} default button 1
end try
end tell
set playListFile to playListsFolder & folderName & ".m3u"
tell application "Finder"
set playListFileExists to exists file playListFile
if playListFileExists then
set {button returned:buttonReturned} to display dialog "file " & folderName & ".m3u exists" buttons {"Cancel", "Overwrite", "Append"}
set overwriteText to buttonReturned is "Overwrite"
end if
end tell
try
set fRef to open for access file playListFile with write permission
if overwriteText then set eof fRef to 0
write ("#EXTM3U" & return) to fRef starting at eof
repeat with i from 1 to (count mp3Files)
set oneFile to item i of mp3Files
set {name:Nm, name extension:Ex} to oneFile
set baseName to text 1 thru ((get offset of "." & Ex in Nm) - 1) of Nm
write "#EXTINF:0," & baseName & return & (POSIX path of (oneFile as text)) & return to fRef starting at eof
end repeat
close access fRef
on error e
try
close access file playListFile
end try
display dialog "an error occured" & return & e buttons {"Cancel"} default button 1
end try
Thanks again Stefan.
I think it makes sense , Iâll have to study the script more when I get home, but it seems to do everything I want and a whole lot more
Hi, I am really new to AppleScript and I wanted something to convert my folder to an M3U playlist. I run this and it keeps telling me âfoldername.m3uâ is not found even when I create an m3u list myself with the asked name myself. Can someone help me troubleshoot? Great program but I am just an idiot for not understanding how to use itâŚ
It seems the script (creates and) writes to a file in a folder called âPlaylistsâ, which is assumed to exist in the userâs âMusicâ folder. If the folder doesnât exist, the script doesnât create it and you get an error very similar to the one you describe.
Thereâs also a bug in that the âoverwriteTextâ variable used in the version in post #4 is only set if the file already exists, so thereâll be an error there too if it doesnât.
Iâve patched up the code to create the folder and set the variable where necessary, but havenât checked if the file formatâs correct:
property type_list : {"mp3", "MP3", "FLAC", "flac", "m4a", "M4A"} -- sets file types to look for
property useFinderWindow : true -- set to false for choosing the folder via Open dialog box
set musicFolder to (path to music folder as text)
set playListsFolder to (musicFolder & "Playlists:") -- assumes that the folder exists !
if not useFinderWindow then set theFolder to choose folder
tell application "Finder"
activate
try
if useFinderWindow then set theFolder to target of Finder window 1
set folderName to name of theFolder
set mp3Files to (every file in theFolder whose name extension is in the type_list)
if (count mp3Files) is 0 then display dialog "no matching music files" buttons {"Cancel"} default button 1
on error number n
if n = -128 then return
display dialog "no open window" buttons {"Cancel"} default button 1
end try
end tell
set playListFile to playListsFolder & folderName & ".m3u"
tell application "Finder"
if (not (exists folder playListsFolder)) then
make new folder at folder musicFolder with properties {name:"Playlists"}
end if
set playListFileExists to exists file playListFile
if playListFileExists then
set {button returned:buttonReturned} to display dialog "file " & folderName & ".m3u exists" buttons {"Cancel", "Overwrite", "Append"}
set overwriteText to buttonReturned is "Overwrite"
else
set overwriteText to true
end if
end tell
try
set fRef to open for access file playListFile with write permission
if overwriteText then set eof fRef to 0
write ("#EXTM3U" & return) to fRef starting at eof
repeat with i from 1 to (count mp3Files)
set oneFile to item i of mp3Files
set {name:Nm, name extension:Ex} to oneFile
set baseName to text 1 thru ((get offset of "." & Ex in Nm) - 1) of Nm
write "#EXTINF:0," & baseName & return & (POSIX path of (oneFile as text)) & return to fRef starting at eof
end repeat
close access fRef
on error e
try
close access file playListFile
end try
display dialog "an error occured" & return & e buttons {"Cancel"} default button 1
end try