Writing an Operator Script

In this section, I’ll lead you through the process of creating a simple user script for Operator. Start by looking through the best documentation for understanding Operator scripts:

http://www.kaply.com/weblog/operator-user-scripts/

There you will find a tutorial for writing a script that lets users find the closest Domino’s Pizza to a given instance of an address (adr):

         http://www.kaply.com/weblog/operator-user-scripts/creating-a-microformat-action-user-?script-basic/
      

In this section, I will walk you through the steps to create a script that performs a similar function. Instead of converting an adr instance into a URL to the Domino’s Pizza web site, our script will geocode the address by creating a URL to http://geocoder.us. Since our script is similar to that of the tutorial, we will follow a ­two-­step strategy:

  1. Install the tutorial script to understand how it works.

  2. Convert the script to one that geocodes the adr instance.

Studying the Tutorial Script

You will find the tutorial script here:

http://www.kaply.com/weblog/wp-content/uploads/2007/07/dominos.js

It’s possible that after this book is published, there might be a newer version of the referenced user scripts. You can check here: http://www.kaply.com/weblog/?operator-user-scripts/.

Install it and restart your web browser. If you run the action on this:

http://upcoming.yahoo.com/event/144855

your browser will conduct a search for the closest Domino’s Pizza stores to 1401 N Shoreline Blvd in Mountain View, CA:

            http://www.dominos.com/apps/storelocator-EN.jsp?street=1401%20N%20Shoreline%20Blvd.&Â
            cityStateZip=California,%20Mountain%20View%2094043
         

Let’s now study the script to understand how it works:

            var dominos = {
              description: "Find the nearest Domino's Pizza",
              shortDescription: "Domino's",
              scope: {
                semantic: {
                  "adr" : "adr"
                }
              },
              doAction: function(semanticObject, semanticObjectType) {
                var url;
                if (semanticObjectType == "adr") {
                  var adr = semanticObject;
                  url = "http://www.dominos.com/apps/storelocator-EN.jsp?";
                  if (adr["street-address"]) {
                    url += "street=";
                    url += adr["street-address"].join(", ");
                  }
                  if ((adr.region) || (adr.locality) || (adr["postal-code"])) {
                    url += "&cityStateZip=";
                  }
                  if (adr.region) {
                    url += adr.region;
                    url += ", ";
                  }
                  if (adr.locality) {
                    url += adr.locality;
                    url += " ";
                  }
                  if (adr["postal-code"]) {
                    url += adr["postal-code"];
                  }
                }
                return url;
              }
            };
            
            SemanticActions.add("dominos", dominos);
         

There are several elements to notice about this script as you think about how to adapt it:

  • The dominos JavaScript object defines an action. An action consists of four properties: description, shortDescription, scope, and doAction.

  • You should change the name of the JavaScript object, its description, and its ?shortDescription to fit the purpose of the new script.

  • The scope property is used to tie an action to a specific data format. The following:

    scope: {

    semantic: {

    "adr" : "adr"

    }

  • means any adr instance. You can limit the scope to only adr instances with the property locality with this:

    scope: {

    semantic: {

    "adr" : "locality"

    }

  • or to a certain URL:

    scope: {

    url: "http://www.flickr.com"

    }

  • Associated with the doAction property is a function that actually creates the URL for Domino’s Pizza by concatenating the various pieces of the adr instance. To adapt this function, you need to understand the URL structure of http://geocoder.us, the service we will use to geocode the address.

  • Note that the simplest type of action of an Operator script is to return a URL, which the browser then loads. (You can learn how to get Operator actions to perform other operations by reading the advanced tutorials at http://www.kaply.com/weblog/? operator-user-scripts/.)

            scope: {
                semantic: {
                  "adr" : "adr"
                }
         
            scope: {
                semantic: {
                  "adr" : "locality"
                }
         
             scope: {
              url: "http://www.flickr.com"
             }
         

Writing a Geocoding Script

As you learned in Chapter 13, there are a variety of sites to use to geocode an address in the United States. One service is Geocoder.us. You can geocode an address here:

http://geocoder.us/demo.cgi?address={address}

For example:

http://geocoder.us/demo.cgi?address=1600+Pennsylvania+Ave%2C+Washington+DC

Taking the URL template for Geocoder.us into account, you can adapt the script to come up with something like the following:

            // based on http://www.kaply.com/weblog/wp-content/uploads/2007/07/dominos.js
            
            var geocoder_us = {
              description: "Geocode with geocoder_us",
              shortDescription: "geocoder_us",
              scope: {
                semantic: {
                  "adr" : "adr"
                }
              },
              doAction: function(semanticObject, semanticObjectType) {
                var url;
                if (semanticObjectType == "adr") {
                  var adr = semanticObject;
                  url = "http://geocoder.us/demo.cgi?address=";
                  if (adr["street-address"]) {
                    url += adr["street-address"].join(", ");
                    url += ", ";
                  }
                  if (adr.locality) {
                    url += adr.locality;
                    url += ", ";
                  }
                  if (adr.region) {
                    url += adr.region;
                    url += ", ";
                  }
                  if (adr["postal-code"]) {
                    url += adr["postal-code"];
                  }
                }
                return url;
              }
            };
            
            SemanticActions.add("geocoder_us", geocoder_us);
         

The resulting URL for Mashup Camp IV on Upcoming.yahoo.com is as follows:

            http://geocoder.us/demo.cgi?address=1401%20N%20Shoreline%20Blvd.,%20Mountain%20View,Â
            %20California,%2094043