November 30 2010 - 9:48 am

Create Default Smart Playlist Templates

A problem with Holiday music hanging around your iTunes library all year long is that, without a cautious Smart Playlist strategy, Grandma Got Runover by Mannheim the Christmas Donkey Steamroller can disrupt your tranquility at any time of year. Like pool-side in August. And there is nothing un-cooler than that.

Now it's unlikely that I would create a regular playlist containing any speck of Holiday music during non-Holiday times of year on purpose. But you gotta watch them Smart Playlists. Thus, for a while, the criteria for every new Smart Playlist I created started with "Genre does not contain Holiday|Xmas|Christmas".

That's a pain to have to remember. And to enter for every new Smart Playlist.

However. The script Export Smart Playlist Criteria allowed me to export a Smart Playlist template that has several of my preferred Smart Playlist criteria already entered. These would include "Genre does not contain Holiday|Xmas|Christmas", "Live Update", and several other idiosyncratic restrictions and requirements. Now whenever I want to quickly create a new Smart Playlist I import that template XML "starter" and then choose "Edit Smart Playlist" from its contextual menu to add only minor finessing.

Now, whether you care to read on depends on whether you are thinking either:

"Dude, you can just keep a generic Smart Playlist in your sidebar and Duplicate it like whenever."

(in which case you're right, you win, like whatever) or:

"But Doug. You could write a script that automatically loads and selects your generic Smart Playlist template file by pressing a shortcut key!"

So here's what I did:

First, I made a new Smart Playlist and set my default criteria. I named the new Smart Playlist "untitled smart". Then I ran the script Export Smart Playlist Criteria and selected to export the "untitled smart" playlist (just the criteria data is exported, not the actual track contents). The script asks for a name for the XML file it will create, so I named it "Default Smart Playlist.xml" and saved it in my ~/Music/ folder. (You can delete the "untitled smart" playlist from iTunes now if you want. Maybe you'd better since you'll be creating another one identical to it in just a few steps.)

Now, you need to create and save a script which will load that XML file into iTunes. If you named everything the way I did and saved the XML file in your ~/Music/ folder then this is all you need to enter into a new AppleScript Editor doc:

set theXML to (((path to music folder) as text) & ("Default Smart Playlist.xml")) as text
tell application "iTunes"
	add (theXML as alias)
	reveal playlist "untitled smart"
end tell

You could add a routine that chooses from other exported Smart Playlist XML files. But I want this to simply load the one XML and that's why its path is hard-coded in the script.

Name the script something like "Make New Smart Playlist" and Save it (as a Script) to your ~/Library/iTunes/Scripts/ folder so it appears in and can be activated from iTunes' Scripts menu.

The best part, of course, is that I can assign this script its own shortcut key and my default Smart Playlist template will be a fingertip or two away.

Now you're probably thinking: "You mentioned something about a way to choose different Smart Playlist XML in a list?"

Just what I was thinking too right now! I've actually exported quite a number of Smart Playlist XMLs that I like to load for different purposes. Things like dates and times (and Holiday) are already entered, I'll just add Artist or something after loading one. To access them, I have stored all the Smart Playlist XML files I exported using Export Smart Playlist Criteria (and only these files) in a folder named "SPXML Files" in my ~/Music/ folder. This script will list those files in a choose box. Select one and it will be added to iTunes:

set theXMLFolder to (((path to music folder) as text) & ("SPXML Files:")) as text
set folderContents to (list folder theXMLFolder without invisibles)
if folderContents is {} then return

tell application "iTunes"
	set theChoice to (choose from list folderContents with prompt "Choose:")
	if theChoice is false then return
	set theXMLName to item 1 of theChoice
	add (((theXMLFolder & theXMLName) as text) as alias)
end tell

Name it something like "Choose Smart Template" and Save it in your ~/Library/iTunes/Scripts/ folder so it appears in and can be activated from iTunes' Scripts menu. It'll look like this when you do:

This lets me easily load up a new Smart Playlist from among a variety of pre-fab'd templates. When they said AppleScript will save you from entering the same data over and over, this is what that was about.

(Last parenthetical: Be sure not to leave a bunch of "untitled smart" playlists lying around. Too many Smart Playlists that you don't need or can't justify keeping will take their toll on iTunes' efficiency.)

Site contents © 2001 - 2022 (that's right: 2001) Doug Adams and weblished by Doug Adams. Contact support AT dougscripts DOT com. About.
All rights reserved. Privacy.
AppleScript, iTunes, iPod, iPad, and iPhone are registered trademarks of Apple Inc. This site has no direct affiliation with Apple, Inc.
The one who says "it cannot be done" should not be interrupting the one who is doing it.