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

10.3: Integrate maporama.com maps in Address Book Apps
I prefer maporama.com to view maps, but the problem is that Address Book uses mapquest to show a map of an address. Thankfully, with Panther, the Adress Book is extendable by placing Applescripts in your ~/Library -> Address Book Plug-Ins folder.

Read the rest of the hint for the script and the how-to...


using terms from application "Address Book"
  on action property
    return "address"
  end action property
  
  on action title for aPerson with anAddress
    return "Maporama"
  end action title
  
  on should enable action for aPerson with anAddress
    return true
  end should enable action
  
  on perform action for aPerson with anAddress
    my showMap(street of anAddress, zip of anAddress, city of anAddress, ¬
      state of anAddress, country code of anAddress)
  end perform action
  
end using terms from

on showMap(street, zip, city, state, country)
  tell application "Safari"
    activate
    set map_doc to make new document at beginning of documents
    set the URL of map_doc to "http://www.maporama.com/share/map.asp"
    
    (* Wait till maporama is loaded in the webbrowser *)
    my waitForLoading(60)
    set map_doc to document 1
    
    (* Fill in the text fields *)
    do JavaScript "document.FindAdr._XgoGCAddress.value = '" & ¬
     street & "'" in map_doc
    do JavaScript "document.FindAdr.Zip.value = '" & zip & ¬
     "'" in map_doc
    do JavaScript "document.FindAdr.State.value = '" & state & ¬
     "'" in map_doc
    do JavaScript "document.FindAdr._XgoGCTownName.value = '" & ¬
     city & "'" in map_doc
    
    (* Find the country and select it *)
    set nbOfCountries to do JavaScript ¬
     "document.FindAdr.COUNTRYCODE.options.length" in map_doc
    repeat with i from 1 to nbOfCountries
      set iValue to do JavaScript ¬
     "document.FindAdr.COUNTRYCODE.options[" & i - 1 & "].value" in map_doc
      if iValue is country then
        set theIndex to i - 1
        exit repeat
      end if
    end repeat
    do JavaScript "document.FindAdr.COUNTRYCODE.options[" & ¬
     theIndex & "].selected = 1" in map_doc
    
    (* Submit the request *)
    do JavaScript "document.FindAdr.submit()" in map_doc
  end tell
end showMap

on waitForLoading(timeoutValue)
  tell application "Safari"
    delay 4
    repeat with i from 1 to timeoutValue
      delay 1
      if (do JavaScript "document.readyState" in document 1) is "complete" then
        exit repeat
      end if
    end repeat
  end tell
end waitForLoading
[robg adds: I had to control-click on the address field; hovering did nothing on my machine...]
    •    
  • Currently 2.38 / 5
  You rated: 3 / 5 (8 votes cast)
 
[23,080 views]  

10.3: Integrate maporama.com maps in Address Book | 21 comments | Create New Account
Click here to return to the '10.3: Integrate maporama.com maps in Address Book' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
10.3 Integrate maporama.com maps in Address Book
Authored by: quentinsf on Nov 16, '03 09:20:55AM

A related tip -

The standard map facility seems to base its choice of country on the address format you've selected for that entry, rather than on the country you've actually typed in the address.



[ Reply to This | # ]
10.3 Integrate maporama.com maps in Address Book
Authored by: Decado on Nov 16, '03 11:51:13AM

Great hint!
i also prefer maporama.
but the script only works about every third time or something. Anybody else with this problem?
And i would like it to open in a new tab in the existing safari window.



[ Reply to This | # ]
10.3 Integrate maporama.com maps in Address Book
Authored by: Decado on Nov 16, '03 12:14:18PM

i fixed it by deleting

tell application "Safari"
activate
set map_doc to make new document at beginning of documents
set the URL of map_doc to "http://www.maporama.com/share/map.asp"

and replacing it with

tell application "Safari"
activate
open location "http://www.maporama.com/share/map.asp"

it now works everytime and opens the adresses in new tabs



[ Reply to This | # ]
10.3 Integrate maporama.com maps in Address Book
Authored by: timstringer on Nov 16, '03 03:08:56PM

The script combined with the fix works great...except that I still need to press the "Go" button to actually see the map. Is anyone else having this problem?



[ Reply to This | # ]
10.3 Integrate maporama.com maps in Address Book
Authored by: Decado on Nov 17, '03 03:09:42PM

It works for me, but i have to wait about five seconds. that is annoying :)



[ Reply to This | # ]
A version of the script for Yahoo Maps
Authored by: bryang on Nov 16, '03 01:19:42PM
I use Yahoo for mapping. Here's a script that almost works. For some reason I can't get the submit to work, but it does fill in the address, city, state, and zip. Also, I haven't tested the country stuff, so it may not work perfectly either.


-- Script adds the abilty to load the selected address in maps.yahoo.com
using terms from application "Address Book"
	on action property
		return "address"
	end action property
	
	on action title for aPerson with anAddress
		return "Map with Yahoo"
	end action title
	
	on should enable action for aPerson with anAddress
		return true
	end should enable action
	
	on perform action for aPerson with anAddress
		my showMap(street of anAddress, zip of anAddress, city of anAddress, ¬
			state of anAddress, country code of anAddress)
	end perform action
	
end using terms from

on showMap(street, zip, city, state, country)
	tell application "Safari"
		activate
		open location "http://maps.yahoo.com"
		
		(* Wait till maps.yahoo.com is loaded in the webbrowser *)
		my waitForLoading(60)
		set map_doc to document 1
		
		(* Fill in the text fields *)
		do JavaScript "document.mapForm2.addr.value = \"" & ¬
			street & "\"" in map_doc
		do JavaScript "document.mapForm2.csz.value = \"" & city & ", " & state & " " & zip & ¬
			"\"" in map_doc
		
		(* Find the country and select it *)
		set nbOfCountries to do JavaScript ¬
			"document.mapForm2.country.options.length" in map_doc
		repeat with i from 1 to nbOfCountries
			set iValue to do JavaScript ¬
				"document.mapForm2.country.options[" & i - 1 & "].value" in map_doc
			if iValue is country then
				set theIndex to i - 1
				exit repeat
			end if
		end repeat
		do JavaScript "document.mapForm2.country.options[" & ¬
			theIndex & "].selected = 1" in map_doc
		
		(* Submit the request *)
		do JavaScript "document.mapForm2.submit()" in map_doc
	end tell
end showMap

on waitForLoading(timeoutValue)
	tell application "Safari"
		delay 4
		repeat with i from 1 to timeoutValue
			delay 1
			if (do JavaScript "document.readyState" in document 1) is "complete" then
				exit repeat
			end if
		end repeat
	end tell
end waitForLoading




[ Reply to This | # ]
10.3: Integrate maporama.com maps in Address Book
Authored by: aranor on Nov 16, '03 05:56:01PM
I've spent a bit of time customizing this script. It doesn't use JavaScript anymore and it doesn't do anything like automatically filling out a form and submitting it. Instead, it passes all the information directly in as GET parameters in the URL. Basically, it works much better than the script in the hint :) Here it is:

using terms from application "Address Book"
	on action property
		return "address"
	end action property
	
	on action title for aPerson with anAddress
		return "Maporama"
	end action title
	
	on should enable action for aPerson with anAddress
		return true
	end should enable action
	
	on perform action for aPerson with anAddress
		my showMap(street of anAddress, zip of anAddress, city of anAddress, ¬
			state of anAddress, country code of anAddress)
	end perform action
	
end using terms from

on showMap(street, zip, city, state, country)
	-- Create GET string for maporama
	-- COUNTRYCODE _XgoGCAddress Zip State _XgoGCTownName
	if country is missing value then
		set countryCode to "US"
	else
		set countryCode to toUpper(country)
	end if
	set getStr to "COUNTRYCODE=" & encode_text(countryCode) & ¬
		"&_XgoGCAddress=" & encode_text(street) & ¬
		"&Zip=" & encode_text(zip) & ¬
		"&State=" & encode_text(state) & ¬
		"&_XgoGCTownName=" & encode_text(city)
	
	tell application "Safari"
		activate
		open location "http://www.maporama.com/share/map.asp?" & getStr
	end tell
end showMap

-- this sub-routine is used to encode text 
on encode_text(this_text)
	set the acceptable_characters to "abcdefghijklmnopqrstuvwxyz0123456789_"
	set the character_list to {}
	repeat with this_char in this_text
		set this_char to the contents of this_char
		if this_char is in the acceptable_characters then
			set the end of the character_list to this_char
		else if this_char is " " then
			set the end of the character_list to "+"
		else
			set the end of the character_list to encode_char(this_char)
		end if
	end repeat
	return (the character_list) as string
end encode_text

-- this sub-routine is used to encode a character 
on encode_char(this_char)
	set the ASCII_num to (the ASCII number this_char)
	set the hex_list to {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"}
	set x to item ((ASCII_num div 16) + 1) of the hex_list
	set y to item ((ASCII_num mod 16) + 1) of the hex_list
	return ("%" & x & y) as string
end encode_char

-- this sub-routine uppercases a string
on toUpper(this_text)
	set character_list to {}
	repeat with this_char in this_text
		set this_char to the contents of this_char
		set the ASCII_num to (the ASCII number this_char)
		if ASCII_num ? 97 and ASCII_num ? 122 then
			set ASCII_num to ASCII_num - 32
		end if
		set this_char to (the ASCII character ASCII_num)
		set the end of the character_list to this_char
	end repeat
	return (the character_list) as string
end toUpper


[ Reply to This | # ]
10.3: Integrate maporama.com maps in Address Book
Authored by: BradW on Nov 16, '03 07:26:44PM

Sorry to ask such a basic question, but your script won't compile on my system because of this line:

if ASCII_num ? 97 and ASCII_num ? 122 then

(8 or so lines from the end of the script). The problem seems to be the ?, and I'm wondering if I'm displaying the web site in a font that swapped out a special character that's used in scripts. The error I'm getting has to do with "expecting a then," but now that I want to report it exactly, I can't get it to display again.

BradW



[ Reply to This | # ]
10.3: Integrate maporama.com maps in Address Book
Authored by: aranor on Nov 16, '03 08:11:55PM

Hrm. I typed it as >= and <= but it must have turned them into the actual greater-than-or-equal-to and less-than-or-equal-to chars (option-, and option-.)



[ Reply to This | # ]
10.3: Integrate maporama.com maps in Address Book
Authored by: aranor on Nov 16, '03 08:40:53PM
If anybody wants an applescript link for this directly, the latest entry on my blog has a link.

[ Reply to This | # ]
10.3: Integrate maporama.com maps in Address Book
Authored by: bluehz on Nov 16, '03 07:58:17PM
This worked better for me:
using terms from application "Address Book"
	on action property
		return "address"
	end action property
	
	on action title for aPerson with anAddress
		return "Map with Maporama"
	end action title
	
	on should enable action for aPerson with anAddress
		return true
	end should enable action
	
	on perform action for aPerson with anAddress
		set estreet to my parseString(street of anAddress)
		set ecity to my parseString(city of anAddress)
		set map_doc to "http://www.maporama.com/share/Map.asp?SESSIONID=&COUNTRYCODE=" ¬
		& country code of anAddress & "&_XgoGCAddress=" & estreet & "&Zip=" & zip of anAddress & ¬
		"&State=" & state of anAddress & "&_XgoGCTownName=" & ecity & "&quality=0&SEARCH_ADDRESS.x=12&SEARCH_ADDRESS.y=14"
		tell application "Safari"
			set browser to make new document
			tell browser
				set URL to map_doc
			end tell
			activate
		end tell
		return true
	end perform action
end using terms from

on parseString(astring)
	set parsed to ""
	set loopCount to 1
	repeat until loopCount > (count characters of astring)
		set char to character loopCount of astring
		if char is in {space} then
			set parsed to parsed & "+"
		else
			set parsed to parsed & char
		end if
		set loopCount to loopCount + 1
	end repeat
	return parsed
end parseString
FYI - ROB - I tried posting this with URL Messaging Protocol (applescript link) to have it load into users script editor with a link. I spent about 2 hours troubleshooting it. Never would display properly in the posting. I researched and found that certain chars were not valid in AS URL's, so I looked for those. Didn't find any. Finally think I narrowed it down to a length issue in the comment.php. Not sure - but I was able to encode small parts of the script and it would work fine. Encode and post teh whole thing and it would not work. I finally figured I could post down to "&_XgoGCTownName" part of the script which ends up being about 1095 chars.

[ Reply to This | # ]
10.3: Integrate maporama.com maps in Address Book
Authored by: aranor on Nov 16, '03 08:22:58PM

D'OH! I originally thought it didn't work because you have a high-ASCII char (opt-return), which won't work for applescript URLs (at least, not in 10.3, according to Apple). However, when I tried pasting the link for my script, the text didn't become a link. Maybe geeklog doesn't like huge URLs like that? Rob - can this be fixed?



[ Reply to This | # ]
10.3: Integrate maporama.com maps in Address Book
Authored by: ars on Nov 16, '03 09:17:47PM

Neither of the two scripts posted above, fill out the country code correctly with foreign addressses in my address book. I have tested this with French, Australian and Dutch addresses. I could see from the URL in Safari's address bar that the country code was US instead of FR or AU for the first two. When I manually changed the country code the map shows up correctly, so that the rest of the address is read correctly. When I tried it with the Dutch address it listed "missing" as country code and again when changing it to NL it worked. Any hint why this might be happening?



[ Reply to This | # ]
10.3: Integrate maporama.com maps in Address Book
Authored by: ars on Nov 16, '03 09:42:44PM

I found what the problem was: I had imported many of my addresses via Isync from my palmos pda, This import does not automatically format foreign addresses in the format used by the other country (the palm address book does not support these other formats). When I changed (via the address book's help) the addresses to the right format it works.



[ Reply to This | # ]
10.3: Integrate maporama.com maps in Address Book
Authored by: BradW on Nov 16, '03 10:46:46PM

Yep, that compiled just fine. Thanks for letting me know what the actual characters should be. Worked like a charm!

Brad



[ Reply to This | # ]
10.3: Integrate maporama.com maps in Address Book
Authored by: timstringer on Nov 17, '03 07:56:47AM

The script works without problem for US addresses, but I haven't had any luck with Canadian addresses. The country ends up getting sent in the URL as "unknown" and the search ends up getting performed in Afganistan (which produces some interesting, albeit not very useful results).

Any suggestions? Thanks!



[ Reply to This | # ]
10.3: Integrate maporama.com maps in Address Book
Authored by: aranor on Nov 17, '03 07:11:36PM

Make sure the country is filled out correctly and make sure the address format is set to Canada.



[ Reply to This | # ]
10.3: Integrate maporama.com maps in Address Book
Authored by: timstringer on Nov 18, '03 09:13:17AM

Thanks for your suggestions. The script fails even if both the Country field and the address format are set to "Canada".

I ended up creating a special "Map with Maporama (Canada)" version of the script that forces the country to Canada. Any more suggestions are welcome.

Tim



[ Reply to This | # ]
10.3: Dail the number using modem
Authored by: ces3001 on Nov 19, '03 06:56:26PM
I'm looking for a (free) way to use an AppleScript plug-in in Address Book to dial the a phone number with my modem, so that I can then just pick up the receiver on my phone to talk with the person. I've looked far and wide and there are no OSAX or other freeware programs that do this with AppleScript, as far as I can tell.

Javier Díaz Reinoso provides a Cocoa "dial with modem" program, with source. Any one care to make it AppleScriptable?

[ Reply to This | # ]

10.3: Integrate maporama.com maps in Address Book
Authored by: vondrix on Jan 07, '04 09:05:38PM
Here are some more modifications. (based on aranors script). They both show only the map (1000x1000). The first script shows it directly in Safari, the second one first downloads the image to your desktop, and opens it in preview. (note that it may take a few seconds, and doesn't really show any progress information) Show in browser:


using terms from application "Address Book"
	on action property
		return "address"
	end action property
	
	on action title for aPerson with anAddress
		return "Show map (maporama)"
	end action title
	
	on should enable action for aPerson with anAddress
		return true
	end should enable action
	
	on perform action for aPerson with anAddress
		set imageUrl to my getMap(street of anAddress, zip of anAddress, city of anAddress, state of anAddress, country code of anAddress)
		tell application "Safari"
			activate
			open location imageUrl
		end tell
	end perform action
end using terms from

on getMap(street, zip, city, state, country)
	-- Create GET string for maporama
	-- COUNTRYCODE _XgoGCAddress Zip State _XgoGCTownName
	if country is missing value then
		set countryCode to "US"
	else
		set countryCode to toUpper(country)
	end if
	set theUrl to "http://www.maporama.com/share/map.asp?" & "COUNTRYCODE=" & encode_text(countryCode) & "&_XgoGCAddress=" & encode_text(street) & "&Zip=" & encode_text(zip) & "&State=" & encode_text(state) & "&_XgoGCTownName=" & encode_text(city)
	return image_url(theUrl)
end getMap

-- this sub-routine is used to get the url for the 1000x1000 image, given the url to the normal maporama page map
on image_url(textUrl)
	return do shell script "curl -s '" & textUrl & "' | awk -F'image.asp' '{print $2}' |sed -e 's/amp;//g' | sed -e 's/[23][67][03]/1000/g' | awk -F'\"' '/XMLOUT/ {print \"http://www.maporama.com/image.asp\" $1}'"
end image_url

-- this sub-routine is used to encode text 
on encode_text(this_text)
	return do shell script "php -r 'echo urlencode(\"" & this_text & "\");'"
end encode_text

-- this sub-routine uppercases a string
on toUpper(this_text)
	return do shell script "php -r 'echo strtoupper(\"" & this_text & "\");'"
end toUpper
Show in preview.app


using terms from application "Address Book"
	on action property
		return "address"
	end action property
	
	on action title for aPerson with anAddress
		return "Download map to desktop (maporama)"
	end action title
	
	on should enable action for aPerson with anAddress
		return true
	end should enable action
	
	on perform action for aPerson with anAddress
		set imageUrl to my getMap(street of anAddress, zip of anAddress, city of anAddress, state of anAddress, country code of anAddress)
		
		set outputFileName to ((path to desktop) as string) & "Map of " & name of aPerson & ".gif"
		tell application "URL Access Scripting"
			download imageUrl to outputFileName replacing yes with progress
		end tell
		tell application "Finder" to open file outputFileName
		
	end perform action
end using terms from

on getMap(street, zip, city, state, country)
	-- Create GET string for maporama
	-- COUNTRYCODE _XgoGCAddress Zip State _XgoGCTownName
	if country is missing value then
		set countryCode to "US"
	else
		set countryCode to toUpper(country)
	end if
	set theUrl to "http://www.maporama.com/share/map.asp?" & "COUNTRYCODE=" & encode_text(countryCode) & "&_XgoGCAddress=" & encode_text(street) & "&Zip=" & encode_text(zip) & "&State=" & encode_text(state) & "&_XgoGCTownName=" & encode_text(city)
	return image_url(theUrl)
end getMap

-- this sub-routine is used to get the url for the 1000x1000 image, given the url to the normal maporama page map
on image_url(textUrl)
	return do shell script "curl -s '" & textUrl & "' | awk -F'image.asp' '{print $2}' |sed -e 's/amp;//g' | sed -e 's/[23][67][03]/1000/g' | awk -F'\"' '/XMLOUT/ {print \"http://www.maporama.com/image.asp\" $1}'"
end image_url

-- this sub-routine is used to encode text 
on encode_text(this_text)
	return do shell script "php -r 'echo urlencode(\"" & this_text & "\");'"
end encode_text

-- this sub-routine uppercases a string
on toUpper(this_text)
	return do shell script "php -r 'echo strtoupper(\"" & this_text & "\");'"
end toUpper


[ Reply to This | # ]
10.3: Integrate maporama.com maps in Address Book
Authored by: luckyluke666 on Jan 08, '04 04:19:02AM

on my system (10.3.2) this script does not compile:
on perform action for aPerson with anAddress

Syntax Error
Expected end of line but found identifier

("aPerson" is highlighted)

---
Luc Bourhis



[ Reply to This | # ]