Control iTunes Under Windows #83
Chapter 11, Native Integration and Packaging
|
421
HACK
} catch (IOException ex) {
System.out.println("exception : " + ex.getMessage( ));
ex.printStackTrace( );
}
}
The line:
tell app "iTunes" to artist of current track as string
will call the artist method on the current track. It will return the artist as a
string and print it to standard out. You can get the standard output by
reading from
proc.getInputStream( )
. The code then prints the string as a
single line, so you can easily read it with the
readLine( ) method on a
DataInputStream
. Once you have the string back, you can print it, put it in a
JLabel, or do whatever else is appropriate for your program.
Apple has more information available on the iTunes AppleScript API at http://
www.apple.com/applescript/itunes/. With this API, you can do almost any-
thing. For example, you can get the name of a playing song:
tell app "iTunes" to name of current track as string
You can also get the album name:
tell app "iTunes" to album of current track as string
Muting the volume is also a piece of cake:
tell application "iTunes" to set mute to true
In the same vein, you can obtain the current volume:
tell application "iTunes" to sound volume as integer
Take a look at the API and see what other interesting things you can do.
Many of the other programs that come with Mac OS X can be controlled
with Apple Events as well, so there is a lot to play with. You can query the
network status, open files and programs, or even tie Mail and iPhoto
together to capture, edit, and send photo albums. The possibilities are huge
and are all accessible right from your own Swing application.
H A C K
#83
Control iTunes Under Windows Hack #83
Use a simple open source library to monitor and control Windows iTunes
from your Swing application.
Windows doesn’t have a standard scripting API like Apple Events, but it
does have another object model that iTunes supports. Using an open source
library, this hack will show you how to script iTunes just as easily on Win-
dows as you can on the Mac.

Get Swing Hacks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.