Tod’s Page
May 1, 2008
 
 
This note describes techniques for using AppleScript with Entrance
that will work with both JRE 1.5 and 1.6 running on OS X.  
(More about JRE 1.6 and the Mac coming soon)
 
 
Extending Entrance With AppleScript
 
In an earlier note, I described how to make simple EntranceExtensions, which are Java classes implementing the EntranceExtension interface:
 
         package com.dbentrance.entrance.tools;
         import com.dbentrance.entrance.JavaClient;
 
         public interface EntranceExtension {
            public  void go(JavaClient client);
         }
 
As you can see, just one method is required, and there are no required behaviors, which makes building EntranceExtensions a snap.  Just add entrance.jar to your Java IDE as a jar library, and away you go.
 
Here’s how to rebuild Entrance Community with new extensions:  Download and unzip the Entrance Community Version sources,  There is a collection of examples in the “extensions/src” directory.  Simply write a new extension and drop it in that directory, the rebuild.  An ant build.xml file is provided, so all you need to do is type “ant”.  
 
Finally, you will need to add your extension to the X Menu, using the “Edit this menu...” dialog.  The dialog includes a “Test” button so you can try things out before committing.
 
Be aware that EntranceExtensions have the “keys to the kingdom”:  they can reach essentially any part of the Entrance environment, and this includes any MySQL host that appears in the Entrance explorer tree.  Be careful about allowing someone to install an EntranceExtension on your system.
 
AppleScript
It is possible for extensions to invoke arbirary shell commands, as BashX demonstrates.  In particular, you can chain to another application or to a script writtien in Perl, PHP, Python and other scripting languages.  You can also invoke AppleScript.
 
AppleScript is a quirky, neat scripting language for the Apple platform.  Adam Goldstein’s AppleScript:  The Missing Manual is fun to read and will give you enough information to start writing useful things.  Also, MacTech Magazine has a  VBA to AppleScript Transition Guide.  We like AppleScript, and see it as one way for Entrance to exchange small datasets with other OS X applications, such as Microsoft Office and (someday) Apple Numbers.
 
Entrance provides a helper class, com.dbentrance.entrance.AppleScript, for embedding AppleScript in Java code.   Complete source is in the community source download (and, if you are an AppleScript developer, but not a Java programmer, we can help you install your scripts.  Email me at the address below for more info)
 
Two ‘execute’ methods are provided, one that takes an AppleScript command as a String, and another that takes the name of an AppleScript file:
 
      public static String execute(String s)
      public static String executeScriptFile(File script)
 
Entrance uses the ApplesScript class to implement some of its special, “OS X only” features.   Here is a list of the features, and the ApplesScript class methods that correspond:
 
     “Connect using ‘mysql’...” on the HostNode popup
     runInTerminal(String cmd)
 
     “Send to iPhoto...” on the ChartPanel popup
     public static void sendToIPhoto(File jpgFile)
 
     “Open in Finder” on the DirectoryNode popup
     openInFinder(File f)
 
     for the GoogleEarthX extension (This AppleScript
     snippet is extracted from the blog of one Jeremy Quinn)  
     googleEarthMoveTo(double lat, double lon,double dist)
 
These methods are static and can be “remixed”.  With entrance.jar on the classpath, you can invoke them in your own Java applications, whether Entrance is running or not.
 
(You’ll need to be at Entrance version 1.2.72 or greater to see all of these)
 
Examples
There are a number of EntranceExtension examples in the “extensions” directory you will find in the community source download.  The coolest one, so far, is the one that controls Google Earth  from an Entrance table view.  When invoked, it sets the current point in Google Earth to (lat,lon) values pulled from the current row.
 
GoogleEarthX will be built into versions beginning with 1.2.72.   To try it, go to the ” X”  menu, and use “Edit this menu...” to install it.  (Select “EntranceExtension” for the type and “GoogleEarthX” for the class name)
 
When you have it installed, you can open a table with columns named “lat” and “lon” (case doesn’t matter), then click anywhere on a row in the table.   When you invoke GoogleEarthX from the X menu, Google Earth will position itself at the corresponding (lat, lon) coordinates.  Magic!  (For this to work smoothly, have Google Earth running before you invoke the extension).
 
Here’s the source for the GoogleEarthX extension.  You can see It is fairly simple to “reach into” Entrance for the current table view, to pull data from it, and then do things with the data using AppleScript:  
 
public class GoogleEarthX implements EntranceExtension {
    private static double NOVALUE = -300;
    
    private double getDouble(JTable jt, int i, int j) {
        Object obj = jt.getModel().getValueAt(i, j);
        if(obj != null) {
            try {
                return Double.parseDouble(obj.toString());
            } catch(Exception e) {
                return NOVALUE;
            }
        }
        else
            return NOVALUE;
    }
    
    public void go(JavaClient client) {
        TablePane tp = client.getSelectedTablePane();
        if(tp == null) {
            Utilities.showMessage("Error", "Open a table first.", client);
            return;
        }
 
        JTable jt = tp.getJTable();
        int i = jt.getSelectedRow();
        if(i == -1) {
            Utilities.showMessage("Error", "Select a table cell first.", client);
            return;
        }
        
        StringBuffer buf = new StringBuffer();
        
        double lat = NOVALUE;  // -300=NOVALUE will be an indicator for "not found"
        double lon = NOVALUE;
        for(int j = 0; j < jt.getColumnCount(); j++) {
            if(jt.getColumnName(j).equalsIgnoreCase("lat"))
                lat = getDouble(jt, i,j);
            else if(jt.getColumnName(j).equalsIgnoreCase("lon"))
                lon = getDouble(jt, i,j);
        }
        
        if(lat != NOVALUE && lon != NOVALUE) {
            // lat, lon, altitude in meters
            AppleScript.googleEarthMoveTo(lat, lon, 200000);  
        }
    }
}
 
Questions?  Comments?
If you are an AppleScript developer working with Entrance please get in touch with me at:  tod at dbentrance.com and let me know what your experiences are.    How can we make Entrance better?
 
 
Earlier posts:
     Crosstabs
 
 
Main page for Entrance:  http://dbentrance.com/
  
 
 
 
Copyright (c) 2008 Tod Landis, All Rights Reserved
 
MySQL is a registered trademark of MySQL AB in the United States, the European Union and other countries.  
Java is a trademark or registered trademarks of Sun Microsystems, Inc. in the U.S. and other countries.