Access Mac Address Book from Java using AppleScript?


I’m a complete newbie to Macs and AppleScript, but am very experienced with Java.

I’m looking into getting the address details from the Address Book application in OS:X, and, from preliminary searching, using AppleScript seems like the most logical way to do it.

I assume I could kick off a script to dump the details into an XML file when I want to get the list, and then read that file in, but I’m hoping there’s a neater solution using some API for Java allowing it to execute AppleScript commands directly.

Does anyone have any idea on how I might be able to proceed?

Thanks in advance!


What are you try to read the XML file into? Why?

For basics on pulling data out of Address Book, check out this cool script posted by Apple called Phonelist which is part of the scripting Safari section.

I’ve written an application in Java which sends SMS messages through the Bigfoot SMS gateway (see I use it all the time on my Win2k box.

My brother is interested in it, but he’s a music type, so spends his life on OS:X. Being Java, it runs on the Mac, but, instead of typing someone’s mobile number into the application, he’d like to be able to select a person’s name from a list. Obviously, it’d make sense for this list of names and mobile numbers to be taken from the Address Book on the Mac.

I’m sure that it would be possible to get an AppleScript to pull out the names and mobile numbers of all the people in the Address Book. It’s just a question of then getting that information into my Java application.

One way of doing this is to use Java’s ability to execute other applications through the exec() method on the Runtime object. I could execute an AppleScript in this way, and get the AppleScript to write the data out to a file, and then read in the file in Java. I’m only thinking XML because it’s very easy to read XML in Java, and everything’s XML these days :stuck_out_tongue:

A nicer method, but I don’t know if it’s feasibile - this is what I’m posting about really - is executing AppleScript commands directly from Java. This would, in theory, allow information returned from AppleScript to be used immediately in the Java app without needing to write to a file first - a bit like Java’s JDBC system for talking to databases.



here is an oppurtunity to think differant! In the Mac OS, since OS 9 with MRJ, you can write AppleScripts to control Java Applications – the Java objects and methods of a running Java application can be made visible to AppleScript as objects and events. Subsequently one can pass that information back and forth.

This Technote will give the information you are looking for:

A Java application can be scripted from AppleScript after you
use the AppleScript editor to generate an ‘aete’ from the running Java
application… if a java class has a ‘set’ and ‘get’ methods, AppleScript
refers to that as a property.

class javaclass
public int getMyFoo(){…};
public int setMyFoo(){…};

translates to AppleScript:

tell application “java app”
set MyFoo of ajavaobject to 1
get MyFoo of ajavaobject
end tell

Also, creating a Applescript Studio Application, you can also include Java.

You could also use Runtime.exec() with the osascript utility. It takes standard in (or a file) and runs it using the AppleScript Engine.