Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

A very quick introduction to Java... Apps
Mac OS X has a great Java environment. Wanna get started? Write a quick app to control iTunes (or any other AppleScriptable App). The following Java file will help you control iTunes:

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:
  1. Save the first file as iTunesControl.java
  2. Save the second file as music.java
  3. Start the Terminal an d change in the directory where you stored the above files
  4. To set the correct classpath, type in the Terminal: setenv CLASSPATH "/System/Library/Java/:"
  5. Now compile the Java files with: javac *.java
  6. Start iTunes with: java music play
  7. Next Song with: java music next
  8. Same for: pause and previous
Hopefully this helps you get started with Java and AppleScript; if you want to learn more about Java, follow this tutorial. Next step would be to include this java file in a .jsp file so we can control iTunes from (any) Web Browser in the world. To do so, you need Tomcat. But thanks to this step by step guide, that's easy.
    •    
  • Currently 3.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (2 votes cast)
 
[16,156 views]  

A very quick introduction to Java... | 7 comments | Create New Account
Click here to return to the 'A very quick introduction to Java...' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
A very quick introduction to Java...
Authored by: vlipper on Apr 16, '03 05:28:32PM
Cool! Someday I will make an applet or something out of this.

I changed itunes to iTunes in your source, don't know if that was the problem, the problem was that my iPod was selected in iTunes, and not (some) playlist...

vlipper

---
Communication is coming on strong - j. hendrix

[ Reply to This | # ]

A very quick introduction to Java...
Authored by: kesmit on May 03, '03 10:41:11PM
Here is the same thing, but in Python. You'll have to download the Objective C->Python bridge from http://sourceforge.net/ project/showfiles.php?group_id=14534. Just copy the following code to a file called "iTunesControl" and do "chmod +x iTunesControl". Then you can type "iTunesControl play" (or any other command).
#!/usr/bin/env python

import os, sys
from objc import *
from Foundation import *
from AppKit import *

class iTunesControl(object):

    def play(self):
        self.doScript('tell application "iTunes" to play')

    def pause(self):
        self.doScript('tell application "iTunes" to pause')

    def next(self):
        self.doScript('tell application "iTunes" to next track')

    def previous(self):
        self.doScript('tell application "iTunes" to previous track')

    def doScript(self, script):
        myscript = NSAppleScript.alloc().initWithSource_(script) 
        myscript.executeAndReturnError_({})

try:
    command = getattr(iTunesControl(), sys.argv[1])
    command()
except AttributeError:
    print 'Command %s does not exist' % sys.argv[1]
except IndexError:
    print 'usage: %s (play|pause|next|previous)' % 
os.path.basename(sys.argv[0])


[ Reply to This | # ]
A very quick introduction to Java...
Authored by: vlipper on Jun 18, '03 03:33:39PM
Cool stuff!
Today I did the Tomcat (embedded in JBoss) trick. Very nice.
You can have a look at it on my page (not all at the same time...)

bye, Vlipper

---
Communication is coming on strong - j. hendrix

[ Reply to This | # ]

doesnt work for me
Authored by: ragerino on Nov 04, '03 03:09:41AM

when i do "java music play" and iTunes isn't started it starts it and plays like expected. but when iTunes is allready started play won't work. also next and previous doesn't work.

i'm using osX10.3.



[ Reply to This | # ]
doesnt work for me
Authored by: Lorrin on Jan 12, '04 12:55:52AM

Yes, I have the same problem, also 10.3. The script fragments work fine if I run them from inside Script Editor, so I guess it's a Java thing. hrm..



[ Reply to This | # ]
doesnt work for me
Authored by: Lorrin on Jan 12, '04 01:15:05AM

..actually, if I let it sit for two minutes (!) it does work. During those two minutes the computer's mostly idle but it does seem that the java process and iTunes alternate back and forth between with one idle and the other using from 0 to 20% CPU... anyone have any ideas? I assume something's not quite right w/ the Java code.



[ Reply to This | # ]
doesnt work for me
Authored by: unowen on Jul 10, '04 01:28:06PM

Anyone figure this out yet? I need to interface Java with AppleScript (specifically iTunes) and I need to be able to pull stuff out of it...any suggestions?



[ Reply to This | # ]