dougscripts.com

About Master Libraries

iTunes 7 introduced "Master Libraries"--"Music", "Movies", TV Shows", and so on. As far as AppleScript is concerned, they don't behave like the original "Library" in previous versions.

 

They're Really Smart Playlists

Although you can't directly access it, iTunes 7 still has a "Master Library". In iTunes AppleScript parlance this is still library playlist 1. This is the main playlist that lists every track in your library: audio files, movie files, PDF files, audiobooks, URL tracks (streams), and podcasts. In previous versions of iTunes, all of these tracks/files would have been listed in your "Library". However, the developers of iTunes thought it would be easier to access the different types of tracks/files if this main library were broken up into sub-libraries. It's not a bad idea.

[You can set a hidden preference to display the "Library" playlist in iTunes 8 or better using Change Hidden iTunes Preferences.]

The way iTunes assigns tracks to these new sub-libraries is the same way you would: those sub-library playlists are really smart playlists. And they behave like smart playlists. When you add an audio file to iTunes, it still goes into the--shall we say--invisible main library. But since the "Music" library is a smart playlist whose criteria is basically "Kind is MPEG audio file" OR "Kind is AIFF audio file" OR "Kind is Protected AAC File", and so on, all audio files are sent to the "Music" library. Same for "Movies", same for "TV Shows", "Audiobooks", and "Podcasts".

Because these libraries are smart playlists, they behave differently than a regular user-created playlist or the master library.

Master Libraries ≠ Main Library: QED

As you probably remember from pre-iTunes 7, when you delete a track from the main library, it gets deleted from iTunes entirely; it will be deleted from every playlist, smart and otherwise. Similarly, as every iTunes AppleScript Scout knows, you can delete a track entirely from iTunes when you delete it from the main library. This is how I normally do it, more or less:

tell application "iTunes"
	set delete_this_track to item 1 of selection
	delete (some track of library playlist 1 whose database ID is (get database ID of delete_this_track))
end tell

Select any track anywhere in any playlist and it will be deleted from iTunes.

Now, with iTunes 7, when you click "Delete" when a track is selected in one of the "Master Libraries", the application is smart enough to know that you are asking to delete the track entirely from iTunes. BUT, if you try to delete a track from a "Master Library" using AppleScript without referencing it in library playlist 1 then it will not be deleted:

tell application "iTunes"
	set delete_this_track to item 1 of selection
	delete delete_this_track
end tell

This snippet works on any track selected in the main library of pre-iTunes 7, but with a track selected in an iTunes 7 "Master Library" it generates an error. It's the same error you would get if you ran the script on a track selected in a playlist. That's because the "Master Libraries" are playlists (the smart kind), not the main library. Like any playlist, they are subsets of the main library.

Where is the main library?

The main library (library playlist 1) is not so invisible. While there is no command in iTunes to do so, you can display it with AppleScript:

tell application "iTunes"
	set view of front browser window to library playlist 1
end tell

This will display the contents of the main library, just like in pre-iTunes 7, listing all file types in your iTunes library. When you do so, you will notice that no playlist in the Source column is selected. That's because in iTunes 7, obviously, the main library is no longer listed or selectable as a Source.

UPDATE: This trick no longer works as of iTunes v7.1. The library playlist 1 can no longer be the object of a window's view. See a workaround on this page. Also, you can set a hidden preference to display the "Library" playlist in iTunes 8 or better using Change Hidden iTunes Preferences.

They're Smart Playlists, Redux

Known fact: You can't add or duplicate a track/file to a smart playlist using AppleScript.

Here is an anomaly in iTunes 7 when the "Music" library is selected:

set newfile to (choose file)

tell application "iTunes"
	set pl to get view of front window
	set addedtrack to add (newfile as alias) to pl
end tell

--result: file permission error


set newfile to (choose file)

tell application "iTunes"
	set pl to get view of front window
	set addedtrack to add (newfile as alias)
end tell

-- result: file added correctly

In the first snippet, iTunes will forbid adding a file to the selected playlist reference pl--"Music" in our case.

Notice how in the second snippet, the pl variable isn't used. But, the file will be successfully added to iTunes and assigned to the 'Master Library" it belongs in.

This is sort of a bug. While you should be able to add or duplicate a track/file to a "Master Library", iTunes should know better than to generate an error if you attempt to put the file in a "Master Library" the file doesn't belong in and just put the track/file where it's supposed to go. I mean, you don't want to put an audio file into the "TV Shows" library; but on the other hand, iTunes should not generate an error when attempting to add the file in general. This behavior would only apply to "Master Libraries", not any old smart playlist, where that smart playlist's criteria must be met. I believe Apple is aware of this anomaly (as of v7.0.1).

(UPDATE 061101: As of iTunes 7.0.2, the first script above will work; iTunes will place the added file's track in the proper "Master Library"--in spite of where you may have intended it to be placed--without producing an error.)

iTunes Will Maintain Smart Playlists

You can't simply delete a track from a Smart Playlist because iTunes will try to fulfill the criteria of the Smart Playlist. (Of course you can Option-Delete a track and its file completely from iTunes from a Smart Playlist.)

AppleScript can't over-ride iTunes smart playlist criteria. You can delete a track from a smart playlist, but if that track still meets the criteria of that smart playlist, iTunes will put it back in the blink of an eye. It's the same with the "Master Libraries". As mentioned above, unless you delete a track from library playlist 1, it will not be deleted from a "Master Library".

A perfect example is the script Remove Dead Tracks. This script will remove dead tracks (!) from a selected playlist. But it doesn't work when the selected playlist is "Music". It will generate an error. That's because it can't remove tracks from a Smart Playlist. (You can use Super Remove Dead Tracks, which will delete dead tracks from library playlist 1.)

Similarly, you can't add or duplicate a track to a smart playlist with AppleScript unless it meets the criteria of that smart playlist.

Epilogue

I like that iTunes 7 segregates my files into the "Master Libraries". It makes maintaining my media collection much easier, especially since I own a Video iPod and am addicted to several TV Shows from the iTS. But as far as AppleScript goes, and as an AppleScript developer, you gotta know how those "Master Libraries" think. And the thing to remember is: they're smart.

Site contents © 2001 - 2019 (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 interrupt the one doing it.