An crossed my path the other day. The script is designed to download the latest summary, and feed it to iTunes as the next track. While it certainly did that for me, I wanted to make some modifications:
So, thought I, a chance to put AppleScript to work. AppleScript is the scripting language under the hood of Mac OS X, and has been in previous versions of the Mac OS as well. I haven’t played with it much, but it’s a great way to programmatically control Mac applications.
When I began googling how to do this, I ran across on macosxhints, with another script from Doug, purportedly from the fine folks at , on how to add a track to Party Shuffle. Convoluted, to me, particularly, knowing very little of the language.
So I just started hacking at the original script which plays the podcast next, and combining pieces of it with the Party Shuffle script. It hit me that I didn’t really care what the next tracks after the podcast are: they don’t have to be in any order, and iTunes will happily substitute tracks in Party Shuffle as they get deleted, like smart playlists. So, all I had to do was delete the rest of the tracks in Party Shuffle, then add the podcast. iTunes will take care of re-filling the shuffle.
Here, then, is the script which allows this to work for me, stripped down to its very basic pieces:
property the_subscription : "NPR: Hourly News Summary"
tell application "iTunes"
set podcast_playlist to some playlist whose special kind is Podcasts
set podcasts_to_play to file tracks of podcast_playlist whose played count = 0 and album = the_subscription
set next_track to the location of item 1 of podcasts_to_play
set rest_of_tracks to a reference to (every track of current playlist whose index > 11)
tell playlist "Party Shuffle" to delete rest_of_tracks
set this_track to add next_track to playlist "Party Shuffle"
end tell
It’s a pretty simple script; let’s look at it in some detail.
The property line sets the name of the podcast. Not the actual file, but the subscription. iTunes uses this as the ‘album’ of the files in the podcast. It’s separated out to a property so that I can easily adjust this script to do several podcasts; simply change the property at the top and it will work for another podcast. I’ve got it doing several.
tell application "iTunes" and the corresponding end tell is how AppleScript actually scripts applications. This brings in all of the objects and commands for iTunes into the vocabulary of the script, and sets up the fact that we’ll be controlling iTunes inside the block.
The first three set lines find the podcast that we’ll be adding to the shuffle. First we find the “Podcasts” playlist, then we find the unplayed tracks that match our ‘album’ (the Property set on the first line), then we get the next_track: the actual file we want to play next.
The fourth set line gets all the unplayed tracks in the current playlist. For me, this is Party Shuffle. The index part of that is the order in which the tracks are scheduled to play. This script would be even shorter if AppleScript could simply manipulate this index, but, alas, it seems that index is a read-only property. Eleven is the magic number for me, because I have Party Shuffle to show 10 recently played songs. Therefore, the currently playing track should have an index of 11, and the rest of the tracks greater than 11.
Then another tell command, to tell Party Shuffle to delete those tracks. Notice how close the previous sentence is to the actual code.
The last set line places the podcast, which we put in next_track above, into the newly cleaned out playlist. iTunes will then fill in Party Shuffle the rest of the way with randomly chosen music, based on the configuration of Party Shuffle.
The final trick is to run this script periodically. That’s a job for cron. launchd probably should be used for this task, to make it all Mac OSeXy, but that’s another exercise.
This script works well for me, but it may have to be modified for your particular circumstance. I’ve got several of these scripts scheduled via cron to provide some podcasts intermixed with the music throughout the day, and, if I do say so myself, it’s pretty freakin’ cool. And it happens automatically.
Some of the ways this script could be improved:
All in all, a cool project, and a worthy introduction (for me, anyway) to the sorts of things that AppleScript can do.
You’re free, of course, to use that script however you want; it’s hereby in the Public Domain. But do indeed let me know how you improve it!
UPDATE: See the of this script.