dougscripts.com

Working With Playlists 1 2 3 

 

The main window of iTunes is called the "browser window". Presumably you can only have one open, but we'll call it "front browser window" anyway. To get a reference to the selected playlist in the "browser window"—the one hilited in the "Source" window—use the view property (note that just because this playlist is selected in the "browser window" doesn't necessarily mean it is also the playlist that contains the currently playing track or stream—that would be current playlist). This returns a reference to the selected playlist:

tell application "iTunes"
   set thePlaylist to view of front browser window
end tell

view is a property of both playlist window and the browser window so to get a reference to the—for lack of a better term—"front playlist", whether it is in a playlist window or browser window, use "front window" (or window 1):

tell application "iTunes"
   get view of front window
   
-- or...

   copy name of view of window 1 to the_name
   
-- or...

   copy front window's view to myPlaylist
end tell

This may be troublesome if you have another window open as the "front window", such as the EQ window or View Options window. These are modal windows and Apple Events cannot be processed while they are open. For this reason and to be sure you are targetting the correct sort of window you may want to include a check for the class of the front window, perhaps like this:

tell application "iTunes"
   if (class of front window is browser window) or (class of front window is playlist window) then
   -- or, fancy pants version...
   -- if {browser window, playlist window} contains (class of front window) then

   get view of front window

      -- continue with tasks

   end if
end tell

The view object will be a distinctive reference id for the playlist that looks something like this:

user playlist id 163 of source id 30 of application "iTunes"

Don't try to literally script this sort of run-time only variable. It's best to copy the entire reference to a user variable.

You can also select (hilite) a playlist in the Source column of the main browser window when you set view:

tell application "iTunes"
   set the view of the front browser window to playlist "Library"
end tell

When you set view to a particular playlist, it doesn't start the thing a-playin'. It just chooses and displays that playlist's tracks in the window.



You can also get the "current" playlist—the one that contains the currently playing track:

tell application "iTunes"
   set thePlaylist to current playlist
end tell

You can, of course, also access the frontmost playlist window, if there is one (don't mistake the "browser window" as a "playlist window" or a "playlist window" for a "playlist"):

tell application "iTunes"
   if front playlist window exists then
      set thePWindow to (get the front playlist window)
   end if
end tell

By the way, you cannot make a playlist window.

As mentioned earlier, setting a variable to a playlist as soon as possible in a script will be an enormous boon to clarity. I have found that scripts run faster if you use a reference to the targetted playlist, rather than the methods mentioned thus far:

tell application "iTunes"
   copy (a reference to (get view of front window)) to thePlaylist
end tell

Then you just reference "thePlaylist", like so:

tell application "iTunes"
   copy (a reference to (get view of front window)) to thePlaylist

   play thePlaylist
   
-- or

   get name of track 1 of thePlaylist
   
-- or

   set comment of every track of thePlaylist to "Mom's Favorites"
   
-- or

   display dialog "Rename Playlist:" default answer (get thePlaylist's name)
end tell

Remember that a "playlist" can be a "user playlist", "audio CD playlist", "device playlist", or "radio tuner playlist"; think of it this way: every "user playlist" is a "playlist", but not every "playlist" is a "user playlist"; every "device playlist" is a "playlist", but not every "playlist" is a "device playlist".

So far, I have been accessing file tracks and playlists—the ones that contain tracks which represent audio files on your computer. We've been able to conveniently refer to these playlist objects without having to know the source container. However, for other types of playlists (device, CD, iPod, radio) you have to determine the source before you can access the playlists and tracks they contain. You will see examples of this on the next pages.

2 3 | next >
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.