
Control iTunes on Mac OS X #82
Chapter 11, Native Integration and Packaging
|
419
HACK
so what better way to show off the power of Java than by taking control of
iTunes directly from your own Java app!
The task of dealing with native applications is, by nature, platform specific.
For example, though Apple ships identical looking copies of iTunes for both
Windows and Mac, the integration APIs couldn’t be more different.
Apple Events
Most well-written Mac OS X applications support an API called Apple
Events. Apple Events let a programmer send commands and requests to a
running application from another program, through a process often called
scripting. The application must be written to support Apple Events and
every scriptable feature must be defined explicitly when the program is writ-
ten. Since it was Apple that wrote iTunes, they did a very good job of expos-
ing virtually every feature through Apple Events. All you need to do is tap
into these events.
Apple Events is an API, and you need a programming language to support it.
There are a variety of languages to choose from, but the easiest one to start
with is AppleScript, as the syntax is simple and OS X ships with a command-
line interpreter. There are also direct Java bindings available, but for the
kinds of simple things you are likely to want to do with iTunes,
exec( )ing
the interpreter will be much easier. All you have to do is call
osascript ...