A frequent question online asks how to add a streaming-radio URL to the macOS Music app as a playlist. This script seems to do the job. It has only the most primitive eror-checking and is probably incompetent, but it’s here in case anyone wants to improve it. Some of the crucial details are taken from Doug’s AppleScripts, as specified at the top of the code:
-- based in part on "Name New Playlist From Selection" at Doug's AppleScripts:
-- https://dougscripts.com/itunes/2018/04/name-new-playlist-from-selection-updated/
property myTitle : "Make playlist from URL"
set answer to button returned of (display dialog "You will need the streaming URL, which you can get from fmstream.org. If you already have the URL, click “I have it.”" buttons {"fmstream.org", "I have it", "Cancel"} with title myTitle default button 2)
if answer is "fmstream.org" then
display dialog "On fmstream.org, search for the radio stream, then select and copy the URL from the lower panel of the window." & return & return & "Then restart me." buttons {"OK"} with title myTitle giving up after 10
try
open location "https://fmstream.org"
end try
return
end if
set urlOK to false
repeat until urlOK is true
set clipText to the clipboard as text
display dialog "Enter the streaming URL:" default answer clipText with title myTitle
set theUrl to text returned of result
set theUrl to theUrl as text
if theUrl starts with "http" then
set urlOK to true
else
display dialog "Please enter a valid URL." with title myTitle giving up after 2
end if
end repeat
if theUrl ends with ";" then
set theUrl to text 1 thru -2 of theUrl
end if
set newName to text returned of (display dialog ¬
"Make a name for the playlist:" default answer ¬
"" with title myTitle)
set tmpPath to (path to application support from user domain)
set tmpPOSIX to POSIX path of tmpPath & "PlaylistFromURL"
try
do shell script "mkdir" & space & quoted form of tmpPOSIX
end try
set plsText to quoted form of (tmpPOSIX & "/temp.pls")
try
do shell script "echo [Playlist] >" & space & plsText
do shell script "echo NumberOfEntries=1 >>" & space & plsText
do shell script "echo File1=" & theUrl & space & ">>" & space & plsText
do shell script "echo Title1=" & newName & space & ">>" & space & plsText
do shell script "echo Length1=1 >>" & space & plsText
on error err
display dialog err
end try
try
do shell script "open" & space & plsText
end try
tell application "Music"
activate
try
set theSelection to selection of front browser window
set theSource to container of view of front browser window
if theSelection is {} then error
on error
display dialog "I couldn't open the URL in the Music app." buttons {"Bummer"} with title myTitle giving up after 5
return
end try
set newPlaylist to (make new playlist at theSource with properties ¬
{name:newName})
repeat with aTrack in theSelection
try
duplicate aTrack to newPlaylist
end try
end repeat
reveal newPlaylist
display dialog "I have created the new playlist" & space & newName & return & return & "I can now delete the temporary location of the URL that is in the Internet Songs playlist." & return & return & "If you have some special reason for keeping it in the Internet Songs playlist, click No." buttons {"Yes", "No"} default button 1 with title myTitle
if button returned of result is "Yes" then
try
set myPlaylist to playlist "Internet Songs"
set myTrack to track newName in myPlaylist
delete myTrack
end try
activate
end if
end tell