Hacking iPod and iTunes
Subject:   Feedback on hack #72
Date:   2004-10-29 06:08:52
From:   has01

A few suggestions regarding your iTunes+AppleScript text:

Objects and references. An AppleScript object is a thing that is scriptable.

It'd be better to say "an application object is..." to avoid confusing objects defined in an application's Apple event interface (documents, windows, playlists, etc) with objects defined within the AppleScript language itself (string, lists, etc).

AppleScript identifies an object in a scriptable application by using a reference to it. A reference is an unambiguous pointer to a very specific thing.

This line could be misleading: application object 'references' are actually simple relational queries, not object-oriented references. It's an important distinction that's often missed since AppleScript - very confusingly - uses almost identical syntax for both, despite the difference in usage and behaviour.

The advantage of application object 'references' is that a single such reference can refer to any number of application objects, where as OO references point to just one. For example:

  tell application "iTunes"
set theList to every track in the playlist "Talking Heads Songs"
repeat with thisTrack in theList
set thisTrack's artist to "Talking Heads"
end repeat
end tell

Iterating over a list of track elements to apply the same command to each is unnecessary; just construct a reference to artist of every track of playlist ... and use it as the direct argument to the set command:

  tell application "iTunes"
set artist of every track in the playlist "Talking Heads Songs" to "Talking Heads"
end tell

This is simpler and more elegant, and will also run faster since it uses less code and only one Apple event instead of several (Apple events are not cheap).

Nice to see Mac application scripting getting exposure.


1 to 1 of 1
  1. Feedback on hack #72
    2008-02-19 13:03:20  botageous [View]

1 to 1 of 1