AppleScript Changes in iTunes 2.0.3
iTunes version 2.0.3 is the first update since the v2.0 release with major fixes to the AppleScripting architecture. Here is a quick summation of new features and fixes. [dec 15 '01]
New player commands
Added to the iTunes Suite is fast forward and rewind. They allow you to skip forwards or backwards in a playing track in accelerated-over-time increments. The commands will be ignored if iTunes is stopped. They produce the same effect as a user clicking - and - holding on iTunes previous/next arrows.
The syntax is similar to the other player commands, simply:
tell application "iTunes" rewind end tell [upm] click to open in Script Editor
(For a discussion of other commands, see Commonly Used iTunes Commands.)
The snippet above will continue rewinding but only to the beginning of the current track, at which point it will resume playing normally. If you use fast forward it will continue through the current track and resume normal playback at the next song; both actions will otherwise continue until another AppleScripting command is sent (stop, play, fast forward, etcetera); or until the user actually invokes another command by clicking on a control button or selecting a Menu option. Which brings us to our first batch of...
New and updated application properties
The property player state now returns fast forwarding or rewinding if iTunes is doing either one of those things, as it similarly does for playing, stopped, and paused during those actions (however, apparently, stopped is returned if iTunes is stopped or paused—a bug not to be repaired until iTunes 3).
The next addition is one I am anxious to experiment with: player position. It returns the position of the player in the current track in seconds:
tell application "iTunes" if player position is greater than 20 then next track end tell [upm] click to open in Script Editor
Even better, you can set the value of player position to put the player at a place in a track in seconds. Use play to start playing from that point:
tell application "iTunes" set player position to 20 play end tell [upm] click to open in Script Editor
The last new application property is fixed indexing. This is purely a device for AppleScript. When fixed indexing is set to true, the play order of playlist tracks will be fixed according to the current play order and AppleScript operations will not affect the sort order -- for example, changing the name of each track will not alter their order. The default startup setting for fixed indexing is false; it can only be set true with AppleScript; it stays set until a script sets it to false again or iTunes is quit.
Finally, the application property formerly known as "visuals" is now visuals enabled, to avoid confusion with the plural form of the application element "visual". I must admit, I have not done a thing with visuals because of this bug, so I am looking forward to having some fun with it.
New track property - "enabled"
Just plain left behind the first time around is the track property, enabled. This is how you control a track's play checkbox. How cool is this property? You can use an AppleScript to include or exclude tracks from the play order:
tell application "iTunes" repeat with thisTrack in (get every track of playlist "Library") if thisTrack's comment is "Never again!" then set thisTrack's enabled to false end repeat end tell [upm] click to open in Script Editor
You can now set the view of the browser window. Essentially you can now select a playlist; while you could always play a playlist, you were not able to select it to make it the active/hilited playlist in the Source column.
Below are some examples of how to set view to a particular playlist:
tell application "iTunes" set the view of browser window 1 to user playlist "Mom's Favorites" -- or... set browser window 1's view to user playlist "Neurotic Outsiders" -- or, stupid script trick... set the view of browser window 1 to user playlist (item 1 of (choose from list (get name of every user playlist)) as text) end tell [upm] click to open in Script Editor
Even though view is also a property of playlist window, only the view of browser window can be set. There is only ever one browser window, and that is iTunes' main window. But you knew that.
Internet radio listeners will be happy to hear that the inherited track properties of URL track can now be set correctly. These include comment, artist, EQ, volume adjustment, etcetera.
It also now seems that the bug which caused a crash or unexpected quit when attempting to get every user playlist is history.