DIY Radio Alarm for Your Mac
I’ve always liked waking up to the radio and having it play while I get ready for the day. A gentle wakeup with some background chatter starts the day much better than a boisterous intruding repetitive alarm. After getting too annoyed with my phone alarm, I spent some time getting my Mac to wake me up with the radio. You could probably install an app to start the radio on your phone, but I prefer Mac’s sound quality and the fact that it‘s relatively stationary in some part of the house. Hopefully, this post will save time for anyone interested in waking up to the radio on their Mac.
What follows is a step-by-step process to get your Mac to wake up at a specified time and automatically start a pre-specified radio station in Chrome as long as your Mac is plugged in and the lid is open. Prerequisites are that you know how to run commands from the Terminal and that you have Chrome installed.
Some warnings before you dive in too deep:
- I haven’t determined how to dynamically adjust the volume on wakeup, so you have to set the volume before going to bed.
- It can take quite a lot of searching to find a valid streaming URL for some radio stations.
And without further adieu, we’ll get started by automatically waking up your Mac.
1. Trigger automatic wakeup
Use Spotlight ( CMD + Space
) to enter the Energy Saver
pane. In the bottom right select Schedule
and set your Mac to wake up whenever you please.
Note: Once or twice my Mac has woken up regardless of being unplugged. So by the end of this, you might have a Mac that wakes up spontaneously.
After waking up Chrome has to be launched automatically.
2. Set up scripts to automatically launch Chrome
Download SleepWatcher 2.2.1 (more info here.) and extract SleepWatcher into any folder; for this example, we’ll use the Home directory. Once extracted, install SleepWatcher by running the 3 commands below from your Terminal.
sudo mkdir -p /user/local/sbin /user/local/share/man/man8
sudo cp ~/sleepwatcher_2.2.1/sleepwatcher /usr/local/sbin
sudo cp ~/sleepwatcher_2.2.1/sleepwatcher.8 \ /usr/local/share/man/man8
In your Home directory create a filed called .wakeup
containing the following:
#!/usr/bin/env bash
radio_url=http://c4.prod.playlists.ihrhls.com/305/playlist.m3u8
open -a "Google Chrome" $radio_url
This bash script will open your Chrome browser and launch the online radio station specified by radio_url
. Make the file executable such that SleepWatcher can run it.
sudo chmod +x ~/.wakeup
The following command will put your Mac to sleep and test that the wakeup script works. Run the command and wake up your Mac after a few seconds.
/usr/local/sbin/sleepwatcher --verbose --wakeup ~/.wakeup & pmset sleepnow
If it worked, the next logical step is to be able to decide what radio station you want to listen to.
3. Get the streaming URL of your favourite radio station
The trick here is to find a streaming URL that automatically starts playing the audio when you navigate to it. E.g., clicking on the link below will immediately start playing Capital FM London.
https://media-ssl.musicradio.com/Capital
The general procedure is to find the streaming website of your radio station, inspect the page source, and search for “stream” to find a few candidate URLs to test.
As an example, get the streaming URL for WILD 94.9 by going to the radio station’s live streaming website. Right-click anywhere and select View Page Source
; this will open a new tab filled with Html code. Press CMD + f
to search for the phrase “stream” and continue until you start seeing URLs. Copy a URL that looks like it could be the streaming URL and open it in a new tab. Try this until you find a URL that automatically starts playing the audio. For this example, the first streaming URL works (see image below), but you’ll have to install a plugin first. URLs ending with .m3u8
are by default not played automatically in Chrome and installing the Play HLS M3u8 Chrome extension will enable this behaviour.
After you’ve added your own radio_url
to the .wakeup
script, run the testing sequence described in step 2 again.
Attentive readers would’ve noticed that the radio will start playing every time the Mac wakes up. It took me a few awkward radio alarms in the middle of meetings and flights to realize just how suboptimal this is… As a solution, we’ll specify a permissible alarm time frame.
4. Set a wakeup time frame
To prevent the radio from starting at any time, we update the .wakeup
script to contain the following:
#!/usr/bin/env bash
radio_url=http://c4.prod.playlists.ihrhls.com/305/playlist.m3u8
curtime=$(date +%H:%M)
if [[ "$curtime" > "06:00" ]] && [[ "$curtime" < "08:30" ]]; then
open -a "Google Chrome" $radio_url
fi
Consequently, the radio will only start playing if your Mac was woken between 6 am and 8:30 am. You can change the time frame to anything that suits you, but be sure to allow 1 or 2 minutes after the actual wake up event for the script to execute. (Note: If you try to test the new script, it will only work if you are in the specified time frame. You might also have a few of these wakeup scripts running in the background after testing — restart your computer to clear these.)
Another nice-to-have created by this system is a one-press button to switch on the radio when you wake up too early. I’ve heard that many people wake up before their alarm sounds and if this happens to you, you can start your radio earlier by pressing any button on your sleeping Mac.
And that’s pretty much it. I hope you get some quality morning radio as a result of this trick. Thanks for reading!
It really sucks when a guide doesn’t work out-of-the-box, so please let me know if you run into any problems. Feedback, tips, and corrections are appreciated.
Streaming URLs I’ve used:
Capital FM: https://media-ssl.musicradio.com/Capital
WILD 94.9: http://c4.prod.playlists.ihrhls.com/305/playlist.m3u8
102.7 KIIS-FM: http://c2.prod.playlists.ihrhls.com/185/playlist.m3u8
ALT 98.7: http://c2.prod.playlists.ihrhls.com/201/playlist.m3u8
Note: Media links are handled differently among browsers; try using a different browser if Chrome doesn’t work for your radio station.