dougscripts.com

AppleScript Changes in iTunes 3

iTunes 3 was released on July 17 2002. It is for OS X only, so 2.0.4 is the end of the line for OS 9 users.

iTunes 3 introduces many cool features, including Smart Playlists, Sound Check for optimizing track volume levels, additional tags...read all about it.

Here is what a quick glance at iTunes 3's AppleScript Dictionary reveals. [july 17 '02]

 

Additions

• new application property: current playlist, read-only reference to the playlist containing the current track.

• new properties of audio CD playlist: compilation, composer, disc count, disc number, and year

• new properties of the track class: compilation, composer, disc count, disc number, finish (used to be stop); played count, played date, rating. Note that some Smart Playlist-related features are scriptable.

• One new playlist property, smart, a boolean read-only Smart Playlist flag.

Removed

• the application property converting. Not sure why; I know I used this in OS 9 scripts.

• the track property stop removed and replaced with finish (see above).

One major change is the renaming of the "iTunes Music Library (2)" file to "iTunes 3 Music Library". If you are upgrading from iTunes 2.0.4 you will find both in your Documents > iTunes folder after you start up iTunes 3. However, the "iTunes Music Library (2)" file is ignored. Don't delete it! iTunes can re-generate a new "iTunes 3 Music Library" from it if need be.

If you are installing iTunes 3 for the first time, the "iTunes 3 Music Library file" will be in the home > Music > iTunes folder.

New Application property

The handy current playlist property returns a read-only reference (eg: user playlist id 2214 of source id 31 of application "iTunes") to the currently playing track's playlist. I used to use the container property of item to get this (get container of current track), but current playlist is much more intuitive. Use it like this, and note the check of player state:

tell application "iTunes"
   if player state is playing then
      name of (get current playlist)
   else
      error "iTunes isn't playing right now"
   end if
end tell

New Track Properties

We can divide the new track properties into two groups: read-only [r/o] and "get-able and set-able". Read-only values of a property can't be changed by the user, only "observed".

Note that all the new track properties are inherited by the current track object as well.

Of the new track properties, played count and played date are read-only cannot be set by AppleScript. These two properties will be useful if you are writing scripts that emulate Smart Playlist updating.

The other new Smart Playlist-related property is rating. You can get and set this value. It takes an integer; each rating "star" is equal to 20, and thus five stars is equal to 100. Setting a track's rating to any number other than a factor of 20 (eg: 30 or 85) will "take", but iTunes apparently rounds this down for star display purposes; ie, setting a track rating to 50 shows two stars, as if it was really 40. I was almost hoping for half a star to show up!

This script scowers the "Library" for low rated tracks and puts them in another Playlist:

tell application "iTunes"
   if player state is playing then
      name of (get current playlist)
   else
      error "iTunes isn't playing right now"
   end if
end tell

Similarly you could look at played count and do an action based on play frequency.

played date returns a value in date format. Explaining date formatting is beyond the scope of this article—but not this one's.

By the way, tracks you haven't played with iTunes 3 yet, or a current track playing for the first time, will return missing value for played date and show "Not available" in the track's Info window; a script trying to use played date that doesn't catch these anomalies may generate an error.

Without a doubt, I heard from most users about the new composer tag, extremely important to Classical listeners. The composer AppleScript property takes a Unicode text string, as does album, artist, comments, etcetera:

tell application "iTunes"
   set composer of every track of user playlist "Mom's Bartok" to "Bartok, B"
end tell

or...

tell application "iTunes"
   set thePlaylist to view of front window
   repeat with i from 1 to (count of thePlaylist's tracks)
      set (track i of thePlaylist)'s composer to (track i of thePlaylist)'s artist as string
   end repeat
end tell

or...

tell application "iTunes"
   if selection is not {} then
      repeat with aTrack in selection
         set composer of aTrack to (get artist of aTrack)
      end repeat
   end if
end tell

Those are some various methods you might use to set composer with a string.

To better keep track of CD tracks, you can use compilation, disc count, and disc number. (These three track properties are also newly added properties of audio CD playlist.)

The first, compilation (is this track from a compilation album?), takes a boolean true or false as a value; disc count and disc number take integer values. I don't know if this information is available from CDDB when you "Get CD Track Names" or directly from the CD, and so I am not certain if this data will suddenly just "show up" unless you place it there—either manually or via AppleScript. Note that if you have iTunes organize your files, files designated as "Part of a compilation" will turn up in the "Compilations" folder in your Music folder.

Here's a snippet using all three props:

tell application "iTunes"
   if compilation of current track then
      display dialog "This track is from disc number " & (get disc number as string) ¬
         & " of " & (get disc count as string)
   else
      display dialog "This track is not from a compilation."
   end if
end tell

Finally, iTunes 2's "stop" property—which was trying to be a partner for track's start property until it was realized that "stop" is also an iTunes command (!)—has been replaced with finish. Both start and finish take integer values in seconds.

New "user playlist" Property

There's only one new property for user playlist: smart. It takes a read-only boolean value, so I guess you can't create a Smart Playlist with AppleScript—although you can use AppleScript to assemble Playlists by whatever criteria you like (pretty much), you just can't tell a Playlist to be or not to be a Smart Playlist. Another obvious observation: Smart Playlists can only be user playlists since smart is not a property of any other type of playlist. Works like this:

tell application "iTunes"
   set thePlaylist to view of front window
   if (class of thePlaylist is user playlist) and (smart of thePlaylist) then beep
end tell

Above: if the selected Playlist is a user playlist and its smart property is set to true, then you'll get the beep.

You cannot get any info or criteria about a Smart Playlist, such as you would see when you click command-i (Get Info...) on a selected Smart Playlist in the Source column.

Site contents © 2001 - 2024 (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.