import com.apple.cocoa.foundation.*;
public class iTunesControl {
public void play() { doScript("tell application \"itunes\" to play"); }
public void pause() { doScript("tell application \"itunes\" to pause"); }
public void next() { doScript("tell application \"itunes\" to next track"); }
public void previous() {doScript("tell application \"itunes\" to previous track"); }
private void doScript(String script) {
// This creates a new NSAppleScript
// object to execute the script
NSAppleScript myScript = new NSAppleScript(script);
// This dictionary holds any errors that are
// encountered during script execution
NSMutableDictionary errors = new NSMutableDictionary();
// Execute the script!
myScript.execute(errors);
}
}
To use the com.apple.cocoa.foundation.* lib provided with every version of OS X, type:setenv CLASSPATH /System/Library/Java/:
Read the rest of the article for instructions on how to use the above class...
An easy way to play around with the iTunesControl class would be:
public class music {
public static void main(String[] args) {
iTunesControl cont = new iTunesControl();
if(args[0].equals("play")) cont.play();
if(args[0].equals("pause")) cont.pause();
if(args[0].equals("next")) cont.next();
if(args[0].equals("previous")) cont.previous();
}
}
To get started, try the following steps:
- Save the first file as iTunesControl.java
- Save the second file as music.java
- Start the Terminal an d change in the directory where you stored the above files
- To set the correct classpath, type in the Terminal: setenv CLASSPATH "/System/Library/Java/:"
- Now compile the Java files with: javac *.java
- Start iTunes with: java music play
- Next Song with: java music next
- Same for: pause and previous

