fetching a zip code

Peter Brigham pmbrig at gmail.com
Sun May 11 23:19:48 EDT 2008


Here's something I arrived at after thinking more about Scott Rossi's  
observation a month or two ago (re the Movie Finder stack) about  
using the URL functions in Rev without ever actually opening a  
browser. The following handler operates on a text field into which  
the user enters an address, and if you don't know the zip code it can  
be fetched automagically. Watch line-wraps:

on getZip
   put fld "addr" into tAddr
   if word -1 of tAddr is a number then exit getZip
   -- already have zip code
   put tAddr into origAddr

   put line 1 of tAddr into addr1
   if char 1 of addr1 is not a number and word 1 of addr1 <> "One" then
     -- probably a company name, or some such
     delete line 1 of tAddr
     put line 1 of tAddr into addr1
   end if
   if word 1 of addr1 = "One" then put "1" into word 1 of addr1
   -- google expects '1' not the 'One' that some snootier places use

   -- the following is to handle things like '110 Main St. - Suite 200'
   -- delete those before sending address to google
   if " - " is in addr1 then -- n-dash
     delete char (offset(" - ",addr1)) to -1 of addr1
   else if " – " is in addr1 then -- option-dash
     delete char (offset(" – ",addr1)) to -1 of addr1
   else if " — " is in addr1 then -- m-dash
     delete char (offset(" — ",addr1)) to -1 of addr1
   else if "#" is in addr1 then
     -- to handle apartment numbers
     -- delete those before sending address to google
     delete char (offset("#",addr1)) to -1 of addr1
   end if
   put sr(addr1) into line 1 of tAddr

   -- now, format for google
   replace return with ", " in tAddr
   put "http://maps.google.com/maps?f=q&hl=en&geocode=&q=" into tURL
   replace space with "+" in tAddr
   replace "++" with "+" in tAddr
   put tAddr after tURL
   put URL tURL into thePage

   if the result <> empty then
     answer "Can't get zip code. Check your online connection." as sheet
     exit getZip
   end if

   put offset("sxpo:",thePage) into ofs
   -- ...sxpr:"MA",sxpo:"02135",sxcn:"US"...
   put char ofs+6 to ofs+10 of thePage into tZip
   if tZip is a number then
     put origAddr & "  " & tZip into fld "addr"
   else
     answer "Can't get zip code." as sheet
   end if
end getZip

function sr str -- strip returns and other white space fore & aft
   return word 1 to -1 of str
end sr

I originally called the handler on closefield, but if you aren't  
online then you get a potentially spurious answer dialog. Instead I  
call it from a menupick handler in a popup menu triggered by a right  
click on the field, giving the user more control.

I tried to do something more elegant than fetching a page containing  
a whole map from google, but I couldn't find any simple RSS feed for  
zip codes, and all the other zip code search pages I found used java  
so I couldn't construct a URL search string from the address. Even  
so, the script takes less than half a second to add the zip code to  
the field, unless the network is clogged. I love the idea of doing  
this kind of thing "behind the scenes," as it were.

BTW, Scott, I took the liberty of turning your Movie Finder stack  
into a standalone, tweaking the interface a bit (perhaps not for the  
better :-)) and adding the functionality of being able to click on a  
movie title and get connected with the MRQE (Movie Review Query  
Engine) list of online movie reviews for the film. It's downloadable  
from
http://home.comcast.net/~pmbrig/MovieLocator.html
I'm interested in feedback partly because I'm finding that folks  
running Leopard don't seem to be able to open my standalones (created  
on an Intel Mac iBook G4, OSX 10.4.1, Studio 2.9 build 610). Maybe  
there's something wrong with my standalone settings? I'm building it  
as a universal binary, not sure what's going wrong.... Anyone else  
have trouble building in Tiger and running the resulting app in  
Leopard? Is there a trick to it? The Windows version seems to work  
fine, ironically.

Peter M. Brigham
pmbrig at gmail.com


More information about the use-livecode mailing list