Last week, my daughter started Kindergarten. I’m very happy for her, especially because she’s going to an awesome public school. As someone who didn’t grow up in the US, I heard a lot of stories about the American school system but one thing I didn’t know is that kids start so early. My daughter starts at 8:10am! We had to find a way to turn our sleeping-in family members into morning people.
I found a solution to help us: coding and music!
My goal: to wake up my daughter with a custom music playlist starting a few minutes before I go see her.
Apple has some documentation about how to use JS to automate different tasks. To be honest the documentation isn’t really good in comparison to their usual doc. Also, the technical implementation is pretty hacky/buggy but that won’t prevent us to have a bit of fun.
The code to start iTunes and play our playlist if very straight forward:
1 2 3 4 5 6 7 8 9 10
As you probably guessed, this script loads iTunes and then uses its
scripting API to load the main library and look for a playlist called
I’m not proud of the ugly try/catch, but playlist is an instance of
ObjectSpecifier which isn’t evaluated until a method is called on it.
You can think of it as a lazy container. The problem is that if we try
play() on a playlist we didn’t find, then an error is thrown.
I didn’t find a way to check if the underlying value is null so I had to
catch the error.
Once we have the above code, we have two options, put it in a script or
convert it into an app. When developing your automation, it’s highly
recommended to use
Script Editor.app which ships with the OS.
From within the app, you can run your script and test / “debug” it.
Script Editor also allows you to export your script as script, script
bundle and app. The easiest way is to export our code as an app:
Note that you can also write a script and create a
or even evalute your automation JS in the terminal:
So we have a script to start iTunes if needed and play our morning playlist. Now we need to schedule our app to start every school day:
OS X has a builtin scheduling system called launchd. It’s kind of like a cron scheduler but with more options. Unfortunately figuring something as simple as scheduling a recurring script is much harder than it should. So here is my plist file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
Note that I called my app
wakeup.app and I put it in my Applications
folder. My launch agent starts the app when it’s called, but it doesn’t
do that when the system loads the service (
RunAtLoad is set to false).
I’m also logging out stdout and stderr to tmp files so I can debug if
something goes wrong. Finally the schedule is defined in the
key with a daily entry Monday to Friday at 6:55am.
Save the plist file as
wakeup.playlist.itunes.plist and drop it in
and load it via
That’s it, everything should work fine, however if you want to make sure
it will, you might want to unload the plist, edit it so
set to true and reload it. At this point, your playlist should play. If
it doesn’t, then check the log files to see what happened.
When everything is good, unload, go back to the original version and
There is plenty more you can do with JS Automation for Mac, if like me you are listening to a lot of music while coding, you might be interested in knowing that Spotify and VLC are scriptable (and so are most browsers).